joshuasundance commited on
Commit
4f59df9
·
1 Parent(s): 50e151e

Upload to_mongo_4326reproj.py

Browse files
Files changed (1) hide show
  1. to_mongo_4326reproj.py +54 -23
to_mongo_4326reproj.py CHANGED
@@ -47,6 +47,26 @@ def create_uuid(input_str: str) -> str:
47
  )
48
 
49
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
50
  def reproject_to_4326_and_convert_to_geojson(
51
  bbox: dict,
52
  ) -> dict:
@@ -76,27 +96,30 @@ def reproject_to_4326_and_convert_to_geojson(
76
 
77
  src_proj = get_src_proj()
78
  dst_proj = Proj("epsg:4326")
79
- transformer = Transformer.from_proj(src_proj, dst_proj)
80
 
81
  # Extract coordinates
82
  xmin, ymin, xmax, ymax = bbox["xmin"], bbox["ymin"], bbox["xmax"], bbox["ymax"]
83
 
84
  # Transform the coordinates
85
- xmin_trans, ymin_trans = transformer.transform(xmin, ymin)
86
- xmax_trans, ymax_trans = transformer.transform(xmax, ymax)
87
 
88
  # Create a polygon from the transformed coordinates
89
  # Ensure that the polygon is closed by repeating the first point at the end
90
- polygon = Polygon(
91
- [
92
- (xmin_trans, ymin_trans),
93
- (xmax_trans, ymin_trans),
94
- (xmax_trans, ymax_trans),
95
- (xmin_trans, ymax_trans),
96
- (xmin_trans, ymin_trans),
97
- ],
98
- )
99
- # Convert the polygon to GeoJSON format
 
 
 
100
  geojson = {
101
  "type": "Polygon",
102
  "coordinates": [list(polygon.exterior.coords)],
@@ -147,15 +170,23 @@ async def process_metadata(
147
 
148
  processed_md = {k: v for k, v in processed_md.items() if k in keepkeys}
149
 
150
- if (
151
- (extent := processed_md.get("extent")) is not None
152
- and extent.get("spatialReference") is not None
153
- and not any(str(v).lower() in {"nan", "none", "null"} for v in extent.values())
154
- ):
155
- try:
156
- processed_md["extent"] = reproject_to_4326_and_convert_to_geojson(extent)
157
- except (ValueError, GEOSException, CRSError) as e:
158
- print(extent, e)
 
 
 
 
 
 
 
 
159
  return processed_md
160
 
161
 
@@ -222,7 +253,7 @@ async def main() -> None:
222
  r"mongodb://root:example@mongo:27017/",
223
  server_api=ServerApi("1"),
224
  )
225
- db = client["govgis-nov2023"]
226
  layers = db.layers
227
 
228
  # Process each server concurrently
 
47
  )
48
 
49
 
50
+ def validate_coordinate(lon: float, lat: float) -> tuple:
51
+ """
52
+ Validate and adjust the longitude and latitude values to ensure they are within the valid range.
53
+
54
+ Parameters:
55
+ lon (float): Longitude value.
56
+ lat (float): Latitude value.
57
+
58
+ Returns:
59
+ tuple: Validated and potentially adjusted (longitude, latitude) pair.
60
+ """
61
+ # Adjust longitude and latitude values to their valid range
62
+ if not -180 <= lon <= 180:
63
+ lon = min(max(lon, -180), 180)
64
+ if not -90 <= lat <= 90:
65
+ lat = min(max(lat, -90), 90)
66
+
67
+ return lon, lat
68
+
69
+
70
  def reproject_to_4326_and_convert_to_geojson(
71
  bbox: dict,
72
  ) -> dict:
 
96
 
97
  src_proj = get_src_proj()
98
  dst_proj = Proj("epsg:4326")
99
+ transformer = Transformer.from_proj(src_proj, dst_proj, always_xy=True)
100
 
101
  # Extract coordinates
102
  xmin, ymin, xmax, ymax = bbox["xmin"], bbox["ymin"], bbox["xmax"], bbox["ymax"]
103
 
104
  # Transform the coordinates
105
+ xmin_trans, ymin_trans = validate_coordinate(*transformer.transform(xmin, ymin))
106
+ xmax_trans, ymax_trans = validate_coordinate(*transformer.transform(xmax, ymax))
107
 
108
  # Create a polygon from the transformed coordinates
109
  # Ensure that the polygon is closed by repeating the first point at the end
110
+ coords = [
111
+ (xmin_trans, ymin_trans),
112
+ (xmax_trans, ymin_trans),
113
+ (xmax_trans, ymax_trans),
114
+ (xmin_trans, ymax_trans),
115
+ (xmin_trans, ymin_trans),
116
+ ]
117
+
118
+ if len(set(coords)) < 3:
119
+ raise ValueError("invalid extent")
120
+
121
+ polygon = Polygon(coords)
122
+ # # Convert the polygon to GeoJSON format
123
  geojson = {
124
  "type": "Polygon",
125
  "coordinates": [list(polygon.exterior.coords)],
 
170
 
171
  processed_md = {k: v for k, v in processed_md.items() if k in keepkeys}
172
 
173
+ if "extent" in processed_md:
174
+ if (
175
+ (extent := processed_md.get("extent")) is not None
176
+ and extent.get("spatialReference") is not None
177
+ and not any(
178
+ str(v).lower() in {"nan", "none", "null"} for v in extent.values()
179
+ )
180
+ ):
181
+ try:
182
+ processed_md["extent"] = reproject_to_4326_and_convert_to_geojson(
183
+ extent,
184
+ )
185
+ except (ValueError, GEOSException, CRSError):
186
+ del processed_md["extent"]
187
+ # print(extent, e)
188
+ else:
189
+ del processed_md["extent"]
190
  return processed_md
191
 
192
 
 
253
  r"mongodb://root:example@mongo:27017/",
254
  server_api=ServerApi("1"),
255
  )
256
+ db = client["govgis-nov2023-slim-spatial"]
257
  layers = db.layers
258
 
259
  # Process each server concurrently