10

I am needing a Python script to convert CSV data to GeoJSON output. The output should match the format, below:

{
  "type": "FeatureCollection",
  "features": [
    {
      "type": "Feature",
      "geometry": {
        "type": "Point",
        "coordinates":  [ -85.362709,40.466442 ]
      },
      "properties": {
        "weather":"Overcast",
        "temp":"30.2 F"
      }
    }
  ]
}

I am using this script to run the process, but it does not produce the desired output:

import csv, json
    li = []
    with open('CurrentObs.csv', newline='') as csvfile:
        reader = csv.reader(csvfile, delimiter=',')
        for latitude, longitude, weather, temp in reader:
            li.append({
               "latitude": latitude,
               "longitude": longitude,
               "weather": weather,
               "temp": temp,
               "geo": {
                    "__type": "GeoPoint",
                    "latitude": latitude,
                    "longitude": longitude,
                }
            })

    with open("GeoObs.json", "w") as f:
        json.dump(li, f)

Any help is greatly appreciated!

4
  • 1
    What is in CurrentObs.csv? Are you sure you need newline=''? Commented Feb 2, 2018 at 16:17
  • You say the output does not match the format above, but you are appending a completely different format to the list. What are you struggling with? Commented Feb 2, 2018 at 16:18
  • are latitude,longitude columns in your csv ? Commented Jun 24, 2018 at 22:48
  • i suggest that you post the content of your csv , for other people who struggle withe same issue/concern Commented Jun 24, 2018 at 22:59

2 Answers 2

18

one could use directly the geojson package:

import csv, json
from geojson import Feature, FeatureCollection, Point

features = []
with open('CurrentObs.csv', newline='') as csvfile:
    reader = csv.reader(csvfile, delimiter=',')
    for latitude, longitude, weather, temp in reader:
        latitude, longitude = map(float, (latitude, longitude))
        features.append(
            Feature(
                geometry = Point((longitude, latitude)),
                properties = {
                    'weather': weather,
                    'temp': temp
                }
            )
        )

collection = FeatureCollection(features)
with open("GeoObs.json", "w") as f:
    f.write('%s' % collection)
Sign up to request clarification or add additional context in comments.

2 Comments

that dosen't work for me !. are latitude and longitude columns in the csv or what ? i got an error in the loop =>my issue
@ewcz How would you modify this to have a proper FeatureCollection like: "type": "FeatureCollection", "features": [ "geometry": { "type": "Point", "coordinates": [ -73.935242, 40.730610 ] }, "properties": {...}] }
4

Check if this script resolves

import csv
import json
from collections import OrderedDict

li = []
with open('CurrentObs.csv', 'r') as csvfile:
    reader = csv.reader(csvfile, delimiter=',')
    for latitude, longitude, weather, temp in reader:
        d = OrderedDict()
        d['type'] = 'Feature'
        d['geometry'] = {
            'type': 'Point',
            'coordinates': [float(latitude), float(longitude)]
        }
        d['properties'] = {
            'weather': weather,
            'temp': temp
        }
        li.append(d)

d = OrderedDict()
d['type'] = 'FeatureCollection'
d['features'] = li
with open('GeoObs.json', 'w') as f:
    f.write(json.dumps(d, sort_keys=False, indent=4))

1 Comment

Nice solution, for applications like OpenLayers, you would want to flip the 'coordinates': [float(latitude), float(longitude)] to 'coordinates': [float(longitude), float(latitude)] and throw in some logic to catch the header...

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.