예제 #1
0
def test_wmts_rest_only():
    # ServiceMetadata
    wmts = WebMapTileService(SERVICE_URL)
    assert wmts.identification.type == 'OGC WMTS'
    assert wmts.identification.version == '1.0.0'
    assert wmts.identification.title == 'WMTS-Testserver DOP80'
    # Content
    assert sorted(list(wmts.contents)) == ['dop80']
    # RESTful WMTS
    assert wmts.restonly
    resource = wmts.buildTileResource(layer='dop80',
                                      tilematrixset='webmercator',
                                      tilematrix='11',
                                      row='706',
                                      column='1089')
    assert resource == 'http://geoserv.weichand.de/mapproxy/wmts/dop80/webmercator/11/1089/706.png'

    tile = wmts.gettile(layer='dop80',
                        tilematrixset='webmercator',
                        tilematrix='11',
                        row='706',
                        column='1089')
    out = open(scratch_file('bvv_bayern_dop80.png'), 'wb')
    bytes_written = out.write(tile.read())
    out.close()
예제 #2
0
 def wmtsRequest(self, layer='AGRICULTURE'):
               
     self.layer = layer  
     
     ID = 'your ID'
     wmts_url = 'https://services.sentinel-hub.com/ogc/wmts/'+ID
     wmts = WebMapTileService(wmts_url)
     
     self.x, self.y = self.deg2num(self.lat_center, self.lon_center, self.zoom)
     
     self.wmtsOut = wmts.gettile(layer=self.layer,
                                 tilematrixset='PopularWebMercator256',
                                 tilematrix=self.zoom,
                                 row=self.y,
                                 column=self.x,
                                 format="image/png")
     
     self.imgArr = imread(io.BytesIO(wmtsOut.read()))
     
     self.lat_max, self.lon_min = self.num2deg(self.x, self.y, self.zoom)
     self.lat_min, self.lon_max = self.num2deg(self.x+1, self.y+1, self.zoom)
     
     imgurl = image_to_url(image=self.imgArr)
     self.map.add_layer(ImageOverlay(url=imgurl,
                                     bounds=[[self.lat_min, self.lon_min],
                                             [self.lat_max, self.lon_max]]))
예제 #3
0
def test_wmts():
    # Find out what a WMTS has to offer. Service metadata:
    from owslib.wmts import WebMapTileService
    wmts = WebMapTileService(SERVICE_URL)
    assert wmts.identification.type == 'OGC WMTS'
    assert wmts.identification.version == '1.0.0'
    assert wmts.identification.title == 'NASA Global Imagery Browse Services for EOSDIS'
    bytearray(wmts.identification.abstract, 'utf-8')
    bytearray(b'Near real time imagery from multiple NASA instruments')
    assert wmts.identification.keywords == ['World', 'Global']
    # Service Provider:
    assert wmts.provider.name == 'National Aeronautics and Space Administration'
    assert wmts.provider.url == 'https://earthdata.nasa.gov/'
    # Available Layers:
    assert len(wmts.contents.keys()) > 0
    assert sorted(list(wmts.contents))[0] == 'AIRS_All_Sky_Outgoing_Longwave_Radiation_Daily_Day'
    # Fetch a tile (using some defaults):
    tile = wmts.gettile(layer='MODIS_Terra_CorrectedReflectance_TrueColor',
                        tilematrixset='EPSG4326_250m', tilematrix='0',
                        row=0, column=0, format="image/jpeg")
    out = open(scratch_file('nasa_modis_terra_truecolour.jpg'), 'wb')
    bytes_written = out.write(tile.read())
    out.close()
    # Test styles for several layers
    # TODO: fix dict order
    # assert wmts.contents['MLS_SO2_147hPa_Night'].styles == {'default': {'isDefault': True, 'title': 'default'}}
    assert wmts.contents['MLS_SO2_147hPa_Night'].styles['default']['isDefault'] is True
    # assert wmts.contents['MLS_SO2_147hPa_Night'].styles == {'default': {'isDefault': True, 'title': 'default'}}
    assert wmts.contents['MLS_SO2_147hPa_Night'].styles['default']['isDefault'] is True
예제 #4
0
def test_wmts():
    # Find out what a WMTS has to offer. Service metadata:
    from owslib.wmts import WebMapTileService
    wmts = WebMapTileService(SERVICE_URL)
    assert wmts.identification.type == 'OGC WMTS'
    assert wmts.identification.version == '1.0.0'
    assert wmts.identification.title == 'NASA Global Imagery Browse Services for EOSDIS'
    bytearray(wmts.identification.abstract, 'utf-8')
    bytearray(b'Near real time imagery from multiple NASA instruments')
    assert wmts.identification.keywords == ['World', 'Global']
    # Service Provider:
    assert wmts.provider.name == 'National Aeronautics and Space Administration'
    assert wmts.provider.url == 'https://earthdata.nasa.gov/'
    # Available Layers:
    assert len(wmts.contents.keys()) > 0
    assert sorted(list(wmts.contents))[0] == 'AIRS_CO_Total_Column_Day'
    # Fetch a tile (using some defaults):
    tile = wmts.gettile(layer='MODIS_Terra_CorrectedReflectance_TrueColor',
                        tilematrixset='EPSG4326_250m',
                        tilematrix='0',
                        row=0,
                        column=0,
                        format="image/jpeg")
    out = open(scratch_file('nasa_modis_terra_truecolour.jpg'), 'wb')
    bytes_written = out.write(tile.read())
    out.close()
    # Test styles for several layers
    # TODO: fix dict order
    # assert wmts.contents['MLS_SO2_147hPa_Night'].styles == {'default': {'isDefault': True, 'title': 'default'}}
    assert wmts.contents['MLS_SO2_147hPa_Night'].styles['default'][
        'isDefault'] is True
    # assert wmts.contents['MLS_SO2_147hPa_Night'].styles == {'default': {'isDefault': True, 'title': 'default'}}
    assert wmts.contents['MLS_SO2_147hPa_Night'].styles['default'][
        'isDefault'] is True
