anonymous13
anonymous13

Reputation: 621

Converting csv to geojson in Python

I am trying to convert a file to Geojson in python and I have below code. I have tried adding row indexes to the code, but the error is still the same.

import csv
import json
from collections import OrderedDict

li = []
with open('sample.csv', 'r') as csvfile:
    reader = csv.reader(csvfile, delimiter=',')
    for lat,long in reader:
        d = OrderedDict()
        d['type'] = 'Feature'
        d['geometry'] = {
            'type': 'Point',
            'coordinates': [float(lat), float(long)]
        }
        li.append(d)

d = OrderedDict()
d['type'] = 'FeatureCollection'
d['features'] = li

I am getting below error,

"too many values to unpack (expected 2)"

sample data

event_id    time_0  time_1  signal  description signal_name_value_desc  product long    lat
a   6/30/2018 18:39 6/30/2018 18:39 1   description1    signal_name1    product-1   -84.52694   46.931625
a   6/30/2018 18:39 6/30/2018 18:39 1   description1    signal_name1    Product - 1 -84.52684   46.931725
a   10/15/2018 21:10    10/15/2018 21:11    1   description1    signal_name1    Product - 2 -94.25526333    42.71689167
a   10/15/2018 21:10    10/15/2018 21:11    1   description1    signal_name1    Product - 2 -94.25524667    42.71689333
a   10/15/2018 21:10    10/15/2018 21:11    1   description1    signal_name1    Product - 2 -94.25519167    42.716895
b   10/15/2018 21:10    10/15/2018 21:11    1   description1    signal_name1    Product - 2 -94.25505167    42.71690833
b   10/15/2018 21:12    10/15/2018 21:13    1   description1    signal_name1    Product - 2 -94.25531167    42.71687167
b   10/15/2018 21:12    10/15/2018 21:13    1   description1    signal_name1    

This is the output I am expecting

{
   "type": "FeatureCollection",
   "features": [
  {
    "type": "Feature",
    "geometry": {
       "type": "Point",
       "coordinates":  [ -84.52694,46.931625 ]
    },
    "properties": {
    "event_id":"a",
    "time_0":"6/30/2018 18:39",
    "time_1":"6/30/2018 18:39",
    "signal":"1",
    "description":"description1",
    "signal_name_value_desc":"signal_name1",
    "product":"product-1",
    }
  }

How can I convert csv to GeoJson. Thanks in advance

Upvotes: 1

Views: 2571

Answers (1)

Mark Tolonen
Mark Tolonen

Reputation: 177745

Since you stated you have 13 columns in your .CSV file, the problem is here:

for lat,long in reader:

That line expects a line of reader to have two columns. Example:

>>> lat,long = 1,2,3,4,5,6,7,8,9,10,11,12,13
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
ValueError: too many values to unpack (expected 2)

You can specify variables for all 13 columns, or use this syntax:

>>> lat,long,*the_rest = 1,2,3,4,5,6,7,8,9,10,11,12,13
>>> lat
1
>>> long
2
>>> the_rest
[3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13]

You can also just use the following and index the required columns:

for row in reader:
    lat = row[0]   # or whatever the columns really are
    long = row[1]

If your data has headers like you've shown, here's an example of using a DictReader to reference columns by name.

Given input.csv:

event_id,time_0,time_1,signal,description,signal_name_value_desc,product,long,lat
a,6/30/2018 18:39,6/30/2018 18:39,1,description1,signal_name1,product-1,-84.52694,46.931625

This code:

import csv
import json
from collections import OrderedDict

li = []
with open('input.csv', newline='') as csvfile:
    reader = csv.DictReader(csvfile)
    for row in reader:
        d = OrderedDict()
        d['type'] = 'Feature'
        d['geometry'] = {
            'type': 'Point',
            'coordinates': [float(row['lat']), float(row['long'])]
        }
        li.append(d)

d = OrderedDict()
d['type'] = 'FeatureCollection'
d['features'] = li

with open('output.json','w') as f:
    json.dump(d,f,indent=2)

Produces output.json:

{
  "type": "FeatureCollection",
  "features": [
    {
      "type": "Feature",
      "geometry": {
        "type": "Point",
        "coordinates": [
          46.931625,
          -84.52694
        ]
      }
    }
  ]
}

Upvotes: 4

Related Questions