Take the 2-minute tour ×
Geographic Information Systems Stack Exchange is a question and answer site for cartographers, geographers and GIS professionals. It's 100% free, no registration required.

I'm looking for demographic information and various things, and most of the time data are available as CSV (my application use geoJSON format).

Shall I encode the CSV to geoJSON or is there a "library" for geoJSON ? Some kind of a database of geojson files ?

Any help is welcome :)

Cheers

share|improve this question
2  
geojson.org/geojson-spec.html –  Mapperz Jun 13 '12 at 14:30
    
Ah yes this the spec for geoJson, useful :) –  Olivier Jun 14 '12 at 17:50
add comment

2 Answers

up vote 4 down vote accepted

if u use python, you can handle csv file for generating geojson form it....

the basic way:

import csv, json

file = open( 'test.csv', 'r' )
reader = csv.DictReader( file , fieldnames = ( "id","name","attribute","lat","lng" ) )
outfile = json.dumps( [ row for row in reader ] )

print outfile

out = open("output.txt", "w")
out.write(outfile) 
out.close()

the other way is from Andrew Dyck here with using geojson template which u created:

import csv

# Read in raw data from csv
rawData = csv.reader(open('sample.csv', 'rb'), dialect='excel')

# the template. where data from the csv will be formatted to geojson
template = \
    ''' \
    { "type" : "Feature",
        "id" : %s,
            "geometry" : {
                "type" : "Point",
                "coordinates" : ["%s","%s"]},
        "properties" : { "name" : "%s", "value" : "%s"}
        },
    '''

# the head of the geojson file
output = \
    ''' \
{ "type" : "Feature Collection",
    {"features" : [
    '''

# loop through the csv by row skipping the first
iter = 0
for row in rawData:
    iter += 1
    if iter >= 2:
        id = row[0]
        lat = row[1]
        lon = row[2]
        name = row[3]
        pop = row[4]
        output += template % (row[0], row[2], row[1], row[3], row[4])

# the tail of the geojson file
output += \
    ''' \
    ]
}
    '''

# opens an geoJSON file to write the output to
outFileHandle = open("output.geojson", "w")
outFileHandle.write(output)
outFileHandle.close()

i hope it helps you...

share|improve this answer
1  
there is also a geojson specific python library in addition to the json library. –  RyanDalton Jun 13 '12 at 16:13
    
Thank you I'll try to have a good use of this :) –  Olivier Jun 14 '12 at 16:22
add comment

If you're only concerned with point data, Aragon's example is perfect. For more complex shapes you will need a bit more.

You can use ogr2ogr to convert nearly any type of data to geoJSON. I would employ some quality checks before relying on the conversion.

ogr2ogr -f geoJSON out.json in.shp

If you're more comfortable with a GUI, qGIS uses the same methods but it's easier to view / modify your process. qGIS can also help you import data from a CSV.

share|improve this answer
    
I briefly used qGIS in the past but didn't realize it could convert shapefile/csv to geoJson ! I'll give a try to ogr2ogr. –  Olivier Jun 15 '12 at 11:53
    
I haven't used it in a while, but I think its in the file types under File > Save As. –  Geoist Jun 15 '12 at 12:09
add comment

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.