예제 #5
0
def test_wmts_rest_only():
    # Test a WMTS with REST only
    from owslib.wmts import WebMapTileService
    wmts = WebMapTileService(SERVICE_URL_REST)
    tile = wmts.gettile(layer="bmaporthofoto30cm",
                        tilematrix="10",
                        row=357,
                        column=547)
    assert (tile.info()['Content-Type'] == 'image/jpeg')
예제 #6
0
    def __init__(self,
                 wmts,
                 layer=None,
                 tms=None,
                 tm=None,
                 fmt=None,
                 pixel_size=_PIXEL_SIZE):
        # Web Map Tile Service
        self.wmts = WebMapTileService(wmts)

        if layer is None:
            layer = next(iter(self.wmts.contents))
        self.layer = self.wmts.contents[layer]

        if tms is None:
            tms = next(iter(self.wmts.tilematrixsets))
        self.tms = self.wmts.tilematrixsets[tms]

        if tm is None:
            tm = next(iter(self.tms.tilematrix))
        self.tm = self.tms.tilematrix[tm]

        # CRS
        self.crs = pp.CRS.from_user_input(self.tms.crs)
        self.epsg = ':'.join(self.crs.to_authority())

        self._from_wgs = pp.Transformer.from_crs(_WGS84, self.crs)
        self._to_wgs = pp.Transformer.from_crs(self.crs, _WGS84)

        self.unit = self.crs.coordinate_system.axis_list[0].unit_name
        self.ucf = self.crs.coordinate_system.axis_list[
            0].unit_conversion_factor

        # Tile span(s)
        self.pixel_size = pixel_size
        self.scale = self.tm.scaledenominator
        self.pixel_span = self.scale * self.pixel_size * self.ucf

        self.tile_width = self.tm.tilewidth
        self.tile_height = self.tm.tileheight

        self.tile_span_x = self.tile_width * self.pixel_span
        self.tile_span_y = self.tile_height * self.pixel_span

        # Domain
        self.matrix_width = self.tm.matrixwidth
        self.matrix_height = self.tm.matrixheight

        self.x_min, self.y_max = self.tm.topleftcorner
        self.x_max = self.x_min + self.matrix_width * self.tile_span_x
        self.y_min = self.y_max - self.matrix_height * self.tile_span_y

        # Format
        self.fmt = self.layer.formats[0] if fmt is None else fmt
예제 #7
0
def test_wmts_example_build_tile_request():
    """
    Example for wmts.buildTileRequest
    """
    wmts = WebMapTileService(SERVICE_URL)
    wmts.buildTileRequest(layer='VIIRS_CityLights_2012',
                          tilematrixset='EPSG4326_500m',
                          tilematrix='6',
                          row=4,
                          column=4)
    request = 'SERVICE=WMTS&REQUEST=GetTile&VERSION=1.0.0&\
예제 #8
0
def test_wmts_example_get_title():
    """
    Example for wmts.getTitle
    """
    wmts = WebMapTileService(SERVICE_URL)
    img = wmts.gettile(layer='VIIRS_CityLights_2012',
                       tilematrixset='EPSG4326_500m',
                       tilematrix='6',
                       row=4,
                       column=4)
    out = open('tile.jpg', 'wb')
    bytes_written = out.write(img.read())
    out.close()
