Skip to content

Instantly share code, notes, and snippets.

@GerardoLopez
Created February 23, 2024 12:33
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save GerardoLopez/dc38e0c6f04d2699af488a9303577c61 to your computer and use it in GitHub Desktop.
Save GerardoLopez/dc38e0c6f04d2699af488a9303577c61 to your computer and use it in GitHub Desktop.
Get attributes from features in one layer that intersect features from another layer
from osgeo import ogr
def ogrIntersection(tiles_layer, site_fname):
# Get layer for the site
driver = ogr.GetDriverByName("GeoJSON")
src_GeoJSON = driver.Open(site_fname)
site_layer = src_GeoJSON.GetLayer()
# List with tiles for every feature in site GeoJSON
tiles = []
# Find overlapping features
for feat1 in tiles_layer:
geom1 = feat1.GetGeometryRef()
for feat2 in site_layer:
geom2 = feat2.GetGeometryRef()
if geom2.Intersects(geom1):
# Get field 0 containing the tile indicex
tiles.append(feat1.GetField(0))
return(tiles);
# MODIS tiles KML
fname = '/home/glopez/Projects/TATSSI/data/kmz/modis_sin.kml'
driver = ogr.GetDriverByName('KML')
src_kml = driver.Open(fname)
tiles_layer = src_kml.GetLayer()
# One of the WorldPeatland sites
site_fname = '/data/world_peatlands/src/WorldPeatland/sites/kampar.geojson'
tiles_for_site = ogrIntersection(tiles_layer, site_fname)
print(tiles_for_site)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment