user6240030 user6240030 - 6 months ago 17
Python Question

json converter only takes last key value for last dictionary instead of all

Ok so I have the following list of dictionarys that I am trying to convert to a json file:

geojson_list = [

{'name': 'Parallelogram1', 'coordinates':
[[115.67097179583487, -32.36672530921233], [115.96656222999665,
-32.36672530921233], [115.90410905434761, -32.49580085924758], [115.60851862018583, -32.49580085924758], [115.67097179583487,
-32.36672530921233]], 'area': 0.0381534978746},

{'name': 'Parallelogram2', 'coordinates': [[116.00622565359758,
-32.5791364092627], [116.02283522420637, -32.5791364092627], [116.02126260408991, -32.59706839673082], [116.00465303348112,
-32.59706839673082], [116.00622565359758, -32.5791364092627]],'area': 0.000297842612008}

]


This is the converter code named GeojsonConverter.py:

import json


def convert_to_geojson(my_list):
"""
This function converts a list of dictionaries into GeoJSON format
The dictionaries require a "coordinates" key whose value will be a 2D
list, a "name" key, with all other additional data.
:param my_list: A list of dictionaries
:return: a GeoJSON string
"""

try:
for d in my_list:
coord_list = d["coordinates"]
name = d["name"]
for coord in coord_list:
float(coord[0])
float(coord[1])

except ValueError:
print "ValueError: Coordinate cannot be converted to float."
return "ValueError: Coordinate cannot be converted to float."

except KeyError:
print "KeyError: No 'coordinates' or 'name' key found in dictionary"
return "KeyError: No 'coordinates' or 'name' key found in dictionary"

except Exception as e:
raise e

else:
feature_list = []
property_dict = {}

for d in my_list:
coord_list = d["coordinates"]
coord_list.append(d["coordinates"][0])
name = d["name"]

for key in d:
if (key is not "name") and (key is not "coordinates"):
property_dict[key] = d[key]

the_geom = {"type": "MultiPolygon", "coordinates": [[coord_list]]}
feature = {"type": "Feature", "geometry": the_geom, "name": name, "properties": property_dict}
feature_list.append(feature)

feature_collection = {"type": "FeatureCollection", "features": feature_list}

return json.dumps(feature_collection)


The converter converts the list just fine right up until the area key. I keep getting the last value in area of the last dictionary for all dictionary areas, so in this case all areas = 0.000297842612008

This is the json file I get after running the list through the converter and writing it to a file:

{ "type": "FeatureCollection", "features": [{"geometry": {"type":
"MultiPolygon", "coordinates": [[[[115.67097179583487,
-32.36672530921233], [115.96656222999665, -32.36672530921233], [115.90410905434761, -32.49580085924758], [115.60851862018583,
-32.49580085924758], [115.67097179583487, -32.36672530921233]]]]}, "type": "Feature", "name": "Parallelogram1", "properties": {"area":
0.000629970457642}},

{"geometry": {"type": "MultiPolygon", "coordinates": [[[[116.00622565359758, -32.5791364092627],
[116.02283522420637, -32.5791364092627], [116.02126260408991,
-32.59706839673082], [116.00465303348112, -32.59706839673082], [116.00622565359758, -32.5791364092627]]]]}, "type": "Feature",
"name": "Parallelogram2", "properties": {"area": 0.000629970457642} }


Notice the two different areas equal the same result when they should not.

The following code is how I am writing to a file.

import GeojsonConverter
my_geojson_string = GeojsonConverter2.convert_to_geojson(geojson_list)
name = "test"
try:
name = name[:-4] #subtract .csv from name to add a character onto the end of the file name. Eg. zzza.csv, not zzz.csva
with open("./datafiles/" + name + "JSON" + ".geojson", 'w') as jsondata: #Save json data into nameJSON.geojson
try:
print ""
print ("Writing json file: " + name + "JSON" + ".geojson")
jsondata.write(my_geojson_string)
except:
print "Error writing to file. FN: write to file"
sys.exit()
except:
print "Error opening file. FN: geojson output"


Where am I going wrong?

edit:

changed the last bit of the converter code to this

for d in my_list:
coord_list = d["coordinates"]
coord_list.append(d["coordinates"][0])
name = d["name"]
area_list = d["area"]

for key in d:
if (key is not "name") and (key is not "coordinates") and (key is not "area"):
property_dict[key] = d[key]
the_geom = {"type": "MultiPolygon", "coordinates": [[coord_list]]}
feature = {"type": "Feature", "geometry": the_geom, "name": name, "area": area_list, "properties": property_dict, }
feature_list.append(feature)

feature_collection = {"type": "FeatureCollection", "features": feature_list}

Answer

You are having a problem caused by variable reuse.

Every run through the for d in mylist: modifies property_dict, which then gets added to the feature_list. The next time through the loop, you modify the **same property_dict **, which overwrites the previous data. Moveing the property_dict = {} into the outer loop will fix this problem.