1

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

3
  • I have about 13 columns in my csv file Commented Dec 8, 2019 at 21:19
  • have you a example that how you expect the result json? and to can you share a example of your csv file..? Commented Dec 8, 2019 at 21:40
  • I have posted sample data.I am unable to upload csv file Commented Dec 9, 2019 at 17:53

1 Answer 1

4

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
        ]
      }
    }
  ]
}
Sign up to request clarification or add additional context in comments.

6 Comments

I tried specifying variables for the columns but the error is still the same
@anonymous13 Please edit your question with the actual CSV. The one shown does not have commas for example. Also include the full trace back that indicates where the error is occurring.
I am not able to add csv to the question
@anonymous13 Not the whole CSV...just cut-n-paste a couple lines from the top...change any data that is sensitive, but get the sample format right. See my example above of input.csv. Your question should show a minimal reproducible example that produces the error.
Thank you the code works, but the error is because one of my lat or long column has an null value. How can I remove 'NA' from any of the columns in csv reader? I am familiar with removing it from dataframes.
|

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.