Spaces:
Sleeping
Sleeping
File size: 7,193 Bytes
4cd6c7b 09d401b f2433b4 09d401b f2433b4 4cd6c7b 09d401b f2433b4 09d401b f2433b4 09d401b 7acace9 09d401b f2433b4 09d401b f2433b4 09d401b f2433b4 09d401b f2433b4 09d401b f2433b4 09d401b f2433b4 |
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 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 |
import streamlit as st
import geopandas as gpd
import pandas as pd
import os
import zipfile
import tempfile
from keplergl import KeplerGl
from shapely.geometry import Polygon, MultiPolygon
import pydeck as pdk
def main():
polygons_zip_path = 'polygons-20240817T212638Z-001.zip'
valid_polygon_gdfs = []
with tempfile.TemporaryDirectory() as tmpdirname:
with zipfile.ZipFile(polygons_zip_path, 'r') as zip_ref:
zip_ref.extractall(tmpdirname)
polygons_dir = os.path.join(tmpdirname, 'polygons')
geojson_files = [f for f in os.listdir(polygons_dir) if f.endswith('.geojson')]
geojson_files = geojson_files[:7]
for file in geojson_files:
try:
gdf = gpd.read_file(os.path.join(polygons_dir, file)).to_crs(epsg=4326)
if gdf.empty:
st.warning(f"Skipping empty file: {file}")
continue
gdf = gdf[gdf.geometry.apply(lambda geom: isinstance(geom, (Polygon, MultiPolygon)))]
if gdf.empty:
st.warning(f"No valid geometries found in file: {file}")
continue
valid_polygon_gdfs.append(gdf)
except Exception as e:
st.error(f"Error processing file {file}: {str(e)}")
continue
if valid_polygon_gdfs:
tab1, tab2 = st.tabs(["Editable Point based map", "Pydeck Heatmap"])
combined_polygon_gdf = gpd.GeoDataFrame(pd.concat(valid_polygon_gdfs, ignore_index=True))
with tab1:
kepler_data = {
"polygons": combined_polygon_gdf
}
kepler_map = KeplerGl(height=600)
kepler_map.add_data(data=kepler_data["polygons"], name="Polygons")
config = {
"version": "v1",
"config": {
"visState": {
"layers": [
{
"id": "polygon-layer",
"type": "geojson",
"config": {
"dataId": "Polygons",
"label": "Polygons",
"color": [255, 0, 0],
"highlightColor": [252, 242, 26, 255],
"columns": {
"geojson": "geometry"
},
"isVisible": True,
"visConfig": {
"opacity": 0.8,
"thickness": 1.5,
"strokeColor": [255, 0, 0],
"colorRange": {
"name": "Custom",
"type": "sequential",
"category": "Custom",
"colors": [
"#000004", "#3b0f70", "#8c2981", "#de4968", "#fe9f6d", "#fcfdbf" # Magma
]
},
"filled": True,
"stroked": True,
"enable3d": False,
"wireframe": False
},
"visualChannels": {
"colorField": {
"name": "class",
"type": "real"
},
"colorScale": "quantile"
}
}
}
]
},
"mapStyle": {
"styleType": "satellite",
"topLayerGroups": {},
"visibleLayerGroups": {
"label": True,
"road": True,
"border": False,
"building": True,
"water": True,
"land": True,
"3d building": False
},
"threeDBuildingColor": [9.665468314072013, 17.18305478057247, 31.1442867897876],
"mapStyles": {}
},
"mapState": {
"bearing": 0,
"dragRotate": True,
"latitude": combined_polygon_gdf.geometry.centroid.y.mean(),
"longitude": combined_polygon_gdf.geometry.centroid.x.mean(),
"pitch": 0,
"zoom": 10
},
"interactionConfig": {
"brush": {
"enabled": True
},
"tooltip": {
"enabled": True,
"fieldsToShow": {
"Polygons": [
{
"name": "class",
"format": None
}
]
}
}
},
"animationConfig": {
"enabled": True
}
}
}
kepler_map = KeplerGl(height=600, config=config)
kepler_map.add_data(data=kepler_data["polygons"], name="Polygons")
kepler_map.save_to_html(file_name="kepler_map.html")
st.components.v1.html(open("kepler_map.html", 'r').read(), height=600)
st.sidebar.title("Summary Stats")
st.sidebar.write(combined_polygon_gdf.describe())
with tab2:
combined_polygon_gdf['centroid'] = combined_polygon_gdf.geometry.centroid
combined_polygon_gdf['longitude'] = combined_polygon_gdf.centroid.x
combined_polygon_gdf['latitude'] = combined_polygon_gdf.centroid.y
data = combined_polygon_gdf[['longitude', 'latitude', 'class']]
heatmap_layer = pdk.Layer(
"HeatmapLayer",
data=data,
get_position='[longitude, latitude]',
get_weight="class",
radius_pixels=50,
)
view_state = pdk.ViewState(
latitude=combined_polygon_gdf.geometry.centroid.y.mean(),
longitude=combined_polygon_gdf.geometry.centroid.x.mean(),
zoom=10,
bearing=0,
pitch=0
)
r = pdk.Deck(
layers=[heatmap_layer],
initial_view_state=view_state,
map_style='mapbox://styles/mapbox/satellite-v9'
)
st.pydeck_chart(r)
else:
st.error("No valid GeoDataFrames could be combined.")
if __name__ == "__main__":
main()
|