nismod / snail

spatial networks impact assessment library 🐌
https://nismod.github.io/snail/
MIT License
9 stars 1 forks source link

Python prototype for polygon/raster intersection #25

Closed tlestang closed 2 years ago

tlestang commented 2 years ago

SIngle Python script in scripts/poly2raster. Test case is polygon_scaled

Exterior ring in blue, red dots indicate crossings on exterior ring. Green lines are inner lines, inner crossings are not represented.

Steps:

  1. Split exterior ring. This gives a list of splits for the exterior ring (blue splits) but also crossings on the exterior ring (exterior_crossings)
  2. Split along horizontal gridlines. For each y level inside bounding box (y = 1,2 and 3 in example), split segments between two crossings that are inside the polygon. This relies on helper split_along_gridlines
  3. Split along vertical gridlines (same as 2)
  4. Concatenate lists of splits (of type LineString) and build split polygons using shapely.ops.polygonize

Result: Figure_1