2

The following code does not give me any errors when executed. However, when I drag/drop the file into url: geojson.io I get the following "invalid Json file:Syntax error unexpected token".

#!/usr/bin/python

import csv
import json
import geojson

csvfile = open('May_6th.csv', 'rU')
jsonfile = open('file.json', 'w')

fieldnames = {'type': 'FeatureCollection',
        'features':[("Name","Bearing (True)","Bearing (Mag)",
"Distance","Total Distance","Planned Speed", 
"Leg Time", "Total Time", "Turn", "Latitude", "Longitude")]}

reader = csv.DictReader( csvfile, fieldnames)



for row in reader:
    json.dump(row, jsonfile)
    jsonfile.write('\n')
print 'csv file converted'
4
  • When I edit the script- I only end up getting additional error when the script is executed in python. Commented May 8, 2015 at 21:17
  • What do you mean "drag/drop the file into url: geojson.io"? Are you saying you tried to paste this code into the input box on that web page? That doesn't make any sense. What you wrote is Python code, it's not geojson, so you can't paste it in where geojson is expected. What are you trying to do? Commented May 8, 2015 at 21:19
  • why are you jsonfile.write('\n')? Commented May 8, 2015 at 21:19
  • BrenBarn- You can drop a *.json extension file into geojson.io. I have done so in a python class- where the script is written in python. I am aiming to to the same with my csv file of lat and longs. Commented May 9, 2015 at 1:53

1 Answer 1

1

By most conventions, a JSON file represents a single javascript object. Your file is a series of objects, separated by newlines. What you're creating is sometimes called JSRec.

More to the point, there's no sign that you're making valid geojson. You import the library but you don't use it. You should probably review the docs at https://github.com/frewsxcv/python-geojson or the GeoJSON format specification to make sure that the file you're creating adheres to the GeoJSON format.

I note that the GeoJSON spec says specifically:

GeoJSON always consists of a single object. This object (referred to as the GeoJSON object below) represents a geometry, feature, or collection of features.

Sign up to request clarification or add additional context in comments.

4 Comments

You can seemingly paste json into geojson.io so the geojson is probably not needed.
I will try it without the geojson. But Joe the geometry is a MultiLineString (since I want to connect all of the points into a 'trackline', my feature is Name, and the remaining columns I have are: Bearing (True), Bearing (Mag), Distance, Total Distance, Planned Speed, Leg Time, Total Time, Turn, Latitude, Longitude. Would all of these be the FeatureCollection (without Lat and Long)....????
example data: Name|Bearing (True)|Bearing (Mag)|Distance|Total Distance| Planned Speed|Leg Time|Total Time|Turn|Latitude|Longitude 2 244.115796 228.012487 0.069682 0.069682 8.5 30 30 69 47.648986 -122.314021
@darcye to be honest, I haven't constructed geojson from raw data myself; I've only used it as the output of GIS tools. Best to consult the docs. Or, maybe someone else will write a more complete answer.

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.