-
Notifications
You must be signed in to change notification settings - Fork 111
Feature/93 wms #94
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Feature/93 wms #94
Changes from all commits
93c6efd
5ccdacb
7c357cd
082df22
92143d9
1b3ddc8
70682c2
d7012e9
dacd8f3
f3b861c
74de690
adc7eb1
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,7 +1,7 @@ | ||
# pylint: disable=unused-argument | ||
"""Provide utility functions""" | ||
from os import path as op | ||
from urllib.parse import urlparse | ||
from urllib.parse import urlparse, parse_qs | ||
|
||
from mercantile import bounds | ||
from pyproj import Proj, transform | ||
|
@@ -30,7 +30,8 @@ def download_tile_tms(tile, imagery, folder, *args): | |
_, image_format = op.splitext(o.path) | ||
r = requests.get(url(tile.split('-'), imagery)) | ||
tile_img = op.join(folder, '{}{}'.format(tile, image_format)) | ||
open(tile_img, 'wb').write(r.content) | ||
with open(tile_img, 'wb')as w: | ||
w.write(r.content) | ||
return tile_img | ||
|
||
def get_tile_tif(tile, imagery, folder, imagery_offset): | ||
|
@@ -86,6 +87,49 @@ def get_tile_tif(tile, imagery, folder, imagery_offset): | |
|
||
return tile_img | ||
|
||
def get_tile_wms(tile, imagery, folder, imagery_offset): | ||
""" | ||
Read a WMS endpoint with query parameters corresponding to a TMS tile | ||
|
||
Converts the tile boundaries to the spatial reference system (SRS) specified | ||
by the WMS query parameter. | ||
""" | ||
# retrieve the necessary parameters from the query string | ||
query_dict = parse_qs(imagery.lower()) | ||
image_format = query_dict['format'][0].split('/')[1] | ||
wms_srs = query_dict['srs'][0] | ||
|
||
# find our tile bounding box | ||
bound = bounds(*[int(t) for t in tile.split('-')]) | ||
p1 = Proj({'init': 'epsg:4326'}) | ||
p2 = Proj({'init': wms_srs}) | ||
|
||
# project the tile bounding box from lat/lng to WMS SRS | ||
tile_ll_proj = transform(p1, p2, bound.west, bound.south) | ||
tile_ur_proj = transform(p1, p2, bound.east, bound.north) | ||
bbox = tile_ll_proj + tile_ur_proj | ||
|
||
# request the image with the transformed bounding box and save | ||
wms_url = imagery.replace('{bbox}', ','.join([str(b) for b in bbox])) | ||
r = requests.get(wms_url) | ||
tile_img = op.join(folder, '{}.{}'.format(tile, image_format)) | ||
with open(tile_img, 'wb') as w: | ||
w.write(r.content) | ||
return tile_img | ||
|
||
|
||
def is_tif(imagery): | ||
"""Determine if an imagery path has a valid tif extension""" | ||
return op.splitext(imagery)[1].lower() in ['.tif', '.tiff', '.vrt'] | ||
|
||
def is_wms(imagery): | ||
"""Determine if an imagery path is a WMS endpoint""" | ||
return '{bbox}' in imagery | ||
|
||
def get_image_function(imagery): | ||
"""Return the correct image downloading function based on the imagery string""" | ||
if is_tif(imagery): | ||
return get_tile_tif | ||
if is_wms(imagery): | ||
return get_tile_wms | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Add something like
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. The current There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. If we don't test for it, shouldn't we at least catch bad imagery? Will You could also put There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. We won't really know if the imagery is bad or not until the downloading function fails There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Okay, sounds good |
||
return download_tile_tms |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,3 +1,3 @@ | ||
astroid>=1.6.0 | ||
isort>=4.2.15 | ||
pylint>=1.8.1 | ||
pylint==1.8.1 |
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -5,7 +5,7 @@ | |
import numpy as np | ||
from PIL import Image | ||
|
||
from label_maker.utils import url, class_match, get_tile_tif | ||
from label_maker.utils import url, class_match, get_tile_tif, get_tile_wms | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Want to update the test for image fetching by testing There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. The function to imagery string matching isn't ideal right now so I'd like it to remain untested for a bit (the tile "getter" functions themselves are the most important things to be reliable) |
||
|
||
class TestUtils(unittest.TestCase): | ||
"""Tests for utility functions""" | ||
|
@@ -85,5 +85,21 @@ def test_get_tile_vrt(self): | |
fixture_tile = Image.open('test/fixtures/{}.jpg'.format(tile)) | ||
self.assertEqual(test_tile, fixture_tile) | ||
|
||
def test_get_tile_wms(self): | ||
"""Test reading of tile from a WMS endpoint""" | ||
tile = '4686-6267-14' | ||
# create tiles directory | ||
dest_folder = 'test' | ||
tiles_dir = op.join(dest_folder, 'tiles') | ||
if not op.isdir(tiles_dir): | ||
makedirs(tiles_dir) | ||
|
||
usgs_url = 'https://basemap.nationalmap.gov/arcgis/services/USGSImageryOnly/MapServer/WMSServer?SERVICE=WMS&REQUEST=GetMap&VERSION=1.1.1&LAYERS=0&STYLES=&FORMAT=image%2Fjpeg&TRANSPARENT=false&HEIGHT=256&WIDTH=256&SRS=EPSG%3A3857&BBOX={bbox}' | ||
|
||
get_tile_wms(tile, usgs_url, tiles_dir, None) | ||
test_tile = Image.open('test/tiles/{}.jpeg'.format(tile)) | ||
fixture_tile = Image.open('test/fixtures/{}.jpeg'.format(tile)) | ||
self.assertEqual(test_tile, fixture_tile) | ||
|
||
if __name__ == '__main__': | ||
unittest.main() |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I'd add another 2-3 inline comments to the code within this function