import numpy as np
import matplotlib.pyplot as plt
import math
# import json
import geojson
from shapely.geometry import asShape
from shapely import wkt
from shapely.geometry import GeometryCollection,Polygon,Point,LineString
import os
width = 200;
height = 400
img = np.zeros((width,height))
geoms = []
path = r'F:\builddata\mkt_wuh\hzw\12345\12_AB_DATA\1209_AB_SEM_DES'
lists = os.listdir(path)
for item in lists:
if item.endswith(".geojson"):
with open(path+'/'+item) as f:
data = f.read()
djs = geojson.loads(data)
if djs['features'][0]['geometry'] is not None:
features = djs['features']
for feature in features:
line = asShape(feature['geometry'])
geoms.append(line)
gc = GeometryCollection(geoms)
# 左下角右上角
xmin,ymin,xmax,ymax = gc.bounds
dx = (xmax - xmin) / width
dy = (ymax - ymin) / height
for g in geoms:
xys=None
if g.geom_type == 'Polygon':
xys = g.exterior.coords.xy
elif g.geom_type == 'LineString':
xys = g.coords.xy
elif g.geom_type == 'Point':
xys = g.coords.xy
xs = xys[0]
ys = xys[1]
for i in range(len(xs)):
x = xs[i]
y = ys[i]
perx = int(math.floor((x - xmin)/dx) -1)
pery = int(math.floor((y - ymin)/dy) -1)
if pery >= 400:
print(pery)
img[perx,pery] = 234
fig = plt.figure()
plt.imshow(img, interpolation ='none', aspect = 'auto')
plt.show()
网友评论