File size: 9,694 Bytes
840b26e
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
7cf83d7
 
4cd0a2c
 
7cf83d7
4cd0a2c
f5bdb1b
840b26e
 
7cf83d7
4cd0a2c
840b26e
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
4cd0a2c
840b26e
 
 
 
 
 
4cd0a2c
840b26e
4cd0a2c
7cf83d7
f5bdb1b
 
840b26e
7cf83d7
4cd0a2c
f5bdb1b
 
 
4cd0a2c
f5bdb1b
 
 
4cd0a2c
f5bdb1b
 
 
 
 
 
 
840b26e
f5bdb1b
 
 
 
 
 
840b26e
f5bdb1b
 
 
324a2f6
840b26e
f5bdb1b
 
 
840b26e
f5bdb1b
 
 
324a2f6
 
f5bdb1b
4cd0a2c
 
840b26e
 
 
 
 
 
 
 
 
 
 
 
324a2f6
840b26e
 
324a2f6
 
840b26e
324a2f6
 
 
 
 
 
840b26e
324a2f6
f5bdb1b
324a2f6
 
840b26e
324a2f6
 
 
 
840b26e
 
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
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
# import streamlit as st
# import geopandas as gpd
# from shapely.geometry import Point
# import folium
# from streamlit_folium import st_folium
# from geopy.geocoders import Nominatim
# import tempfile
# import os
# import zipfile

# # Configure page
# st.set_page_config(page_title="City Shapefile Generator", layout="wide")
# st.title("🌍 City Location Exporter")

# # Initialize geocoder
# geolocator = Nominatim(user_agent="city_shape_app")

# def get_city_location(city_name):
#     """Get coordinates for a city name"""
#     location = geolocator.geocode(city_name)
#     if location:
#         return location.latitude, location.longitude
#     return None

# def create_geodataframe(lat, lon, city_name):
#     """Create a GeoDataFrame from coordinates"""
#     geometry = Point(lon, lat)
#     return gpd.GeoDataFrame(
#         [[city_name, lat, lon, geometry]],
#         columns=["city", "latitude", "longitude", "geometry"],
#         crs="EPSG:4326"
#     )

# def create_map(lat, lon, city_name):
#     """Create Folium map with marker"""
#     m = folium.Map(location=[lat, lon], zoom_start=12)
#     folium.Marker(
#         [lat, lon],
#         popup=city_name,
#         tooltip=city_name,
#         icon=folium.Icon(color="red", icon="info-sign")
#     ).add_to(m)
#     folium.CircleMarker(
#         [lat, lon],
#         radius=100,
#         color='blue',
#         fill=True,
#         fill_color='blue'
#     ).add_to(m)
#     return m

# def save_to_format(gdf, format_type, temp_dir, city_name):
#     """Save to specific format and return file path"""
#     base_path = os.path.join(temp_dir, f"{city_name}_location")
    
#     if format_type == "SHP":
#         full_path = f"{base_path}.shp"
#         gdf.to_file(full_path)
#         return [f"{base_path}.{ext}" for ext in ["shp", "shx", "dbf", "prj", "cpg"]]
#     elif format_type == "GeoJSON":
#         full_path = f"{base_path}.geojson"
#         gdf.to_file(full_path, driver='GeoJSON')
#         return [full_path]
#     elif format_type == "KML":
#         full_path = f"{base_path}.kml"
#         gdf.to_file(full_path, driver='KML')
#         return [full_path]

# def create_download_button(file_paths, format_type, city_name):
#     """Create a download button for the files"""
#     if format_type == "SHP":
#         zip_path = f"{city_name}_shapefile.zip"
#         with zipfile.ZipFile(zip_path, 'w') as zipf:
#             for file in file_paths:
#                 if os.path.exists(file):
#                     zipf.write(file, os.path.basename(file))
        
#         with open(zip_path, 'rb') as f:
#             return st.download_button(
#                 label=f"Download {format_type}",
#                 data=f,
#                 file_name=zip_path,
#                 mime="application/zip",
#                 key=f"{city_name}_shp"  # Unique key for each button
#             )
#     else:
#         with open(file_paths[0], 'rb') as f:
#             return st.download_button(
#                 label=f"Download {format_type}",
#                 data=f,
#                 file_name=os.path.basename(file_paths[0]),
#                 mime="application/octet-stream",
#                 key=f"{city_name}_{format_type.lower()}"
#             )

# # Main app
# city_name = st.text_input("Enter city name:", "Paris")

# if 'coords' not in st.session_state:
#     st.session_state.coords = None
# if 'city_found' not in st.session_state:
#     st.session_state.city_found = None

# if st.button("Locate City"):
#     with st.spinner("Searching location..."):
#         coords = get_city_location(city_name)
#         st.session_state.coords = coords
#         st.session_state.city_found = city_name

# if st.session_state.coords:
#     lat, lon = st.session_state.coords
#     st.success(f"Found {st.session_state.city_found} at coordinates: {lat:.4f}, {lon:.4f}")
    
#     # Display persistent map
#     m = create_map(lat, lon, st.session_state.city_found)
#     st_folium(m, width=800, height=500)
    
#     # Create GeoDataFrame
#     gdf = create_geodataframe(lat, lon, st.session_state.city_found)
    
#     # Persistent download buttons
#     st.header("Download Options")
#     col1, col2, col3 = st.columns(3)
    
