简体   繁体   中英

Determining what points and polygons are in a grid square

I am using python and geojson to do this, I want to specify a point and that point will be the center of a square, assuming the square is 1 mile by one mile I want to list all the points and polys found in the square, including polys bigger than the square.

I have multiple geojson files so will need to do the check a few times which is fine. I have been playing with the code below which checks to see if the cell center is near the centre of the square but will have issues for oddly shaped polygons. I really want to know all items / features that are found in the square.

import json
from shapely.geometry import shape, Point
from shapely.geometry import asShape, mapping

point = Point(14.9783266342289, 16.87265432621112)
max_distance_from_center = 1

with open('cells.geojson') as f:
    js = json.load(f)

for feature in js['features']:
    polygon = asShape(feature['geometry'])
    distance = point.distance(polygon.centroid)
    # print(f'{distance} - {polygon.centroid}')
    if distance < max_distance_from_center:
        print (f'Found cells containing polygon:{feature}')

For source data I was using a exported map from https://azgaar.github.io/Fantasy-Map-Generator/ the grid should be 10 miles by 10 miles. Suggestions on how to do this?

Update:

Here is a poorly drawn diagram. Within the grid square I want to identify all markers and polygons that fall within the bounds of the square even if they go out side of it. I want to have a list of all features that have some presence in the grid square. I highlighted the areas in yellow.

Poorly draw image

I looked at intersects and it may do it. Will try tonight.

you can try this:

First, create grid.

from shapely.geometry import Point
from matplotlib.pyplot as plt

point = Point(0, -10)
square = point.buffer(0.5).envelope

fig, ax = plt.subplots(figsize=(5,5))
gpd.GeoSeries(square).plot(ax=ax)
gpd.GeoSeries(point).plot(ax=ax, color = "black",markersize=30)
plt.grid()
plt.show()

在此处输入图片说明
and then,

import geopandas as gpd
# get geodataframe from geojson file
geo_df = gpd.GeoDataFrame.from_file('cells.geojson')
geo_df['grid_yn'] = geo_df['geometry'].apply(lambda x : x.intersects(square))

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM