-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathwms_fetcher.py
More file actions
166 lines (135 loc) · 5.27 KB
/
wms_fetcher.py
File metadata and controls
166 lines (135 loc) · 5.27 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
"""
WMS Elevation Fetcher
Fetch elevation raster data from WMS services and extract bounding boxes from shapefiles.
"""
import requests
import xml.etree.ElementTree as ET
import tempfile
import zipfile
from pathlib import Path
def get_wms_layers(wms_url):
"""
Fetch WMS GetCapabilities and return list of available layers.
Returns list of {name, title} dicts.
"""
try:
resp = requests.get(wms_url, params={
'SERVICE': 'WMS',
'REQUEST': 'GetCapabilities',
}, timeout=30)
resp.raise_for_status()
root = ET.fromstring(resp.content)
# Handle WMS namespaces (1.1.1 and 1.3.0)
ns = ''
if root.tag.startswith('{'):
ns = root.tag.split('}')[0] + '}'
layers = []
for layer_elem in root.iter(f'{ns}Layer'):
name_elem = layer_elem.find(f'{ns}Name')
title_elem = layer_elem.find(f'{ns}Title')
if name_elem is not None and name_elem.text:
layers.append({
'name': name_elem.text.strip(),
'title': (title_elem.text.strip() if title_elem is not None and title_elem.text else name_elem.text.strip())
})
return {'success': True, 'layers': layers}
except requests.exceptions.RequestException as e:
return {'success': False, 'error': f'Request failed: {e}'}
except ET.ParseError as e:
return {'success': False, 'error': f'Failed to parse WMS capabilities XML: {e}'}
except Exception as e:
return {'success': False, 'error': str(e)}
def extract_shapefile_bbox(zip_bytes):
"""
Read a shapefile from ZIP bytes and return bounding box + EPSG code.
Returns {success, bbox: (minx, miny, maxx, maxy), epsg_code} or error.
"""
try:
import geopandas as gpd
with tempfile.TemporaryDirectory() as tmpdir:
zip_path = Path(tmpdir) / "boundary.zip"
zip_path.write_bytes(zip_bytes)
with zipfile.ZipFile(zip_path, 'r') as zf:
zf.extractall(tmpdir)
shp_files = list(Path(tmpdir).glob("**/*.shp"))
if not shp_files:
return {'success': False, 'error': 'No .shp file found in the uploaded ZIP'}
gdf = gpd.read_file(shp_files[0])
if gdf.empty:
return {'success': False, 'error': 'Shapefile contains no features'}
bounds = gdf.total_bounds # (minx, miny, maxx, maxy)
bbox = (float(bounds[0]), float(bounds[1]), float(bounds[2]), float(bounds[3]))
epsg_code = None
if gdf.crs is not None:
try:
epsg_code = gdf.crs.to_epsg()
except Exception:
pass
return {
'success': True,
'bbox': bbox,
'epsg_code': epsg_code,
'num_features': len(gdf),
}
except Exception as e:
return {'success': False, 'error': str(e)}
def fetch_wms_elevation_tif(wms_url, layer_name, bbox, crs_epsg):
"""
Fetch a WMS GetMap request as GeoTIFF.
bbox: (minx, miny, maxx, maxy)
crs_epsg: integer EPSG code
Returns {success, tif_bytes} or error.
"""
try:
minx, miny, maxx, maxy = bbox
dx = maxx - minx
dy = maxy - miny
if dx <= 0 or dy <= 0:
return {'success': False, 'error': 'Invalid bounding box dimensions'}
# Calculate pixel dimensions (max 2048 on longest side)
max_dim = 2048
if dx >= dy:
width = max_dim
height = max(1, int(round(max_dim * dy / dx)))
else:
height = max_dim
width = max(1, int(round(max_dim * dx / dy)))
# WMS 1.3.0: EPSG:4326 uses lat/lon axis order (swap)
if crs_epsg == 4326:
bbox_str = f'{miny},{minx},{maxy},{maxx}'
else:
bbox_str = f'{minx},{miny},{maxx},{maxy}'
params = {
'SERVICE': 'WMS',
'VERSION': '1.3.0',
'REQUEST': 'GetMap',
'LAYERS': layer_name,
'CRS': f'EPSG:{crs_epsg}',
'BBOX': bbox_str,
'WIDTH': str(width),
'HEIGHT': str(height),
'FORMAT': 'image/tiff',
'STYLES': '',
}
resp = requests.get(wms_url, params=params, timeout=120)
resp.raise_for_status()
content_type = resp.headers.get('Content-Type', '')
# Check for XML error response
if 'xml' in content_type.lower() or 'html' in content_type.lower():
try:
error_text = resp.text[:500]
return {'success': False, 'error': f'WMS returned error: {error_text}'}
except Exception:
return {'success': False, 'error': 'WMS returned a non-image response'}
if len(resp.content) < 100:
return {'success': False, 'error': 'WMS returned an empty or too-small response'}
return {
'success': True,
'tif_bytes': resp.content,
'width': width,
'height': height,
}
except requests.exceptions.RequestException as e:
return {'success': False, 'error': f'Request failed: {e}'}
except Exception as e:
return {'success': False, 'error': str(e)}