예제 #9
0
def get_map(lon_min, lon_max, lat_min, lat_max, zoom=19):
    """
    Get an ESRI World Imagery map of the selected region

    Args:
        lon_min: Minimum longitude (degrees)
        lon_max: Maximum longitude (degrees)
        lat_min: Minimum latitude (degrees)
        lat_max: Maximum latitude (degrees)
        zoom: Zoom level

    Returns:
        np.array: Numpy array which can be plotted with plt.imshow
    """
    upperleft_tile = mercantile.tile(lon_min, lat_max, zoom)
    xmin, ymin = upperleft_tile.x, upperleft_tile.y
    lowerright_tile = mercantile.tile(lon_max, lat_min, zoom)
    xmax, ymax = lowerright_tile.x, lowerright_tile.y

    total_image = np.zeros([256 * (ymax - ymin + 1), 256 * (xmax - xmin + 1), 3], dtype='uint8')

    os.makedirs("tilecache", exist_ok=True)

    tile_min = mercantile.tile(lon_min, lat_min, zoom)
    tile_max = mercantile.tile(lon_max, lat_max, zoom)

    wmts = WebMapTileService("http://server.arcgisonline.com/arcgis/rest/" +
                             "services/World_Imagery/MapServer/WMTS/1.0.0/WMTSCapabilities.xml")

    for x in range(tile_min.x, tile_max.x + 1):
        for y in range(tile_max.y, tile_min.y + 1):
            tilename = os.path.join("tilecache", f"World_Imagery_{zoom}_{x}_{y}.jpg")
            if not os.path.isfile(tilename):
                tile = wmts.gettile(layer="World_Imagery", tilematrix=str(zoom), row=y, column=x)
                out = open(tilename, "wb")
                out.write(tile.read())
                out.close()
            tile_image = imread(tilename)
            total_image[(y - ymin) * 256: (y - ymin + 1) * 256,
                        (x - xmin) * 256: (x - xmin + 1) * 256] = tile_image

    total_llmin = {'lon': mercantile.bounds(xmin, ymax, zoom).west, 'lat': mercantile.bounds(xmin, ymax, zoom).south}
    total_llmax = {'lon': mercantile.bounds(xmax, ymin, zoom).east, 'lat': mercantile.bounds(xmax, ymin, zoom).north}

    pix_xmin = int(round(np.interp(lon_min, [total_llmin['lon'], total_llmax['lon']], [0, total_image.shape[1]])))
    pix_ymin = int(round(np.interp(lat_min, [total_llmin['lat'], total_llmax['lat']], [0, total_image.shape[0]])))
    pix_xmax = int(round(np.interp(lon_max, [total_llmin['lon'], total_llmax['lon']], [0, total_image.shape[1]])))
    pix_ymax = int(round(np.interp(lat_max, [total_llmin['lat'], total_llmax['lat']], [0, total_image.shape[0]])))

    return total_image[total_image.shape[0] - pix_ymax: total_image.shape[0] - pix_ymin, pix_xmin: pix_xmax]
예제 #10
0
def test_wmts_gettile(ows_server):
    wmts = WebMapTileService(url=ows_server.url + "/wmts")

    contents = list(wmts.contents)
    test_layer_name = contents[0]

    tile = wmts.gettile(layer=test_layer_name,
                        tilematrixset='WholeWorld_WebMercator',
                        tilematrix='0',
                        row=0,
                        column=0,
                        format="image/png")

    assert tile
    assert tile.info()['Content-Type'] == 'image/png'
예제 #11
0
파일: utils.py 프로젝트: jmwenda/hypermap
def update_layers_wmts(service):
    """
    Update layers for an OGC_WMTS service.
    """
    wmts = WebMapTileService(service.url)
    layer_names = list(wmts.contents)
    for layer_name in layer_names:
        ows_layer = wmts.contents[layer_name]
        print 'Updating layer %s' % ows_layer.name
        layer, created = Layer.objects.get_or_create(name=ows_layer.name,
                                                     service=service)
        if layer.active:
            layer.title = ows_layer.title
            layer.abstract = ows_layer.abstract
            layer.url = service.url
            layer.page_url = reverse('layer_detail',
                                     kwargs={'layer_id': layer.id})
            bbox = list(ows_layer.boundingBoxWGS84
                        or (-179.0, -89.0, 179.0, 89.0))
            layer.bbox_x0 = bbox[0]
            layer.bbox_y0 = bbox[1]
            layer.bbox_x1 = bbox[2]
            layer.bbox_y1 = bbox[3]
            layer.save()
            # dates
            add_mined_dates(layer)
예제 #12
0
def check_wmts(source, info_msgs, warning_msgs, error_msgs):
    """
    Check WMTS source

    Parameters
    ----------
    source : dict
        Source dictionary
    info_msgs : list
        Good messages
    warning_msgs: list
        Warning messages
    error_msgs: list:
        Error Messages
    """

    try:
        wmts_url = source['properties']['url']
        if not validators.url(wmts_url):
            error_msgs.append("URL validation error: {}".format(wmts_url))
        with warnings.catch_warnings():
            warnings.simplefilter("ignore")
            wmts = WebMapTileService(wmts_url)
    except Exception as e:
        error_msgs.append("Exception: {}".format(str(e)))
예제 #13
0
def get_wmts_cap(result):
    def convert_layer(lyr):
        return {
            "name": lyr,
            "title": wmts[lyr].title,
            "tilematrixsets": ",".join(list(wmts[lyr].tilematrixsetlinks.keys())),
            "imgformats": ",".join(wmts[lyr].formats),
        }

    try:
        url = result["url"]
        md_id = result["mdId"]
        wmts = WebMapTileService(url)
        title = wmts.identification.title
        abstract = wmts.identification.abstract
        keywords = wmts.identification.keywords
        layers = list(wmts.contents)
        result["title"] = title
        result["abstract"] = abstract
        result["layers"] = list(map(convert_layer, layers))
        result["keywords"] = keywords
    except Exception:
        message = f"exception while retrieving WMTS cap for service mdId: {md_id}, url: {url}"
        logging.exception(message)
        
    return result
예제 #14
0
    def request(self, service):

        try:

            if service == 'WMS':
                self.resp['resp'] = WebMapService(
                    "https://data.linz.govt.nz/services;"
                    "key=" + self.key + "/wms/",
                    version='1.1.1')
                return
            if service == 'WMTS':
                self.resp['resp'] = WebMapTileService(
                    "https://data.linz.govt.nz/services;"
                    "key=" + self.key + "/wmts/1.0.0/WMTSCapabilities.xml?"
                    "count=10",
                    version='1.0.0')
                return
            if service == 'WFS':
                self.resp['resp'] = WebFeatureService(
                    "https://data.linz.govt.nz/services;"
                    "key=" + self.key + "/wfs/?"
                    "service=WFS&"
                    "request=GetCapabilities",
                    version='1.1.0')
                return

        except:
            self.resp[
                'err'] = "ERROR: Something went wrong with the request. Timeout? Incorrect API KEY?"
예제 #15
0
파일: wmts_driver.py 프로젝트: ys0129/oirds
def connectToDGTile(config):
    return WebMapTileService(
        "https://evwhs.digitalglobe.com/earthservice/wmtsaccess?connectid=" +
        config['connectid'],
        username=config['uname'],
        password=config['passwd'],
        version='1.1.1')
예제 #16
0
def test_wmts_gettile_wkss(ows_server):
    wmts = WebMapTileService(url=ows_server.url + "/wmts")

    contents = list(wmts.contents)
    test_layer_name = contents[0]

    tile = wmts.gettile(
        layer=test_layer_name,
        tilematrixset="urn:ogc:def:wkss:OGC:1.0:GoogleMapsCompatible",
        tilematrix="0",
        row=0,
        column=0,
        format="image/png",
    )

    assert tile
    assert tile.info()["Content-Type"] == "image/png"
예제 #17
0
def test_wmts_gettile_exception(ows_server):
    wmts = WebMapTileService(url=ows_server.url + "/wmts")

    contents = list(wmts.contents)
    test_layer_name = contents[0]
    try:
        # supplying an unsupported tilematrixset
        wmts.gettile(layer=test_layer_name,
                     tilematrixset='WholeWorld_WebMercatorxxx',
                     tilematrix='0',
                     row=0,
                     column=0,
                     format="image/png")
    except ServiceException as e:
        assert 'Invalid Tile Matrix Set:' in str(e)
    else:
        assert False
예제 #18
0
def test_wmts_server(ows_server):
    # Use owslib to confirm that we have a somewhat compliant WCS service
    wmts = WebMapTileService(url=ows_server.url + "/wmts")

    assert wmts.identification.type == "OGC WMTS"
    assert wmts.identification.version == "1.0.0"

    # Ensure that we have at least some layers available
    contents = list(wmts.contents)
    assert contents
예제 #19
0
파일: app.py 프로젝트: wpk-/amstelkant
    def __init__(self, url: str, layer: str, tile_system: str,
                 tile_level: Union[str, int], format: str) -> None:
        """Creates a tile matrix (WMTS layer at fixed zoom level).

        Args:
            url: The URL to the Web Map Tile Service (WMTS).
            layer: The WMTS layer name.
            tile_system: Name of the tile system (should use the
                Rijksdriehoek coordinate system, or at least use 1-metre
                units).
            tile_level: The zoom level.
            format: The image format for tiles. Usually either
                `image/png` or `image/jpeg`.
        """
        self.wmts = WebMapTileService(url)
        self.layer = layer
        self.tile_system = tile_system
        self.tile_level = str(tile_level)
        self.format = format

        assert layer in self.wmts.contents, \
            'Layer not found.'
        assert tile_system in self.wmts.tilematrixsets, \
            'Tile matrix set not found.'
        assert tile_system in self.wmts.contents[layer].tilematrixsetlinks, \
            'Tile system not found.'
        assert format in self.wmts.contents[layer].formats, \
            'Unsupported format.'

        # See: pdf, pp. 8-9
        # (Top left corner is min x and max y.)
        # Note: Rijksdriehoek metric is in metres. This is relevant
        #   because the scaling parameter has been omitted for this
        #   reason.
        self.matrix = (
            self.wmts.tilematrixsets[tile_system].tilematrix[str(tile_level)])
        self.pixel_span = self.matrix.scaledenominator * 0.28 * 1e-3
        self.span_x = self.matrix.tilewidth * self.pixel_span
        self.span_y = self.matrix.tileheight * self.pixel_span
예제 #20
0
async def check_wmts(source, session):
    """
    Check WMTS source

    Parameters
    ----------
    source : dict
        Source dictionary
    session : ClientSession
        aiohttp ClientSession object

    Returns
    -------
    list:
        Good messages
    list:
        Warning messages
    list:
        Error Messages

    """
    error_msgs = []
    warning_msgs = []
    info_msgs = []

    try:
        wmts_url = source["properties"]["url"]
        headers = get_http_headers(source)

        if not validators.url(wmts_url):
            error_msgs.append("URL validation error: {}".format(wmts_url))

        with warnings.catch_warnings():
            warnings.simplefilter("ignore")
            response = await get_url(wmts_url,
                                     session,
                                     with_text=True,
                                     headers=headers)
            if response.exception is not None:
                error_msgs.append(response.exception)
                return info_msgs, warning_msgs, error_msgs

            xml = response.text
            wmts = WebMapTileService(wmts_url, xml=xml.encode("utf-8"))
            info_msgs.append("Good")
    except Exception as e:
        error_msgs.append("Exception: {}".format(str(e)))

    return info_msgs, warning_msgs, error_msgs
예제 #21
0
def doWmts(url):
    wmts = WebMapTileService(url)

    ## Huom! Katsoin mitä QGIS tallentaa WMTS "sourceen" ja siellä näkyy olevan GetCapaiblities-osoite
    wmtsGetCaps = next(
        (i for i in wmts.getOperationByName('GetCapabilities').methods
         if i['type'] == 'Get'), None)

    return {
        'type':
        'WMTS',
        'contents':
        list(wmts.contents),
        'formatPerLayer':
        dict(map(lambda kv: (kv[0], kv[1].formats), wmts.contents.items())),
        'tileMatrixSetsPerLayer':
        dict(
            map(lambda kv: (kv[0], kv[1].tilematrixsets),
                wmts.contents.items())),
        'url':
        wmtsGetCaps['url'],
        'service':
        wmts
    }
예제 #22
0
    def mundi_wmts(self,
                   dataset: str,
                   version: str = "1.0.0") -> WebMapTileService:
        """
        Get a WebMapTileService instance for this collection

        :param dataset: the target dataset (eg, "L1C" if collection is "Sentinel1"
        :param version: WMTS version (only 1.0.0 is supported)
        :return: a WebMapTileService instance
        """
        if version in ["1.0.0"]:
            return WebMapTileService(self._service_end_point('wmts', dataset),
                                     version)
        else:
            raise MundiException(ErrorMessages.UNSUPPORTED_SERVICE)
예제 #23
0
def test_wmts_tile_caching():
    image_cache = WMTSRasterSource._shared_image_cache
    image_cache.clear()
    assert len(image_cache) == 0

    url = 'https://map1c.vis.earthdata.nasa.gov/wmts-geo/wmts.cgi'
    wmts = WebMapTileService(url)
    layer_name = 'MODIS_Terra_CorrectedReflectance_TrueColor'

    source = WMTSRasterSource(wmts, layer_name)

    gettile_counter = CallCounter(wmts, 'gettile')
    crs = ccrs.PlateCarree()
    extent = (-180, 180, -90, 90)
    resolution = (20, 10)
    with gettile_counter:
        source.fetch_raster(crs, extent, resolution)
    n_tiles = 2
    assert gettile_counter.count == n_tiles, ('Too many tile requests - '
                                              'expected {}, got {}.'.format(
                                                  n_tiles,
                                                  gettile_counter.count)
                                              )
    gc.collect()
    assert len(image_cache) == 1
    assert len(image_cache[wmts]) == 1
    tiles_key = (layer_name, '0')
    assert len(image_cache[wmts][tiles_key]) == n_tiles

    # Second time around we shouldn't request any more tiles so the
    # call count will stay the same.
    with gettile_counter:
        source.fetch_raster(crs, extent, resolution)
    assert gettile_counter.count == n_tiles, ('Too many tile requests - '
                                              'expected {}, got {}.'.format(
                                                  n_tiles,
                                                  gettile_counter.count)
                                              )
    gc.collect()
    assert len(image_cache) == 1
    assert len(image_cache[wmts]) == 1
    tiles_key = (layer_name, '0')
    assert len(image_cache[wmts][tiles_key]) == n_tiles

    # Once there are no live references the weak-ref cache should clear.
    del source, wmts, gettile_counter
    gc.collect()
    assert len(image_cache) == 0
예제 #24
0
def update_layers_wmts(service):
    """
    Update layers for an OGC:WMTS service.
    """
    wmts = WebMapTileService(service.url)
    layer_names = list(wmts.contents)
    for layer_name in layer_names:
        ows_layer = wmts.contents[layer_name]
        print 'Updating layer %s' % ows_layer.name
        layer, created = Layer.objects.get_or_create(name=ows_layer.name, service=service)
        if layer.active:
            links = [['OGC:WMTS', service.url]]
            layer.type = 'OGC:WMTS'
            layer.title = ows_layer.title
            layer.abstract = ows_layer.abstract
            # keywords
            for keyword in ows_layer.keywords:
                layer.keywords.add(keyword)
            layer.url = service.url
            layer.page_url = reverse('layer_detail', kwargs={'layer_id': layer.id})
            links.append([
                'WWW:LINK',
                settings.SITE_URL.rstrip('/') + layer.page_url
            ])
            bbox = list(ows_layer.boundingBoxWGS84 or (-179.0, -89.0, 179.0, 89.0))
            layer.bbox_x0 = bbox[0]
            layer.bbox_y0 = bbox[1]
            layer.bbox_x1 = bbox[2]
            layer.bbox_y1 = bbox[3]
            layer.wkt_geometry = bbox2wktpolygon(bbox)
            layer.xml = create_metadata_record(
                identifier=layer.id_string,
                source=service.url,
                links=links,
                format='OGC:WMS',
                type=layer.csw_type,
                relation=service.id_string,
                title=ows_layer.title,
                alternative=ows_layer.name,
                abstract=layer.abstract,
                keywords=ows_layer.keywords,
                wkt_geometry=layer.wkt_geometry
            )
            layer.anytext = gen_anytext(layer.title, layer.abstract, ows_layer.keywords)
            layer.save()
            # dates
            add_mined_dates(layer)
