Suchinthana commited on
Commit
d6f2bf8
·
1 Parent(s): 24b7949

removing bounds, adding duplicates

Browse files
Files changed (1) hide show
  1. app.py +5 -54
app.py CHANGED
@@ -113,31 +113,9 @@ def generate_geojson(response):
113
 
114
  # Generate static map image
115
  @spaces.GPU
116
- def generate_static_map(geojson_data, bounds=None):
117
  # Create a static map object with specified dimensions
118
  m = StaticMap(600, 600)
119
-
120
- if bounds:
121
- #log
122
- logging.info(f"Bounds received: {bounds}")
123
- # If bounds are provided, set the center of the map
124
- center_lat = (bounds[0][0] + bounds[1][0]) / 2
125
- center_lng = (bounds[0][1] + bounds[1][1]) / 2
126
- #zoom = 10 # Adjust zoom level as needed
127
- #m.set_center(center_lat, center_lng) #zoom
128
- else:
129
- # Default center and zoom level
130
- center_lat, center_lng = 0, 0
131
- #zoom = 1
132
-
133
- # Check if there are no features to avoid an empty map
134
- if not geojson_data["features"]:
135
- # Add a small invisible marker to prevent rendering error
136
- #log marker
137
- logging.info(f"Adding invisible marker to {bounds[0], bounds[1]}")
138
- m.add_marker(CircleMarker(tuple(bounds[0]), 'blue', 10)) # White marker with size 0 #FFFFFF
139
- m.add_marker(CircleMarker(tuple(bounds[1]), 'blue', 10)) # White marker with size 0
140
- return m.render()
141
 
142
  # Process each feature in the GeoJSON
143
  for feature in geojson_data["features"]:
@@ -145,13 +123,13 @@ def generate_static_map(geojson_data, bounds=None):
145
  coords = feature["geometry"]["coordinates"]
146
 
147
  if geom_type == "Point":
148
- m.add_marker(CircleMarker((coords[0], coords[1]), 'blue', 10))
149
  elif geom_type in ["MultiPoint", "LineString"]:
150
  for coord in coords:
151
- m.add_marker(CircleMarker((coord[0], coord[1]), 'blue', 10))
152
  elif geom_type in ["Polygon", "MultiPolygon"]:
153
  for polygon in coords:
154
- m.add_polygon(Polygon([(c[0], c[1]) for c in polygon], 'blue', 3))
155
 
156
  return m.render() #zoom=10
157
 
@@ -188,27 +166,6 @@ def generate_satellite_image(init_image, mask_image, prompt):
188
  )
189
  return result.images[0]
190
 
191
- def get_bounds(geojson):
192
- coordinates = []
193
- for feature in geojson["features"]:
194
- geom_type = feature["geometry"]["type"]
195
- coords = feature["geometry"]["coordinates"]
196
- if geom_type == "Point":
197
- coordinates.append(coords)
198
- elif geom_type in ["MultiPoint", "LineString"]:
199
- coordinates.extend(coords)
200
- elif geom_type in ["MultiLineString", "Polygon"]:
201
- for part in coords:
202
- coordinates.extend(part)
203
- elif geom_type == "MultiPolygon":
204
- for polygon in coords:
205
- for part in polygon:
206
- coordinates.extend(part)
207
- lats = [coord[1] for coord in coordinates]
208
- lngs = [coord[0] for coord in coordinates]
209
- logging.info(f"Bounds: {min(lats), min(lngs)}, {max(lats), max(lngs)}")
210
- return [[min(lats), min(lngs)], [max(lats), max(lngs)]]
211
-
212
  # Gradio UI
213
  @spaces.GPU
214
  def handle_query(query):
@@ -219,13 +176,7 @@ def handle_query(query):
219
  # Generate the main map image
220
  map_image = generate_static_map(geojson_data)
221
 
222
- # Generate the empty map using the same bounds
223
- bounds = get_bounds(geojson_data)
224
- empty_geojson = {
225
- "type": "FeatureCollection",
226
- "features": [] # Empty map contains no features
227
- }
228
- empty_map_image = generate_static_map(empty_geojson, bounds=bounds) # Empty map with the same bounds
229
 
230
  # Create the mask
231
  difference = np.abs(np.array(map_image.convert("RGB")) - np.array(empty_map_image.convert("RGB")))
 
113
 
114
  # Generate static map image
115
  @spaces.GPU
116
+ def generate_static_map(geojson_data, invisible=False):
117
  # Create a static map object with specified dimensions
118
  m = StaticMap(600, 600)
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
119
 
120
  # Process each feature in the GeoJSON
121
  for feature in geojson_data["features"]:
 
123
  coords = feature["geometry"]["coordinates"]
124
 
125
  if geom_type == "Point":
126
+ m.add_marker(CircleMarker((coords[0], coords[1]), '#1C00ff00' if invisible == True else 'blue', 10))
127
  elif geom_type in ["MultiPoint", "LineString"]:
128
  for coord in coords:
129
+ m.add_marker(CircleMarker((coord[0], coord[1]), '#1C00ff00' if invisible == True else 'blue', 10))
130
  elif geom_type in ["Polygon", "MultiPolygon"]:
131
  for polygon in coords:
132
+ m.add_polygon(Polygon([(c[0], c[1]) for c in polygon], '#1C00ff00' if invisible == True else 'blue', 3))
133
 
134
  return m.render() #zoom=10
135
 
 
166
  )
167
  return result.images[0]
168
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
169
  # Gradio UI
170
  @spaces.GPU
171
  def handle_query(query):
 
176
  # Generate the main map image
177
  map_image = generate_static_map(geojson_data)
178
 
179
+ empty_map_image = generate_static_map(geojson_data, invisible=True) # Empty map with the same bounds
 
 
 
 
 
 
180
 
181
  # Create the mask
182
  difference = np.abs(np.array(map_image.convert("RGB")) - np.array(empty_map_image.convert("RGB")))