#     with tempfile.TemporaryDirectory() as temp_dir:
#         # Shapefile
#         with col1:
#             shp_files = save_to_format(gdf, "SHP", temp_dir, st.session_state.city_found)
#             create_download_button(shp_files, "SHP", st.session_state.city_found)
        
#         # GeoJSON
#         with col2:
#             geojson_file = save_to_format(gdf, "GeoJSON", temp_dir, st.session_state.city_found)
#             create_download_button(geojson_file, "GeoJSON", st.session_state.city_found)
        
#         # KML
#         with col3:
#             kml_file = save_to_format(gdf, "KML", temp_dir, st.session_state.city_found)
#             create_download_button(kml_file, "KML", st.session_state.city_found)

# elif st.session_state.city_found and not st.session_state.coords:
#     st.error("City not found. Please try a different name.")



import streamlit as st
import geopandas as gpd
import folium
from streamlit_folium import st_folium
import tempfile
import os
import zipfile
import osmnx as ox
from shapely.geometry import Polygon

# Configure page
st.set_page_config(page_title="City Boundary Exporter", layout="wide")
st.title("πŸŒ† City Boundary Exporter")

def get_city_boundary(city_name):
    """Get city boundary polygon from OpenStreetMap"""
    try:
        # Get the boundary geometry
        gdf = ox.geocode_to_gdf(city_name)
        
        # Simplify geometry if too complex
        if gdf.shape[0] > 0:
            gdf['geometry'] = gdf['geometry'].simplify(0.001)
            return gdf
        return None
    except Exception as e:
        st.error(f"Error fetching boundary: {str(e)}")
        return None

def create_map(gdf):
    """Create Folium map with the boundary"""
    centroid = gdf.geometry.centroid.iloc[0]
    m = folium.Map(location=[centroid.y, centroid.x], zoom_start=11)
    
    folium.GeoJson(
        gdf.__geo_interface__,
        style_function=lambda x: {
            'fillColor': '#1f77b4',
            'color': '#1f77b4',
            'weight': 2,
            'fillOpacity': 0.4
        }
    ).add_to(m)
    
    # Add centroid marker
    folium.Marker(
        [centroid.y, centroid.x],
        popup="Centroid",
        icon=folium.Icon(color="red")
    ).add_to(m)
    
    return m

def save_to_format(gdf, format_type, temp_dir, city_name):
    """Save to specific format and return file path"""
    base_path = os.path.join(temp_dir, f"{city_name}_boundary")
    
    if format_type == "SHP":
        full_path = f"{base_path}.shp"
        gdf.to_file(full_path)
        return [f"{base_path}.{ext}" for ext in ["shp", "shx", "dbf", "prj", "cpg"]]
    elif format_type == "GeoJSON":
        full_path = f"{base_path}.geojson"
        gdf.to_file(full_path, driver='GeoJSON')
        return [full_path]
    elif format_type == "KML":
        full_path = f"{base_path}.kml"
        gdf.to_file(full_path, driver='KML')
        return [full_path]

def create_download_button(file_paths, format_type, city_name):
    """Create a download button for the files"""
    if format_type == "SHP":
        zip_path = f"{city_name}_boundary.zip"
        with zipfile.ZipFile(zip_path, 'w') as zipf:
            for file in file_paths:
                if os.path.exists(file):
                    zipf.write(file, os.path.basename(file))
        
        with open(zip_path, 'rb') as f:
            st.download_button(
                label=f"Download {format_type}",
                data=f,
                file_name=zip_path,
                mime="application/zip",
                key=f"{city_name}_shp"
            )
    else:
        with open(file_paths[0], 'rb') as f:
            st.download_button(
                label=f"Download {format_type}",
                data=f,
                file_name=os.path.basename(file_paths[0]),
                mime="application/octet-stream",
                key=f"{city_name}_{format_type.lower()}"
            )

# Main app
city_name = st.text_input("Enter city name (include country if needed):", "Berlin, Germany")

if st.button("Get City Boundary"):
    with st.spinner("Fetching boundary from OpenStreetMap..."):
        boundary_gdf = get_city_boundary(city_name)
        
        if boundary_gdf is not None:
            st.session_state.boundary_gdf = boundary_gdf
            st.session_state.city_found = city_name

if 'boundary_gdf' in st.session_state:
    st.success(f"Found boundary for {st.session_state.city_found}")
    
    # Display map
    m = create_map(st.session_state.boundary_gdf)
    st_folium(m, width=800, height=500)
    
    # Download options
    st.header("Download Options")
    col1, col2, col3 = st.columns(3)
    
    with tempfile.TemporaryDirectory() as temp_dir:
        # Shapefile
        with col1:
            shp_files = save_to_format(st.session_state.boundary_gdf, "SHP", temp_dir, st.session_state.city_found)
            create_download_button(shp_files, "SHP", st.session_state.city_found)
        
        # GeoJSON
        with col2:
            geojson_file = save_to_format(st.session_state.boundary_gdf, "GeoJSON", temp_dir, st.session_state.city_found)
            create_download_button(geojson_file, "GeoJSON", st.session_state.city_found)
        
        # KML
        with col3:
            kml_file = save_to_format(st.session_state.boundary_gdf, "KML", temp_dir, st.session_state.city_found)
            create_download_button(kml_file, "KML", st.session_state.city_found)