1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59
| fname = './india_state_geo.json' with open(fname) as f: data = json.load(f) for i, c in enumerate(data['features']): if c['properties']['NAME_1'] in ['Haryana', 'Uttar Pradesh', 'Punjab']: print (i,c['properties']['NAME_1'] ) hary_shp = shape(data['features'][12]['geometry'])
import time start = time.time() from warnings import filterwarnings filterwarnings(action='ignore', category=DeprecationWarning, message='`np.bool` is a deprecated alias')
xx_, yy_ = np.meshgrid(lon_sa,lat_sa) mask_hary = shapely.vectorized.contains(hary_shp, xx_,yy_)
from shapely.geometry import shape, MultiPolygon import json
with open(fname) as f: data = json.load(f)
polygons = [shape(feature['geometry']) for feature in data['features']]
poly_index = [3,4,15,19,21,22,23,24,31,34] merged_polygon = polygons[poly_index[0]] for i,polygon in enumerate(polygons): if i in poly_index[1:]: merged_polygon = merged_polygon.union(polygon)
if type(merged_polygon) != MultiPolygon: merged_polygon = MultiPolygon([merged_polygon])
import fiona import json fname2 = './example.shp'
with fiona.open(fname2) as src: with open( 'example.json', "w") as output: output.write(json.dumps(list(src)))
with open('A.json') as f: A = json.load(f) with open('B.json') as f: B = json.load(f)
merged_polygon = A.union(shape(B[0]['geometry']))
|
Kommentare