2017-09-28 14 views
0

J'ai trois fichiers GeoJSON contenant les limites des districts scolaires. Chaque fichier contient les données pour un groupe particulier de districts scolaires (School_District_Unified.geojson, School_District_Elementary.geojson, School_District_Secondary.geojson).Fusion de fichiers GeoJSON pour les districts scolaires

Link aux fichiers GeoJSON ici.

Le script que je suis en train d'utiliser est de this post, mais ne pas avoir de succès:

import json 
import geojson 
from functools import partial 
import pyproj 
import shapely.geometry 
import shapely.ops 

# reading into three geojson objects, in a GCS (WGS84) 
with open('School_District_Elementary.geojson') as geojson1: 
    poly1_geojson = json.load(geojson1) 

with open('School_District_Secondary.geojson') as geojson2: 
    poly2_geojson = json.load(geojson2) 

with open('School_District_Unified.geojson') as geojson3: 
    poly3_geojson = json.load(geojson3) 

# pulling out the polygons 
poly1 = shapely.geometry.asShape(poly1_geojson['features'][2] 
['geometry']) 
poly2 = shapely.geometry.asShape(poly2_geojson['features'][2] 
['geometry']) 
poly3 = shapely.geometry.asShape(poly3_geojson['features'][2] 
['geometry']) 

# checking to make sure they registered as polygons 
print poly1.geom_type 
print poly2.geom_type 
print poly3.geom_type 

# merging the polygons - they are feature collections, containing a 
# point, a polyline, and a polygon - I extract the polygon 
# for my purposes, they overlap, so merging produces a single polygon 
# rather than a list of polygons 
mergedPolygon = poly1.union(poly2) 

# using geojson module to convert from WKT back into GeoJSON format 
geojson_out = geojson.Feature(geometry=mergedPolygon, properties={}) 

# outputting the updated geojson file - for mapping/storage in its 
#GCS format 
with open('Merged_Polygon.json', 'w') as outfile: 
    json.dump(geojson_out.geometry, outfile, indent=3, 
encoding="utf-8") 
outfile.close() 

# reprojecting the merged polygon to determine the correct area 
# it is a polygon covering much of the US, and derived from USGS 
#data, so using Albers Equal Area 
project = partial(
    pyproj.transform, 
    pyproj.Proj(init='epsg:4326'), 
    pyproj.Proj(init='epsg:5070')) 

mergedPolygon_proj = shapely.ops.transform(project,mergedPolygon) 

Répondre

0

json.dump(geojson_out.geometry, outfile, indent=3, encoding="utf-8")

json.dump n'a pas un paramètre encoding.

1) Supprimer le codage de l'appel json.dump.

json.dump(geojson_out.geometry, outfile, indent=3) 

2) Changement d'utiliser le codage dans le fichier ouvert

with open('Merged_Polygon.json', 'w', encoding='utf8') as outfile: 
    json.dump(geojson_out.geometry, outfile, ensure_ascii=False) 
+1

Lorsque vous utilisez 'avec()', le gestionnaire de contexte, il n'y a pas besoin de 'close()'. – Parfait