Search code examples
pythongeojsonshapely

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). My goal is to find latitude and longitude points near the area described in the GeoJSON. The GeoJSON files vary an example is:

{"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: GeoJSON rendered to show problem with scaling

The image above is a representation of the original GeoJSON (dark grey) and the nearby GeoJSON (light grey). 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.


Solution

  • I figured it out use:

    polygon_nearby = polygon.buffer(0.1)
    

    enter image description here