예제 #25
0
 def get_service_obj(self):
     try:
         if self.service == "wmts":
             self.obj = WebMapTileService(
                 url=None,
                 xml=self.xml,
                 version=self.version,
             )
         elif self.service == "wfs":
             self.obj = WebFeatureService(
                 url=None,
                 xml=self.xml,
                 version=self.version,
             )
     except XMLSyntaxError:
         # most likely the locally stored xml is corrupt
         self.err = "{0}: XMLSyntaxError".format(self.domain)
예제 #26
0
def test_wmts_tile_caching():
    image_cache = WMTSRasterSource._shared_image_cache
    image_cache.clear()
    assert len(image_cache) == 0

    url = 'https://map1c.vis.earthdata.nasa.gov/wmts-geo/wmts.cgi'
    wmts = WebMapTileService(url)
    layer_name = 'MODIS_Terra_CorrectedReflectance_TrueColor'

    source = WMTSRasterSource(wmts, layer_name)

    crs = ccrs.PlateCarree()
    extent = (-180, 180, -90, 90)
    resolution = (20, 10)
    n_tiles = 2
    with mock.patch.object(wmts, 'gettile',
                           wraps=wmts.gettile) as gettile_counter:
        source.fetch_raster(crs, extent, resolution)
    assert gettile_counter.call_count == n_tiles, (
        f'Too many tile requests - expected {n_tiles}, got '
        f'{gettile_counter.call_count}.')
    del gettile_counter
    gc.collect()
    assert len(image_cache) == 1
    assert len(image_cache[wmts]) == 1
    tiles_key = (layer_name, '0')
    assert len(image_cache[wmts][tiles_key]) == n_tiles

    # Second time around we shouldn't request any more tiles.
    with mock.patch.object(wmts, 'gettile',
                           wraps=wmts.gettile) as gettile_counter:
        source.fetch_raster(crs, extent, resolution)
    gettile_counter.assert_not_called()
    del gettile_counter
    gc.collect()
    assert len(image_cache) == 1
    assert len(image_cache[wmts]) == 1
    tiles_key = (layer_name, '0')
    assert len(image_cache[wmts][tiles_key]) == n_tiles

    # Once there are no live references the weak-ref cache should clear.
    del source, wmts
    gc.collect()
    assert len(image_cache) == 0
예제 #27
0
 def loadServiceList(self, service_id: int):
     self.iFace.messageBar().pushMessage('Info: ', 'Please wait loading layers ... ', level=Qgis.Info)
     self.bar.show()
     self.iFace.mainWindow().repaint()
     self.generatedService = WebMapServiceClass(service_id)
     url = self.generatedService.service_url
     if self.generatedService.service_type == ServiceType.WebMapService.value:
         try:
             wms = WebMapService(url)
             self.generatedService.setWebMapService(wms)
         except Exception as e:
             QMessageBox.information(None, "ERROR:", 'Unable to load this service now.' + str(e))
     elif self.generatedService.service_type == ServiceType.WebMapTileService.value:
         try:
             wmts = WebMapTileService(url)
             self.generatedService.setWebMapService(wmts)
         except Exception as e:
             QMessageBox.information(None, "ERROR:", 'Unable to load this service now.' + str(e))
     self.bar.close()
예제 #28
0
def main():
    # URL of NASA GIBS
    URL = 'http://gibs.earthdata.nasa.gov/wmts/epsg4326/best/wmts.cgi'
    wmts = WebMapTileService(URL)

    # Layers for MODIS true color and snow RGB
    layers = [
        'MODIS_Terra_SurfaceReflectance_Bands143',
        'MODIS_Terra_CorrectedReflectance_Bands367'
    ]

    date_str = '2017-12-07'

    # Plot setup
    plot_CRS = ccrs.Mercator()
    geodetic_CRS = ccrs.Geodetic()
    x0, y0 = plot_CRS.transform_point(3.7, 43.9, geodetic_CRS)
    x1, y1 = plot_CRS.transform_point(22.5, 50.8, geodetic_CRS)
    ysize = 8
    xsize = 2 * ysize * (x1 - x0) / (y1 - y0)
    fig = plt.figure(figsize=(xsize, ysize), dpi=100)

    for layer, offset in zip(layers, [0, 0.5]):
        ax = plt.axes([offset, 0, 0.5, 1], projection=plot_CRS)
        ax.set_xlim((x0, x1))
        ax.set_ylim((y0, y1))
        ax.add_wmts(wmts, layer, wmts_kwargs={'time': date_str})
        txt = plt.text(4.7,
                       43.2,
                       wmts[layer].title,
                       fontsize=18,
                       color='wheat',
                       transform=geodetic_CRS)
        txt.set_path_effects(
            [PathEffects.withStroke(linewidth=5, foreground='black')])
        state_boundaries = feat.NaturalEarthFeature(category='cultural',
                                                    name='admin_0_countries',
                                                    scale='10m',
                                                    facecolor='none')
        ax.coastlines(resolution='10m', zorder=1, color='black')
        ax.add_feature(state_boundaries, zorder=1, edgecolor='black')
    plt.show()
예제 #29
0
    def get_data(self, element, ranges, style):
        if WebMapTileService is None:
            raise SkipRendering('WMTS element requires owslib and PIL '
                                'to be installed.')
        tile_source = None
        for url in element.data:
            if isinstance(url, util.basestring):
                try:
                    tile_source = WebMapTileService(url)
                    break
                except:
                    pass
            elif isinstance(url, WebMapTileService):
                tile_source = url
                break

        if tile_source is None:
            raise SkipRendering("No valid tile source URL found in WMTS "
                                "Element, rendering skipped.")
        return (tile_source, element.layer), style, {}
예제 #30
0
def wmts_metadata(wmts_url):
    """Discover what's available"""
    from owslib.wmts import WebMapTileService
    wmts = WebMapTileService(wmts_url)
    
    tilematrixsets = {}
    for identifier, tilematrix in wmts.tilematrixsets.items():
        # Store information of identifier and associated crs
        zooms = [int(key) for key in tilematrix.tilematrix]
        tilematrixsets[identifier] = {
            "crs" : tilematrix.crs,
            "min_zoom" : min(zooms),
            "max_zoom" : max(zooms),
        }
    
    provider_metadata = {}
    for name, variant in wmts.contents.items():
        d = defaultdict(list)

        for attribute in ("formats", "layers"):
            for key in getattr(variant, attribute):
                d[attribute].append(key)

        for style in variant.styles:
            if style is None:
                d["styles"].append("default")
            else:
                d["styles"].append(style)

        for url in variant.resourceURLs:
            d["url"].append(url["template"])
            
        for identifier in variant._tilematrixsets:
            d["tilematrixset"].append(identifier)
        
        d["bboxWGS84"] = variant.boundingBoxWGS84
        provider_metadata[name] = d

    return provider_metadata, tilematrixsets
예제 #31
0
def main_vis():
    # URL of NASA GIBS
    URL = 'http://gibs.earthdata.nasa.gov/wmts/epsg4326/best/wmts.cgi'
    wmts = WebMapTileService(URL)

    # Layers for MODIS true color and snow RGB
    layers = ['MODIS_Terra_SurfaceReflectance_Bands143']
    # 'MODIS_Terra_CorrectedReflectance_Bands367'
    date_str = '2017-12-07'

    # Plot setup
    plot_CRS = ccrs.LambertConformal(central_longitude=13,
                                     central_latitude=46,
                                     standard_parallels=[35])
    geodetic_CRS = ccrs.Geodetic()
    x0, y0 = plot_CRS.transform_point(3.7, 43.9, geodetic_CRS)
    x1, y1 = plot_CRS.transform_point(22.5, 50.8, geodetic_CRS)

    ax = plt.axes(projection=plot_CRS)
    ax.set_xlim((x0, x1))
    ax.set_ylim((y0, y1))
    ax.add_wmts(wmts, layers[0], wmts_kwargs={'time': date_str})
예제 #32
0
    def build_wmts_url(self, api_layer, srv_details, rsc_type="ds_dyn_lyr_srv", mode="complete"):
        """Format the input WMTS URL to fit QGIS criterias.

        Retrieve GetCapabilities from information transmitted by Isogeo API
        to complete URL syntax.
        """
        # local variables
        layer_name = api_layer.get("id")
        layer_title = api_layer.get("titles")[0].get("value", "WMTS Layer")
        wmts_url_getcap = srv_details.get("path")\
                          + "?request=GetCapabilities&service=WMTS"
        geoserver = "geoserver" in wmts_url_getcap
        # basic checks on service url
        try:
            wmts = WebMapTileService(wmts_url_getcap)
        except TypeError as e:
            logger.error("WMTS - OWSLib mixing str and unicode args", e)
            wmts = WebMapTileService(unicode(wmts_url_getcap))
        except ServiceException as e:
            logger.error(e)
            return 0, "WMTS - Bad operation: " + wmts_url_getcap, str(e)
        except HTTPError as e:
            logger.error(e)
            return 0, "WMTS - Service not reached: " + wmts_url_getcap, e
        except Exception as e:
            logger.error("WMTS - {}: {}".format(wmts_url_getcap, e))
            return 0, "WMTS - Service not reached: " + wmts_url_getcap, e

        # check if GetTile operation is available
        if not hasattr(wmts, "gettile") or "GetTile" not in [op.name for op in wmts.operations]:
            return 0, "Required GetTile operation not available in: " + wmts_url_getcap
        else:
            logger.debug("GetTile available")
            pass

        # check if layer is present and queryable
        try:
            wmts_lyr = wmts[layer_name]
            layer_title = wmts_lyr.title
            layer_id = wmts_lyr.id
        except KeyError as e:
            logger.error("Layer {} not found in WMTS service: {}"
                         .format(layer_name,
                                 wmts_url_getcap))
            return (0,
                    "Layer {} not found in WMS service: {}"
                    .format(layer_name,
                            wmts_url_getcap), e)

        # Tile Matrix Set & SRS
        srs_map = plg_tools.get_map_crs()
        def_tile_matrix_set = wmts_lyr.tilematrixsets[0]
        if srs_map in wmts_lyr.tilematrixsets:
            logger.debug("WMTS - It's a SRS match! With map canvas: " + srs_map)
            tile_matrix_set = wmts.tilematrixsets.get(srs_map).identifier
            srs = srs_map
        elif "EPSG:4326" in wmts_lyr.tilematrixsets:
            logger.debug("WMTS - It's a SRS match! With standard WGS 84 (4326)")
            tile_matrix_set = wmts.tilematrixsets.get("EPSG:4326").identifier
            srs = "EPSG:4326"
        elif "EPSG:900913" in wmts_lyr.tilematrixsets:
            logger.debug("WMTS - It's a SRS match! With Google (900913)")
            tile_matrix_set = wmts.tilematrixsets.get("EPSG:900913").identifier
            srs = "EPSG:900913"
        else:
            logger.debug("WMTS - Searched SRS not available within service CRS.")
            tile_matrix_set = wmts.tilematrixsets.get(def_tile_matrix_set).identifier
            srs = tile_matrix_set

        # Format definition
        wmts_lyr_formats = wmts.getOperationByName('GetTile').formatOptions
        formats_image = [f.split(" ", 1)[0] for f in wmts_lyr_formats
                         if f in qgis_wms_formats]
        if len(formats_image):
            if "image/png" in formats_image:
                layer_format = "image/png"
            elif "image/jpeg" in formats_image:
                layer_format = "image/jpeg"
            else:
                layer_format = formats_image[0]
        else:
            logger.debug("WMTS - No format available among preferred by QGIS.")
            layer_format = "image/png"

        # Style definition
        lyr_style = wmts_lyr.styles.keys()[0]

        # GetTile URL
        wmts_lyr_url = wmts.getOperationByName('GetTile').methods
        wmts_lyr_url = wmts_lyr_url[0].get("url")
        if wmts_lyr_url[-1] == "&":
            wmts_lyr_url = wmts_lyr_url[:-1]
        else:
            pass

        # construct URL
        wmts_url_params = {"SERVICE": "WMTS",
                           "VERSION": "1.0.0",
                           "REQUEST": "GetCapabilities",
                           "layers": layer_id,
                           "crs": srs,
                           "format": layer_format,
                           "styles": "",
                           "tileMatrixSet": tile_matrix_set,
                           "url": wmts_lyr_url,
                           }
        wmts_url_final = unquote(urlencode(wmts_url_params, "utf8"))
        logger.debug(wmts_url_final)

        # method ending
        return ["WMTS", layer_title, wmts_url_final]
