-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathopen_buildings.py
44 lines (38 loc) · 2.14 KB
/
open_buildings.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
import ee
import geemap
import pandas as pd
import geopandas as gpd
from shapely.geometry import Polygon, MultiPolygon
from .layer import Layer
class OpenBuildings(Layer):
def __init__(self, country='USA', **kwargs):
super().__init__(**kwargs)
self.country = country
def get_data(self, bbox):
dataset = ee.FeatureCollection(f"projects/sat-io/open-datasets/VIDA_COMBINED/{self.country}")
open_buildings = dataset.filterBounds(ee.Geometry.BBox(*bbox))
openbuilds = geemap.ee_to_gdf(open_buildings).reset_index()
# filter out geom_type GeometryCollection
gc_openbuilds = openbuilds[openbuilds.geom_type == 'GeometryCollection']
if len(gc_openbuilds) > 0:
# select Polygons and Multipolygons from GeometryCollection
gc_openbuilds['geometries'] = gc_openbuilds.apply(lambda x: [g for g in x.geometry.geoms], axis=1)
gc_openbuilds_polygon = []
# iterate over each row in gc_openbuilds
for index, row in gc_openbuilds.iterrows():
for geom in row['geometries']:
# Check if the geometry is a Polygon or MultiPolygon
if isinstance(geom, Polygon) or isinstance(geom, MultiPolygon):
# Create a new row with the same attributes as the original row, but with the Polygon geometry
new_row = row.drop(['geometry', 'geometries'])
new_row['geometry'] = geom
gc_openbuilds_polygon.append(new_row)
if len(gc_openbuilds_polygon) > 0:
# convert list to geodataframe
gc_openbuilds_polygon = gpd.GeoDataFrame(gc_openbuilds_polygon, geometry='geometry')
# replace GeometryCollection with Polygon, merge back to openbuilds
openbuilds = openbuilds[openbuilds.geom_type != 'GeometryCollection']
openbuilds = pd.concat([openbuilds, gc_openbuilds_polygon], ignore_index=True).reset_index()
else:
openbuilds = openbuilds[openbuilds.geom_type != 'GeometryCollection'].reset_index()
return openbuilds