Suchinthana commited on
Commit
0261894
·
1 Parent(s): c6fc1b3

Validation update

Browse files
Files changed (1) hide show
  1. app.py +7 -7
app.py CHANGED
@@ -24,7 +24,7 @@ geolocator = Nominatim(user_agent="geoapi")
24
  # Define Pydantic models for GeoJSON validation
25
  class Geometry(BaseModel):
26
  type: str
27
- coordinates: Union[List[List[float]], List[List[List[float]]]]
28
 
29
  class Feature(BaseModel):
30
  type: str = "Feature"
@@ -136,9 +136,10 @@ def generate_geojson(response):
136
  if coord:
137
  coordinates.append(coord)
138
 
139
- if feature_type == "Polygon":
140
  coordinates.append(coordinates[0]) # Close the polygon
141
 
 
142
  geojson_data = {
143
  "type": "FeatureCollection",
144
  "features": [
@@ -147,20 +148,19 @@ def generate_geojson(response):
147
  "properties": properties,
148
  "geometry": {
149
  "type": feature_type,
150
- "coordinates": [coordinates] if feature_type == "Polygon" else coordinates
151
  }
152
  }
153
  ]
154
  }
155
 
156
- # Validate the generated GeoJSON using Pydantic
157
  try:
158
  validated_geojson = FeatureCollection(**geojson_data)
159
- logger.info("GeoJSON validation successful.")
160
  return validated_geojson.dict()
161
  except ValidationError as e:
162
- logger.error(f"GeoJSON validation failed: {e}")
163
- raise
164
 
165
  # Generate static map image
166
  @spaces.GPU
 
24
  # Define Pydantic models for GeoJSON validation
25
  class Geometry(BaseModel):
26
  type: str
27
+ coordinates: Union[List[float], List[List[float]]]
28
 
29
  class Feature(BaseModel):
30
  type: str = "Feature"
 
136
  if coord:
137
  coordinates.append(coord)
138
 
139
+ if feature_type == "Polygon" and len(coordinates) > 2:
140
  coordinates.append(coordinates[0]) # Close the polygon
141
 
142
+ # Create the GeoJSON object
143
  geojson_data = {
144
  "type": "FeatureCollection",
145
  "features": [
 
148
  "properties": properties,
149
  "geometry": {
150
  "type": feature_type,
151
+ "coordinates": coordinates if feature_type != "Polygon" else [coordinates],
152
  }
153
  }
154
  ]
155
  }
156
 
157
+ # Validate the GeoJSON
158
  try:
159
  validated_geojson = FeatureCollection(**geojson_data)
 
160
  return validated_geojson.dict()
161
  except ValidationError as e:
162
+ logger.error(f"Invalid GeoJSON data: {e}")
163
+ raise ValueError("Generated GeoJSON is invalid.")
164
 
165
  # Generate static map image
166
  @spaces.GPU