Replies: 1 comment
-
The Built EnvironmentRelated to issue #18 Comprised of:
1. RoadsDownloadThe Roads dataset can be downloaded from Overture Maps
ProcessingVariables:
Stepsa). Identify the divisions within the country that have been affectred by a crisis.
Where the a. This process require a rasterization of the roads to produce a raster of the roads footprint with The rasterization can be done by
2. Electricity GridDownloadThe current known source of electricity grid data (February 14th 2025) is GridFinder
ProcessingVariables
Stepsa). Identify divisions within the country/bounding box that have been affected by the crisis.
Where To get the affected electricity grid: This process require a rasterization of the grid to produce a raster of the grid footprint with The rasterization can be done by
3. BuildingsVariables:
Download buildings from vida which has buildings from google, buildings, and open street map
from osgeo import ogr, osr
import os
def compute_centroids(input_file: str, output_file: str, driver_name: str = "FlatGeobuf"):
"""
Computes centroids of polygons/multipolygons from an input vector dataset
and saves them in a new dataset while preserving attributes.
:param input_file: Path to the input vector file (.fgb, .gpkg, .shp, etc.).
:param output_file: Path to the output file where centroids will be saved.
:param driver_name: OGR driver name (default: "FlatGeobuf"). Can be "GPKG" or "ESRI Shapefile".
"""
dataset = ogr.Open(input_file, 0) # Read-only mode
if not dataset:
raise RuntimeError(f"Could not open {input_file}")
input_layer = dataset.GetLayer()
spatial_ref = input_layer.GetSpatialRef() # Keep original spatial reference
driver = ogr.GetDriverByName(driver_name)
if not driver:
raise RuntimeError(f"OGR driver '{driver_name}' not found")
if os.path.exists(output_file):
driver.DeleteDataSource(output_file)
output_dataset = driver.CreateDataSource(output_file)
output_layer = output_dataset.CreateLayer("centroids", srs=spatial_ref, geom_type=ogr.wkbPoint)
input_layer_def = input_layer.GetLayerDefn()
for i in range(input_layer_def.GetFieldCount()):
output_layer.CreateField(input_layer_def.GetFieldDefn(i))
for feature in input_layer:
geom = feature.GetGeometryRef()
if geom and geom.GetGeometryName() in ["POLYGON", "MULTIPOLYGON"]:
centroid = geom.Centroid()
new_feature = ogr.Feature(output_layer.GetLayerDefn())
new_feature.SetGeometry(centroid)
for i in range(feature.GetFieldCount()):
new_feature.SetField(i, feature.GetField(i))
output_layer.CreateFeature(new_feature)
new_feature = None # Cleanup
# Close datasets
dataset = None
output_dataset = None
if __name__ == "__main__":
compute_centroids("input_building_dataset.fgb", "buildings_centroids.fgb", driver_name="FlatGeobuf") The following ogr2ogr has been previously used: #!/bin/bash
ogr2ogr -sql "SELECT ST_Centroid(geometry) as geometry, fid, area_in_meters, confidence FROM bangladesh_buildings" -dialect sqlite bangladesh_buildings_centroids.fgb bangladesh_buildings.fgb -nln bangladesh_buildings_centroids -overwrite -progress
from osgeo import gdal, ogr, osr
def polygonize_raster(input_raster, output_shapefile):
"""
Converts a raster file into a vector polygon layer.
Parameters:
- input_raster: str, path to the input raster file
- output_shapefile: str, path to the output shapefile
"""
dataset = gdal.Open(input_raster)
if dataset is None:
raise ValueError(f"Could not open raster file: {input_raster}")
band = dataset.GetRasterBand(1)
driver = ogr.GetDriverByName("ESRI Shapefile")
out_ds = driver.CreateDataSource(output_shapefile)
out_layer = out_ds.CreateLayer("polygonized", geom_type=ogr.wkbPolygon)
field = ogr.FieldDefn("DN", ogr.OFTInteger)
out_layer.CreateField(field)
gdal.Polygonize(band, None, out_layer, 0, [], callback=None)
# Cleanup
dataset = None
out_ds = None
polygonize_raster("affected_areas.tif", "affected_areas.gpkg")
ogr2ogr -f GPKG intersection_output.gpkg \
-dialect SQLite \
-sql "SELECT ST_Intersection(a.geometry, b.geometry) AS geometry, a.*, b.* FROM building_centroids a JOIN affected_areas b ON ST_Intersects(a.geometry, b.geometry)" \
building_centroids.gpkg
|
Beta Was this translation helpful? Give feedback.
Uh oh!
There was an error while loading. Please reload this page.
-
Built env. component currently consists of:
4. evaluate (converting building centroids into raster indices and count buildings)
Beta Was this translation helpful? Give feedback.
All reactions