Spaces:
Runtime error
Runtime error
Suchinthana
commited on
Commit
Β·
0376c05
1
Parent(s):
0261894
geocode update
Browse files
app.py
CHANGED
|
@@ -131,13 +131,24 @@ def generate_geojson(response):
|
|
| 131 |
properties = response['output']['feature_representation']['properties']
|
| 132 |
|
| 133 |
coordinates = []
|
| 134 |
-
for city in city_names:
|
| 135 |
-
coord = get_geo_coordinates(city)
|
| 136 |
-
if coord:
|
| 137 |
-
coordinates.append(coord)
|
| 138 |
|
| 139 |
-
|
| 140 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 141 |
|
| 142 |
# Create the GeoJSON object
|
| 143 |
geojson_data = {
|
|
@@ -148,10 +159,10 @@ def generate_geojson(response):
|
|
| 148 |
"properties": properties,
|
| 149 |
"geometry": {
|
| 150 |
"type": feature_type,
|
| 151 |
-
"coordinates": coordinates
|
| 152 |
-
}
|
| 153 |
}
|
| 154 |
-
]
|
| 155 |
}
|
| 156 |
|
| 157 |
# Validate the GeoJSON
|
|
|
|
| 131 |
properties = response['output']['feature_representation']['properties']
|
| 132 |
|
| 133 |
coordinates = []
|
|
|
|
|
|
|
|
|
|
|
|
|
| 134 |
|
| 135 |
+
# Fetch coordinates for cities
|
| 136 |
+
for city in city_names:
|
| 137 |
+
try:
|
| 138 |
+
coord = get_geo_coordinates(city) # Function to fetch city coordinates
|
| 139 |
+
if coord:
|
| 140 |
+
coordinates.append(coord)
|
| 141 |
+
else:
|
| 142 |
+
logger.warning(f"Coordinates not found for city: {city}")
|
| 143 |
+
except Exception as e:
|
| 144 |
+
logger.error(f"Error fetching coordinates for {city}: {e}")
|
| 145 |
+
|
| 146 |
+
if feature_type == "Polygon":
|
| 147 |
+
if len(coordinates) < 3:
|
| 148 |
+
raise ValueError("Polygon requires at least 3 coordinates.")
|
| 149 |
+
# Close the polygon by appending the first point at the end
|
| 150 |
+
coordinates.append(coordinates[0])
|
| 151 |
+
coordinates = [coordinates] # Nest coordinates for Polygon
|
| 152 |
|
| 153 |
# Create the GeoJSON object
|
| 154 |
geojson_data = {
|
|
|
|
| 159 |
"properties": properties,
|
| 160 |
"geometry": {
|
| 161 |
"type": feature_type,
|
| 162 |
+
"coordinates": coordinates,
|
| 163 |
+
},
|
| 164 |
}
|
| 165 |
+
],
|
| 166 |
}
|
| 167 |
|
| 168 |
# Validate the GeoJSON
|