简体   繁体   中英

In Python, trying to convert geocoded tsv file into geojson format

trying to convert a geocoded TSV file into JSON format but i'm having trouble with it. Here's the code:

import geojson
import csv

def create_map(datafile):
    geo_map = {"type":"FeatureCollection"}
    item_list = []
    datablock = list(csv.reader(datafile))
    for i, line in enumerate(datablock):
        data = {}
        data['type'] = 'Feature'
        data['id'] = i
        data['properties']={'title': line['Movie Title'],
                           'description': line['Amenities'],
                           'date': line['Date']}
        data['name'] = {line['Location']}
        data['geometry'] = {'type':'Point',
                           'coordinates':(line['Lat'], line['Lng'])}
        item_list.append(data)
    for point in item_list:
        geo_map.setdefault('features', []).append(point)
    with open("thedamngeojson.geojson", 'w') as f:
        f.write(geojson.dumps(geo_map))

create_map('MovieParksGeocode2.tsv')

I'm getting a TypeError:list indices must be integers, not str on the data['properties'] line but I don't understand, isn't that how I set values to the geoJSON fields?

The file I'm reading from has values under these keys: Location Movie Title Date Amenities Lat Lng

The file is viewable here: https://github.com/yongcho822/Movies-in-the-park/blob/master/MovieParksGeocodeTest.tsv

Thanks guys, much appreciated as always.

You have a couple things going on here that need to get fixed.

1.Your TSV contains newlines with double quotes. I don't think this is intended, and will cause some problems.

Location    Movie Title Date    Amenities   Formatted_Address   Lat Lng
"
Edgebrook Park, Chicago "   A League of Their Own   7-Jun   "
Family friendly activities and games. Also: crying is allowed." Edgebrook Park, 6525 North Hiawatha Avenue, Chicago, IL 60646, USA  41.9998876  -87.7627672
"

2.You don't need the geojson module to dump out JSON - which is all GeoJSON is. Just import json instead.

3.You are trying to read a TSV, but you don't include the delimiter=\\t option that is needed for that.

4.You are trying to read keys off the rows, but you aren't using DictReader which does that for you.Hence the TypeError about indices you mention above.

Check out my revised code block below..you still need to fix your TSV to be a valid TSV.

import csv
import json

def create_map(datafile):
    geo_map = {"type":"FeatureCollection"}
    item_list = []
    with open(datafile,'r') as tsvfile:
        reader = csv.DictReader(tsvfile,delimiter='\t')
        for i, line in enumerate(reader):
            print line
            data = {}
            data['type'] = 'Feature'
            data['id'] = i
            data['properties']={'title': line['Movie Title'],
                               'description': line['Amenities'],
                               'date': line['Date']}
            data['name'] = {line['Location']}
            data['geometry'] = {'type':'Point',
                               'coordinates':(line['Lat'], line['Lng'])}
            item_list.append(data)
        for point in item_list:
            geo_map.setdefault('features', []).append(point)
        with open("thedamngeojson.geojson", 'w') as f:
            f.write(json.dumps(geo_map))

create_map('MovieParksGeocode2.tsv')

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM