缩放 GeoJSON 以查找附近的经纬度点
Scale GeoJSON to find latitude and longitude points nearby
我正在尝试使用 shapely 缩放 GeoJSON 多边形(任何其他方法都可以,只要我可以 运行 在 AWS Lambda 函数上使用)。我的目标是找到 GeoJSON 中描述的区域附近的纬度和经度点。 GeoJSON 文件有所不同,例如:
{"type": "FeatureCollection", "features": [{"type": "Feature", "properties": {}, "geometry": {"type": "MultiPolygon", "coordinates": [[[[-1.4502, 51.2757], [-1.7798, 51.3443], [-3.7793, 50.9446], [-5.2515, 50.8059], [-5.4053, 50.6947], [-5.6689, 50.3455], [-5.6689, 50.3034], [-5.5371, 50.2332], [-4.5923, 50.2191], [-3.7134, 50.3455], [-3.4058, 50.4855], [-2.3511, 50.8059], [-1.7139, 50.8198], [-1.626, 50.8476], [-1.4502, 51.0276], [-1.4502, 51.2757]]]]}}]}
我目前的代码是:
import json
from shapely import affinity
from shapely.geometry import shape, Point, mapping
def handler(event, context):
with open('polygon.json') as f:
js = json.load(f)
polygon = shape(js['features'][0]['geometry'])
polygon_nearby = affinity.scale(polygon, xfact=1.1, yfact=1.1)
print(json.dumps({"type": "FeatureCollection", "features": [{"type": "Feature", 'properties': {}, 'geometry': mapping(polygon)}]}))
print(json.dumps({"type": "FeatureCollection", "features": [{"type": "Feature", 'properties': {}, 'geometry': mapping(polygon_nearby)}]}))
for point in [(-1.7798, 51.3442), (-1.7798, 51.3444), (-1.4504, 51.27), (-1.4503, 51.28)]:
if polygon.contains(Point(point)):
print('Found point in polygon', point)
elif polygon_nearby.contains(Point(point)):
print('Found point nearby', point)
else:
print("Not found", point)
if __name__ == '__main__':
handler(None, None)
问题是,当多边形缩放时,它当前是从一个中心点开始缩放的。中心点上方的边界未缩放,如下图所示:
上图是原始 GeoJSON(深灰色)和附近的 GeoJSON(浅灰色)的表示。
普尔上方的边界(在地图上)对于两者都是相同的。我认为这是因为边界位于它缩放的中心点之上。
我想要实现的是 polygon_nearby
进行缩放,以便所有边界都大于原始 polygon
或任何其他在附近找到纬度和经度点的方法原始 GeoJSON 中描述的区域。
我想通了:
polygon_nearby = polygon.buffer(0.1)
我正在尝试使用 shapely 缩放 GeoJSON 多边形(任何其他方法都可以,只要我可以 运行 在 AWS Lambda 函数上使用)。我的目标是找到 GeoJSON 中描述的区域附近的纬度和经度点。 GeoJSON 文件有所不同,例如:
{"type": "FeatureCollection", "features": [{"type": "Feature", "properties": {}, "geometry": {"type": "MultiPolygon", "coordinates": [[[[-1.4502, 51.2757], [-1.7798, 51.3443], [-3.7793, 50.9446], [-5.2515, 50.8059], [-5.4053, 50.6947], [-5.6689, 50.3455], [-5.6689, 50.3034], [-5.5371, 50.2332], [-4.5923, 50.2191], [-3.7134, 50.3455], [-3.4058, 50.4855], [-2.3511, 50.8059], [-1.7139, 50.8198], [-1.626, 50.8476], [-1.4502, 51.0276], [-1.4502, 51.2757]]]]}}]}
我目前的代码是:
import json
from shapely import affinity
from shapely.geometry import shape, Point, mapping
def handler(event, context):
with open('polygon.json') as f:
js = json.load(f)
polygon = shape(js['features'][0]['geometry'])
polygon_nearby = affinity.scale(polygon, xfact=1.1, yfact=1.1)
print(json.dumps({"type": "FeatureCollection", "features": [{"type": "Feature", 'properties': {}, 'geometry': mapping(polygon)}]}))
print(json.dumps({"type": "FeatureCollection", "features": [{"type": "Feature", 'properties': {}, 'geometry': mapping(polygon_nearby)}]}))
for point in [(-1.7798, 51.3442), (-1.7798, 51.3444), (-1.4504, 51.27), (-1.4503, 51.28)]:
if polygon.contains(Point(point)):
print('Found point in polygon', point)
elif polygon_nearby.contains(Point(point)):
print('Found point nearby', point)
else:
print("Not found", point)
if __name__ == '__main__':
handler(None, None)
问题是,当多边形缩放时,它当前是从一个中心点开始缩放的。中心点上方的边界未缩放,如下图所示:
上图是原始 GeoJSON(深灰色)和附近的 GeoJSON(浅灰色)的表示。 普尔上方的边界(在地图上)对于两者都是相同的。我认为这是因为边界位于它缩放的中心点之上。
我想要实现的是 polygon_nearby
进行缩放,以便所有边界都大于原始 polygon
或任何其他在附近找到纬度和经度点的方法原始 GeoJSON 中描述的区域。
我想通了:
polygon_nearby = polygon.buffer(0.1)