예제 #33
0
def process_wmts_service(url, name, type, username, password, wmts=None, owner=None, parent=None):
    """
    Create a new WMTS service
    """
    if wmts is None:
        wmts = WebMapTileService(url)
    try:
        base_url = _clean_url(wmts.getOperationByName("GetTile").methods["Get"]["url"])

        if base_url and base_url != url:
            url = base_url
            wmts = WebMapTileService(base_url)
    except:
        logger.info("Could not retrieve GetMap url, using originally supplied URL %s" % url)
        pass
    try:
        service = Service.objects.get(base_url=url)
        return_dict = [
            {
                "status": "ok",
                "msg": _("This is an existing service"),
                "service_id": service.pk,
                "service_name": service.name,
                "service_title": service.title,
            }
        ]
        return HttpResponse(json.dumps(return_dict), mimetype="application/json", status=200)
    except:
        pass
    title = wmts.identification.title
    if not name:
        if title:
            name = wmts.identification.title
        else:
            name = urlsplit(url).netloc
    for layer, layer_metadata in wmts.contents.items():
        if layer is None or layer_metadata.name is None:
            continue
        logger.info("Registering layer %s" % layer_metadata.name)
        layer_uuid = str(uuid.uuid1())
        try:
            keywords = map(lambda x: x[:100], layer_metadata.keywords)
        except:
            keywords = []
        if not layer_metadata.abstract:
            abstract = ""
        else:
            abstract = layer_metadata.abstract

        bbox = list(layer_metadata.boundingBoxWGS84 or (-179.0, -89.0, 179.0, 89.0))

        # Need to check if layer already exists??
        saved_layer, created = Layer.objects.get_or_create(
            typename=layer_metadata.name,
            service=service,
            defaults=dict(
                name=layer_metadata.name,
                store=service.name,  # ??
                storeType="remoteStore",
                workspace="remoteWorkspace",
                title=layer_metadata.title or layer_metadata.name,
                abstract=layer_metadata.abstract or ("Not provided"),
                uuid=layer_uuid,
                owner=None,
                srid=layer_metadata.tilematrixsets,
                bbox_x0=bbox[0],
                bbox_x1=bbox[2],
                bbox_y0=bbox[1],
                bbox_y1=bbox[3],
            ),
        )
        if created:
            saved_layer.save()
            saved_layer.set_default_permissions()
            saved_layer.keywords.add(*keywords)
            set_attributes(saved_layer)

            service_layer, created = ServiceLayer.objects.get_or_create(typename=layer_metadata.name, service=service)
            service_layer.layer = saved_layer
            service_layer.title = layer_metadata.title
            service_layer.description = layer_metadata.abstract
            service_layer.styles = layer_metadata.styles
            service_layer.save()
    message = "%d Layers Registered" % count
    return_dict = {"status": "ok", "msg": message}
    return HttpResponse(json.dumps(return_dict), mimetype="application/json", status=200)