Fast Convert of large csv to geojson

旧巷老猫 提交于 2019-12-11 19:37:24

问题


I have a csv with 6 millions of rows and i need to convert it to a geojson file.

cloud solutions exists in the internet but it takes whole day to convert it.
Is there a quick way to do that with python ?

UPDATE OF QUESTION :

i tried the solution here but i'm getting this error :

ValueError                                Traceback (most recent call last)
<ipython-input-48-0224e45ed66e> in <module>()
  5 with open('Documents/neo4j-community-3.3.5/import/train.csv', newline='') as csvfile:
  6     reader = csv.reader(csvfile, delimiter=',')
----> 7     for pickup_latitude, pickup_longitude in reader:
  8         pickup_latitude,pickup_longitude = map(float,   (pickup_latitude, pickup_longitude))
  9         features.append(

ValueError: too many values to unpack (expected 2)

this is my code :

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

features = []
with open('Documents/neo4j-community-3.3.5/import/train.csv', newline='')   as csvfile:
reader = csv.reader(csvfile, delimiter=',')
for pickup_latitude, pickup_longitude in reader:
    pickup_latitude,pickup_longitude = map(float, (pickup_latitude, pickup_longitude))
    features.append(
        Feature(
            geometry = Point((pickup_longitude, pickup_latitude)),

        )
    )

collection = FeatureCollection(features) 
with open("GeoObs.json", "w") as f:
f.write('%s' % collection)

Note :pickup_latitude and pickup_longitude are two columns in my csv


回答1:


The error means that you have more columns in your csv file than just two which the for statement expects. To fix this, you could take all columns and then analyze only the first two:

for cols in reader:
    pickup_latitude, pickup_longitude = map(float, cols[0:2])


来源:https://stackoverflow.com/questions/51013294/fast-convert-of-large-csv-to-geojson

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!