Quiero transformar un archivo json ciudades.json
en un archivo geojson. Por ejemplo :
[
{
"Province": "AZILAL",
"Commune": "AIT ABBAS",
"Type_Commune": "Rural",
"Nbre_Inscrits": 445,
"Nbre_Votants": 132,
"Nbre_Nuls": 12,
"Nbre_Exprimees": 120,
"Turnout": 0.2696629213483146,
"PI": 1,
"PJD": 47,
"PAM": 55,
"USFP": 0,
"APFGD": 0,
"PPS": 11,
"RNI": 0,
"MP": 0,
"UC": 0,
"FFD": 1,
"MDS": 1,
"AAR": 0,
"P Néo-Démocrates": 1,
"PEDD": 0,
"PRD": 2,
"Winner": "PAM",
"Swing": 1,
"longitude": -6.5763368,
"latitude": 33.1926354
},
{
"Province": "AZILAL",
"Commune": "AIT ABBAS",
...
Sólo sé que para convertir mis datos a un GeoJSON Feature
o FeatureCollection
. Las geometrías GeoJSON
, Point
, Polygon
, MultiPolygon
, etc., no soportan atributos como ipAddress y score, en su caso. Cómo definir una característica que tiene una geometría y atributos?
Intente lo siguiente:
#! usr/bin/env python
from sys import argv
from os.path import exists
import simplejson as json
in_file, out_file = "map_input_pol_analysis.json", "map_input_pol_stn_analysis.geojson"
data = json.load(open(in_file))
geojson = {
"type": "FeatureCollection",
"features": [
{
"type": "Feature",
"geometry" : {
"type": "Point",
"coordinates": [d["longitude"], d["latitude"]],
},
"properties" : d,
} for d in data]
}
output = open(out_file, 'w')
json.dump(geojson, output)
print(geojson)
Pero me devuelve :
---------------------------------------------------------------------------
UnicodeDecodeError Traceback (most recent call last)
<ipython-input-88-a9b360e66970> in <module>
7 in_file, out_file = "map_input_pol_analysis.json", "map_input_pol_stn_analysis.geojson"
8
----> 9 data = json.load(open(in_file))
10
11 geojson = {
C:\ProgramData\Anaconda3\lib\site-packages\simplejson\__init__.py in load(fp, encoding, cls, object_hook, parse_float, parse_int, parse_constant, object_pairs_hook, use_decimal, namedtuple_as_object, tuple_as_array, **kw)
455
456 """
--> 457 return loads(fp.read(),
458 encoding=encoding, cls=cls, object_hook=object_hook,
459 parse_float=parse_float, parse_int=parse_int,
C:\ProgramData\Anaconda3\lib\encodings\cp1252.py in decode(self, input, final)
21 class IncrementalDecoder(codecs.IncrementalDecoder):
22 def decode(self, input, final=False):
---> 23 return codecs.charmap_decode(input,self.errors,decoding_table)[0]
24
25 class StreamWriter(Codec,codecs.StreamWriter):
UnicodeDecodeError: 'charmap' codec can't decode byte 0x8f in position 2395751: character maps to <undefined>