Spaces:
Running
on
Zero
Running
on
Zero
Suchinthana
commited on
Commit
·
24a1808
1
Parent(s):
c1450eb
Simple polygon conversion
Browse files
app.py
CHANGED
@@ -10,6 +10,7 @@ from staticmap import StaticMap, CircleMarker, Polygon
|
|
10 |
from diffusers import ControlNetModel, StableDiffusionControlNetInpaintPipeline
|
11 |
import spaces
|
12 |
import logging
|
|
|
13 |
from typing import List, Union
|
14 |
|
15 |
# Set up logging
|
@@ -151,6 +152,36 @@ def generate_geojson(response):
|
|
151 |
|
152 |
return geojson_data
|
153 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
154 |
# Generate static map image
|
155 |
@spaces.GPU
|
156 |
def generate_static_map(geojson_data, invisible=False):
|
@@ -167,6 +198,10 @@ def generate_static_map(geojson_data, invisible=False):
|
|
167 |
for coord in coords:
|
168 |
m.add_marker(CircleMarker((coord[0], coord[1]), '#1C00ff00' if invisible else '#42445A85', 100))
|
169 |
elif geom_type in ["Polygon", "MultiPolygon"]:
|
|
|
|
|
|
|
|
|
170 |
for polygon in coords:
|
171 |
m.add_polygon(Polygon([(c[0], c[1]) for c in polygon], '#1C00ff00' if invisible else '#42445A85', 3))
|
172 |
|
|
|
10 |
from diffusers import ControlNetModel, StableDiffusionControlNetInpaintPipeline
|
11 |
import spaces
|
12 |
import logging
|
13 |
+
import math
|
14 |
from typing import List, Union
|
15 |
|
16 |
# Set up logging
|
|
|
152 |
|
153 |
return geojson_data
|
154 |
|
155 |
+
# Sort coordinates for a simple polygon (Reduce intersection points)
|
156 |
+
def sort_coordinates_for_simple_polygon(geojson):
|
157 |
+
# Extract coordinates from the GeoJSON
|
158 |
+
coordinates = geojson['features'][0]['geometry']['coordinates'][0]
|
159 |
+
|
160 |
+
# Remove the last point if it duplicates the first (GeoJSON convention for polygons)
|
161 |
+
if coordinates[0] == coordinates[-1]:
|
162 |
+
coordinates = coordinates[:-1]
|
163 |
+
|
164 |
+
# Calculate the centroid of the points
|
165 |
+
centroid_x = sum(point[0] for point in coordinates) / len(coordinates)
|
166 |
+
centroid_y = sum(point[1] for point in coordinates) / len(coordinates)
|
167 |
+
|
168 |
+
# Define a function to calculate the angle relative to the centroid
|
169 |
+
def angle_from_centroid(point):
|
170 |
+
dx = point[0] - centroid_x
|
171 |
+
dy = point[1] - centroid_y
|
172 |
+
return math.atan2(dy, dx)
|
173 |
+
|
174 |
+
# Sort points by their angle from the centroid
|
175 |
+
sorted_coordinates = sorted(coordinates, key=angle_from_centroid)
|
176 |
+
|
177 |
+
# Close the polygon by appending the first point to the end
|
178 |
+
sorted_coordinates.append(sorted_coordinates[0])
|
179 |
+
|
180 |
+
# Update the GeoJSON with sorted coordinates
|
181 |
+
geojson['features'][0]['geometry']['coordinates'][0] = sorted_coordinates
|
182 |
+
|
183 |
+
return geojson
|
184 |
+
|
185 |
# Generate static map image
|
186 |
@spaces.GPU
|
187 |
def generate_static_map(geojson_data, invisible=False):
|
|
|
198 |
for coord in coords:
|
199 |
m.add_marker(CircleMarker((coord[0], coord[1]), '#1C00ff00' if invisible else '#42445A85', 100))
|
200 |
elif geom_type in ["Polygon", "MultiPolygon"]:
|
201 |
+
# Sort coordinates for simple polygons
|
202 |
+
if geom_type == "Polygon":
|
203 |
+
feature["geometry"]["coordinates"] = sort_coordinates_for_simple_polygon(geojson_data)
|
204 |
+
coords = feature["geometry"]["coordinates"]
|
205 |
for polygon in coords:
|
206 |
m.add_polygon(Polygon([(c[0], c[1]) for c in polygon], '#1C00ff00' if invisible else '#42445A85', 3))
|
207 |
|