我正在开始使用 Python3,我正在编写一个脚本来将 CSV 文件转换为 GeoJSON 文件。 转换有效,但我仍然对坐标有问题,没有采用正确的格式。我需要删除双引号,但它不起作用。
这是我的部分代码:
def readCsvFile():
features=[]
geoJson={}
with open('resources/data_export.csv',newline= '') as csvfile:
csvReader = csv.DictReader(csvfile,delimiter=',',quotechar='|')
for row in csvReader:
features.append({'type': 'Feature',
'geometry': {
'type': 'Point', "coordinates" : [str(row['longitude']), str(row['latitude'])]}, 'properties': {'id': decodeString(row['id']), 'field1': decodeString(row['field1']), 'field2': decodeString(row['field2'])})
geoJson = {'type': 'FeatureCollection', 'features': features}
return json.dumps(geoJson)
我得到的结果是:
{"type": "FeatureCollection", "features": [{"type": "Feature", "geometry": {"type": "Point", "coordinates": ["16.394564", "48.246426"]}, "properties": {"id": "0", ...
虽然我应该得到:
{"type": "FeatureCollection", "features": [{"type": "Feature", "geometry": {"type": "Point", "coordinates": [16.394564, 48.246426]}, "properties": {"id": "0",
请您参考如下方法:
尝试将其转换为 float 而不是字符串:
"coordinates" : [float(row['longitude']), float(row['latitude'])]}
