Created
September 10, 2021 11:48
-
-
Save jacquesfize/13c61406a218896fc7f688d48871b2fd to your computer and use it in GitHub Desktop.
Build a hexagon grid over a shapely geometry
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import geopandas as gpd | |
from shapely.geometry import shape | |
CB = None # replace by a shapely geometry object. | |
r=0.2 # radius of the hexagon | |
h = (r * np.sqrt(3))/2 | |
j = r/2 | |
hex_array=[] | |
y= ymin | |
flip = True | |
while y <ymax: | |
x=xmin | |
if flip: | |
x+=r+r/2 | |
while x < xmax: | |
hexagon = shape( | |
{ | |
"type": "Polygon", | |
"coordinates": [ | |
[ | |
[x +j, y - h], | |
[x + r, y], | |
[x+j,y+h], | |
[x-j, y + h], | |
[x -r, y], | |
[x - h / 2, y-h] | |
] | |
], | |
} | |
) | |
x+=r*3 | |
if CB.intersects(hexagon): | |
hex_array.append(hexagon) | |
y+=h | |
flip=not flip | |
gpd.GeoDataFrame({"geometry":hex_array}).boundary.plot() |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment