[英]Scale GeoJSON to find latitude and longitude points nearby
I am trying to scale GeoJSON polygons using shapely (any other method will do as long as I can run it on an AWS Lambda function).我正在尝试使用 shapely 缩放 GeoJSON 多边形(只要我可以在 AWS Lambda 函数上运行它,任何其他方法都可以)。 My goal is to find latitude and longitude points near the area described in the GeoJSON.
我的目标是在 GeoJSON 中描述的区域附近找到经纬度点。 The GeoJSON files vary an example is:
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]]]]}}]}
The code I currently have is:我目前拥有的代码是:
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)
The problem is that when the polygon scales it currently scales from a central point.问题是当多边形缩放时,它当前是从中心点缩放的。 Boundaries above the central point are not scaled, as shown in the following image:
中心点上方的边界未缩放,如下图所示:
The image above is a representation of the original GeoJSON (dark grey) and the nearby GeoJSON (light grey).上图是原始 GeoJSON(深灰色)和附近 GeoJSON(浅灰色)的表示。 The boundary above Poole (on the map) is the same for both.
普尔上方的边界(在地图上)对两者来说是相同的。 This I think is because the boundary is above the central point which it was scaled from.
我认为这是因为边界高于它被缩放的中心点。
What I am trying to achieve is for the polygon_nearby
to be scaled so that all the boundaries are bigger than in the original polygon
or any other method of finding latitude and longitude points near the area described in the original GeoJSON.我想要实现的是对
polygon_nearby
进行缩放,以使所有边界都大于原始polygon
或任何其他在原始 GeoJSON 中描述的区域附近查找纬度和经度点的方法。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.