def test_wms_getmap(ows_server):
    # Use owslib to confirm that we have a somewhat compliant WMS service
    wms = WebMapService(url=ows_server.url+"/wms", version="1.3.0", timeout=120)

    # Ensure that we have at least some layers available
    contents = list(wms.contents)
    test_layer_name = contents[0]
    test_layer = wms.contents[test_layer_name]

    bbox = test_layer.boundingBoxWGS84

    img = wms.getmap(layers=[test_layer_name],
                     styles=[],
                     srs="EPSG:4326",
                     bbox=pytest.helpers.enclosed_bbox(bbox),
                     size=(150, 150),
                     format="image/png",
                     transparent=True,
                     time=test_layer.timepositions[len(test_layer.timepositions) // 2].strip(),
                     )
    assert img
    assert img.info()['Content-Type'] == 'image/png'

    img = wms.getmap(layers=[test_layer_name],
                     styles=[],
                     srs="EPSG:4326",
                     bbox=pytest.helpers.disjoint_bbox(bbox),
                     size=(150, 150),
                     format="image/png",
                     transparent=True,
                     time=test_layer.timepositions[len(test_layer.timepositions) // 2].strip(),
                     )
    assert img
    assert img.info()['Content-Type'] == 'image/png'
 def check_advertised_wms_layers(self):
     """
     Makes a GetMap request for each layer advertised by WMS service.
     An exception is raised on failure.
     | Check advertised wms layers |
     """
     wms = WebMapService(self._url, version=self._ogc_version)
     for layer in wms.contents.values():
         wms.getmap(
             layers=[layer.name],
             srs=layer.crsOptions[0],
             bbox=layer.boundingBox[0:-1],
             size=(300, 300),
             format=wms.getOperationByName('GetMap').formatOptions[0])
Exemple #3
0
 def check_advertised_wms_layers(self):
     """
     Makes a GetMap request for each layer advertised by WMS service.
     An exception is raised on failure.
     | Check advertised wms layers |
     """
     wms = WebMapService(self._url, version=self._ogc_version)
     for layer in wms.contents.values():
         wms.getmap(
             layers=[layer.name],
             srs=layer.crsOptions[0],
             bbox=layer.boundingBox[0:-1],
             size=(300, 300),
             format=wms.getOperationByName('GetMap').formatOptions[0])
Exemple #4
0
def add_to_axes(ax,fig,crs_df,params_WMS={},zorder=2):
    """
    """
    from owslib.wms import WebMapService
    import io
    import matplotlib.image as mpimg
    xmin, xmax, ymin, ymax = ax.axis()
    bbox_to_wms=(xmin,ymin,xmax,ymax)
    print(bbox_to_wms)
    #calculando tamaño del axes en pixeles
    bbax=ax.get_window_extent().transformed(fig.dpi_scale_trans.inverted())
    size_px=bbax.width * fig.dpi, bbax.height*fig.dpi
    size_px=tuple([int(round(n)) for n in size_px])
    #print(size_px)
    urlWms=params_WMS["url"]
    srsWms=params_WMS["srs"]
    layersWms=  params_WMS["layers"]
    layers_transparent=  params_WMS["transparent"] if "transparent" in params_WMS else True
    layers_format=  params_WMS["format"] if "format" in params_WMS else "image/png"
    wms=WebMapService(urlWms,version="1.1.1")
    #print(list(wms.contents))
    #print(crs_df.upper(),srsWms)
    if crs_df.upper() != srsWms.upper():
        from pyproj import Proj, transform
        print("reproyectando bounds de wms")
        dfProj = Proj(init=crs_df.lower())
        wmsProj = Proj(init=srsWms.lower())
        xmin_WMS,ymin_WMS=transform(dfProj,wmsProj,xmin,ymin)
        xmax_WMS,ymax_WMS=transform(dfProj,wmsProj,xmax,ymax)
        bbox_to_wms=(xmin_WMS,ymin_WMS,xmax_WMS,ymax_WMS)
    img=wms.getmap(layers=layersWms,srs=srsWms,bbox=bbox_to_wms ,format=layers_format,size=size_px,transparent=layers_transparent)
    i=io.BytesIO(img.read())
    i=mpimg.imread(i,format="png")
    ax.imshow(i,extent=[xmin,xmax,ymin,ymax],zorder=zorder)
    def get_wms_image_size(self,layer_name,srs,min_x,min_y,max_x,max_y):
        """
        Get the size of the png image returned for the current service url
        | set service url | ${WMS_URL} |
	    | ${image_size_in_kb} | get wms image size | bathymetry | EPSG:4326 | -112 | 55 | -106 | 71 |
	    | ${greater_than_5kb} | ${image_size_in_kb} > 5 |
	    | Should Be True | ${greater_than_5kb} |
       
        
        returns an integer which is the size of the image in kB
        
        """
        wms = WebMapService(self._url,version=self._ogc_version)

        img = wms.getmap( layers = [layer_name], srs=srs,
                  bbox=(float(min_x),float(min_y),float(max_x),float(max_y)),size=(300,300),format='image/png')

        out = open('test.png','wb')
        out.write(img.read())
        out.close()

        f = open('test.png','rb')
        size = os.path.getsize('test.png') / 1024
        f.close()

        os.remove('test.png')

        return int(math.ceil(size)) 
def test_ncwms2():
    """Test with an ncWMS2 server.
    """
    # Note that this does not exercise the bug in https://github.com/geopython/OWSLib/issues/556
    wms = WebMapService(NCWMS2_URL, version='1.3.0')
    rsp = wms.getmap(
        layers=['f33_thredds/min_temp'],
        styles=['default'],
        srs='CRS:84',
        bbox=(-124.17, 46.02, -123.29, 46.38),
        size=(256, 256),
        format='image/png',
        transparent=True,
        mode='32bit',

    )
    assert type(rsp) is ResponseWrapper
    assert "service=WMS" in wms.request
    assert "version=1.3.0" in wms.request
    assert "request=GetMap" in wms.request
    assert "layers=f33_thredds/min_temp" in wms.request
    assert "styles=default" in wms.request
    assert "crs=CRS%3A84" in wms.request
    assert "width=256" in wms.request
    assert "height=256" in wms.request
    assert "format=image%2Fpng" in wms.request
    assert "transparent=TRUE" in wms.request
def test_getmap_130_national_map():
    """National Map"""
    # TODO: use flaky tests or fix it: https://pypi.python.org/pypi/pytest-ignore-flaky
    url = SERVICE_URL_NATIONAL_MAP
    wms = WebMapService(url, version='1.3.0')
    rsp = wms.getmap(
        layers=['3'],
        styles=['default'],
        srs='CRS:84',
        bbox=(-176.646, 17.7016, -64.8017, 71.2854),
        size=(500, 300),
        format='image/png',
        transparent=True)
    assert type(rsp) is ResponseWrapper
    assert "service=WMS" in wms.request
    assert "version=1.3.0" in wms.request
    assert "request=GetMap" in wms.request
    assert "layers=3" in wms.request
    assert "styles=default" in wms.request
    assert "crs=CRS%3A84" in wms.request
    assert "box=-176.646%2C17.7016%2C-64.8017%2C71.2854" in wms.request
    assert "width=500" in wms.request
    assert "height=300" in wms.request
    assert "format=image%2Fpng" in wms.request
    assert "transparent=TRUE" in wms.request
Exemple #8
0
def test_ncwms2():
    """Test with an ncWMS2 server.
    """
    # Note that this does not exercise the bug in https://github.com/geopython/OWSLib/issues/556
    wms = WebMapService(NCWMS2_URL, version='1.3.0')
    rsp = wms.getmap(
        layers=['f33_thredds/min_temp'],
        styles=['default'],
        srs='CRS:84',
        bbox=(-124.17, 46.02, -123.29, 46.38),
        size=(256, 256),
        format='image/png',
        transparent=True,
        mode='32bit',

    )
    assert type(rsp) is ResponseWrapper
    assert "service=WMS" in wms.request
    assert "version=1.3.0" in wms.request
    assert "request=GetMap" in wms.request
    assert "layers=f33_thredds/min_temp" in wms.request
    assert "styles=default" in wms.request
    assert "crs=CRS%3A84" in wms.request
    assert "width=256" in wms.request
    assert "height=256" in wms.request
    assert "format=image%2Fpng" in wms.request
    assert "transparent=TRUE" in wms.request
Exemple #9
0
def test_wms_getmap_130_service_exception():
    """GetMap 1.3.0 ServiceException for an invalid CRS"""
    wms = WebMapService(SERVICE_URL, version='1.3.0')
    try:
        wms.getmap(
            layers=['nexrad_base_reflect'],
            styles=['default'],
            srs='EPSG:4328',
            bbox=(-126, 24, -66, 50),
            size=(250, 250),
            format='image/jpeg',
            transparent=True)
    except ServiceException as e:
        assert "msWMSLoadGetMapParams(): WMS server error. Invalid CRS given : CRS must be valid for all requested layers." in str(e)  # noqa
    else:
        assert False
def test_wms_getmap_130_service_exception():
    """GetMap 1.3.0 ServiceException for an invalid CRS"""
    wms = WebMapService(SERVICE_URL, version='1.3.0')
    try:
        wms.getmap(
            layers=['nexrad_base_reflect'],
            styles=['default'],
            srs='EPSG:4328',
            bbox=(-126, 24, -66, 50),
            size=(250, 250),
            format='image/jpeg',
            transparent=True)
    except ServiceException as e:
        assert "msWMSLoadGetMapParams(): WMS server error. Invalid CRS given : CRS must be valid for all requested layers." in str(e)  # noqa
    else:
        assert False
    def get_wms_image_size(self, layer_name, srs, min_x, min_y, max_x, max_y):
        """
        Get the size of the png image returned for the current service url
        | set service url | ${WMS_URL} |
        | ${image_size_in_kb} | get wms image size | bathymetry | EPSG:4326 | -112 | 55 | -106 | 71 |
        | ${greater_than_5kb} | ${image_size_in_kb} > 5 |
        | Should Be True | ${greater_than_5kb} |
        returns an integer which is the size of the image in kB

        """
        wms = WebMapService(self._url, version=self._ogc_version)

        img = wms.getmap(layers=[layer_name], srs=srs,
                         bbox=(float(min_x),
                               float(min_y),
                               float(max_x),
                               float(max_y)),
                         size=(300, 300), format='image/png')

        out = open('test.png', 'wb')
        out.write(img.read())
        out.close()

        f = open('test.png', 'rb')
        size = os.path.getsize('test.png') / 1024
        f.close()

        os.remove('test.png')

        return int(math.ceil(size))
Exemple #12
0
def test_getmap_130_national_map():
    """National Map"""
    # TODO: use flaky tests or fix it: https://pypi.python.org/pypi/pytest-ignore-flaky
    url = SERVICE_URL_NATIONAL_MAP
    wms = WebMapService(url, version='1.3.0')
    rsp = wms.getmap(
        layers=['3'],
        styles=['default'],
        srs='CRS:84',
        bbox=(-176.646, 17.7016, -64.8017, 71.2854),
        size=(500, 300),
        format='image/png',
        transparent=True)
    assert type(rsp) is ResponseWrapper
    assert "service=WMS" in wms.request
    assert "version=1.3.0" in wms.request
    assert "request=GetMap" in wms.request
    assert "layers=3" in wms.request
    assert "styles=default" in wms.request
    assert "crs=CRS%3A84" in wms.request
    assert "box=-176.646%2C17.7016%2C-64.8017%2C71.2854" in wms.request
    assert "width=500" in wms.request
    assert "height=300" in wms.request
    assert "format=image%2Fpng" in wms.request
    assert "transparent=TRUE" in wms.request
Exemple #13
0
	def generate_preview_image(self, styles=None):
		wms = WebMapService(self.wms.online_resource)
		img = wms.getmap(layers=[self.name],
			srs='EPSG:4326',
			bbox=self.latlon_bbox,
			size=(300,250), # TODO: Calculate optimum size for preview image at this approx size
			format='image/jpeg',
			transparent=True)
		out = open(('%s.jpg' % (self.name)), 'wb')
		out.write(img.read())
		out.close()
Exemple #14
0
def build_wms_image(bbox):
    url = "%s/wms" % settings.GEOSERVER_BASE_URL
    wms = WebMapService(url, version='1.1.1')
    img = wms.getmap(layers=['haiti'],
                     srs='EPSG:4326',
                     bbox=bbox,
                     size=(1400, 700),
                     bgcolor="#b5d0d0",
                     format='image/jpeg',
                     transparent=True)
    return img
Exemple #15
0
def test_wms_getmap_130():
    """GetMap 1.3.0"""
    wms = WebMapService(SERVICE_URL, version='1.3.0')
    rsp = wms.getmap(layers=['nexrad_base_reflect'],
                     styles=['default'],
                     srs='EPSG:4326',
                     bbox=(-126, 24, -66, 50),
                     size=(250, 250),
                     format='image/jpeg',
                     transparent=True)
    assert type(rsp) is ResponseWrapper
def test_wms_getmap_130():
    """GetMap 1.3.0"""
    wms = WebMapService(SERVICE_URL, version='1.3.0')
    rsp = wms.getmap(
        layers=['nexrad_base_reflect'],
        styles=['default'],
        srs='EPSG:4326',
        bbox=(-126, 24, -66, 50),
        size=(250, 250),
        format='image/jpeg',
        transparent=True)
    assert type(rsp) is ResponseWrapper
Exemple #17
0
def singleDownloadmap(bbox, ImageURL, ContourURL, ImageOutDirectory,
                      ContourOutDirectory):
    ImageWms = WebMapService(ImageURL, version='1.1.1', timeout=2000)
    ContourWms = WebMapService(ContourURL, version='1.1.1', timeout=2000)
    img = ImageWms.getmap(layers=['Actueel_ortho25'],
                          srs='EPSG:4326',
                          bbox=bbox,
                          size=(1024, 1024),
                          format='image/GeoTIFF',
                          transparent=True)
    ContourImg = ContourWms.getmap(layers=['aan'],
                                   srs='EPSG:4326',
                                   bbox=bbox,
                                   size=(4096, 4096),
                                   format='image/png',
                                   transparent=True)
    filename = "{}_{}_{}_{}.tif".format(bbox[0], bbox[1], bbox[2], bbox[3])
    filename2 = "{}_{}_{}_{}.png".format(bbox[0], bbox[1], bbox[2], bbox[3])
    with open(ImageOutDirectory + filename, 'wb') as out:
        out.write(img.read())
    with open(ContourOutDirectory + filename2, 'wb') as out1:
        out1.write(ContourImg.read())
Exemple #18
0
def test_wms_getmap(wms_server):
    # Use owslib to confirm that we have a somewhat compliant WMS service
    wms = WebMapService(url=wms_server.url, version="1.3.0")

    # Ensure that we have at least some layers available
    contents = list(wms.contents)
    test_layer_name = contents[0]
    test_layer = wms.contents[test_layer_name]

    bbox = test_layer.boundingBoxWGS84

    img = wms.getmap(
        layers=[test_layer_name],
        styles=[],
        srs="EPSG:4326",
        bbox=enclosed_bbox(bbox),
        size=(256, 256),
        format="image/png",
        transparent=True,
        time=test_layer.timepositions[len(test_layer.timepositions) //
                                      2].strip(),
    )
    assert img
    assert what("", h=img.read()) == "png"

    img = wms.getmap(
        layers=[test_layer_name],
        styles=[],
        srs="EPSG:4326",
        bbox=disjoint_bbox(bbox),
        size=(256, 256),
        format="image/png",
        transparent=True,
        time=test_layer.timepositions[len(test_layer.timepositions) //
                                      2].strip(),
    )
    assert img
    assert what("", h=img.read()) == "png"
def get_layer(layer_name):

    wms = WebMapService('http://geoserver.gis.irisnetlab.be/geoserver/wfs', version="1.3")

    kml = wms.getmap(
        layers=[layer_name],
        srs="epsg:4326",
        bbox=wms[layer_name].boundingBox[:-1],
        size=(3000, 3000),
        format='kml', 
        transparent=True
    ).read()

    return kml
def test_wms_getmap_111():
    """MESONET GetMap 1.1.1"""
    wms = WebMapService(SERVICE_URL, version='1.1.1')
    assert wms.request == '{}?service=WMS&request=GetCapabilities&version=1.1.1'.format(SERVICE_URL)
    rsp = wms.getmap(
        layers=['nexrad_base_reflect'],
        styles=['default'],
        srs='EPSG:4326',
        bbox=(-126, 24, -66, 50),
        size=(250, 250),
        format='image/jpeg',
        transparent=True)
    import owslib.util
    assert type(rsp) is ResponseWrapper
Exemple #21
0
def test_wms_getmap_111():
    """MESONET GetMap 1.1.1"""
    wms = WebMapService(SERVICE_URL, version='1.1.1')
    assert wms.request == '{}?service=WMS&request=GetCapabilities&version=1.1.1'.format(
        SERVICE_URL)
    rsp = wms.getmap(layers=['nexrad_base_reflect'],
                     styles=['default'],
                     srs='EPSG:4326',
                     bbox=(-126, 24, -66, 50),
                     size=(250, 250),
                     format='image/jpeg',
                     transparent=True)
    import owslib.util
    assert type(rsp) is ResponseWrapper
Exemple #22
0
def request_image(bbox, size, wms_url, wms_layer, wms_srs, wms_version,
                  wms_format, retries):
    """
    Request an image from a WMS.

    Parameters
    ----------
    bbox : list of float
        The coordinates of the bounding box. [xmin, ymin, xmax, ymax]
    size : list of int
        The size of the image to be requested in pixels. [x, y]
    wms_url : str
        The url of the WMS service to use.
    wms_layer : str
        The layer of the WMS service to use.
    wms_srs : str
        The spatial reference system of the WMS data to request.
    wms_version : str
        The image format of the WMS data to request.
    wms_format : str
        The version number of the WMS service.
    retries : int
        Amount of times to retry retrieving an image from the WMS if it
        fails.

    Returns
    -------
    img : (MxNx3) array
        The RGB values of each pixel
    """
    for i in range(retries):
        try:
            wms = WebMapService(wms_url, version=wms_version)
            wms_img = wms.getmap(layers=[wms_layer],
                                 srs=wms_srs,
                                 bbox=bbox,
                                 size=size,
                                 format=wms_format,
                                 transparent=True)
            break
        except ReadTimeout as e:
            if i != retries - 1:
                print("ReadTimeout, trying again..")
            else:
                raise e

    img = mpimg.imread(BytesIO(wms_img.read()), 0)

    return img
Exemple #23
0
def test_wms():
    url = "https://geoportal.cuzk.cz/WMS_ORTOFOTO_PUB/WMService.aspx"
    wms = WMS(url)

    assert wms

    response = wms.getmap(
            size=[800, 800],
            srs="epsg:4326",
            bbox=[15, 50, 15.5, 50.5],
            format="image/jpeg",
            layers=["GR_ORTFOTORGB"]
            )

    assert response
    assert int(response.info()["Content-Length"])
Exemple #24
0
def download_image(tilename,
                   url,
                   layer,
                   outputfile=None,
                   px_size=0.1,
                   timeout=500,
                   verbose=False):

    t0 = time.time()

    (N, E) = parse_tilename(tilename)

    bb = (E, N, E + 1000, N + 1000)

    size_x = int((bb[2] - bb[0]) / px_size)
    size_y = int((bb[3] - bb[1]) / px_size)

    if verbose:
        print('Downloading %s. It might take a while...' % outputfile)
        print(' ')
        print('Bounding box of tile: %s' % str(bb))
        print('Image size: (%s, %s)' % (size_x, size_y))

    ows_version = [int(n) for n in owslib.__version__.split('.')]
    #if ows_version[0] >= 0 and ows_version[1] >= 9:
    wms = WebMapService(url, timeout=timeout)
    #else:
    # use stupid method override hack
    #wms = WebMapServiceTimeOut(url, timeout=timeout)

    img = wms.getmap(layers=[layer],
                     styles=[''],
                     srs='EPSG:25832',
                     bbox=bb,
                     size=(size_x, size_y),
                     format=r'image/jpeg')

    out = open(outputfile, 'wb')
    out.write(img.read())
    out.close()

    t1 = time.time()

    if verbose:
        print('Download took %s s' % str(t1 - t0))

    return outputfile
def main():
	wms = WebMapService('http://maps.nzoss.org.nz/mapserv?template=openlayers&service=wms&request=getCapabilities', version='1.1.0')

	layer_name = 'default'
	layer = wms[layer_name]
	
	img = wms.getmap(
		layers=[layer_name],
		srs=layer.crsOptions[0],
		bbox=(layer.boundingBox[0], layer.boundingBox[1], layer.boundingBox[2], layer.boundingBox[3]),
		size=(500,400),
		format='image/png',
		transparent=False
		)

	with open('nz_oss_wms.png', 'wb') as out:
		out.write(img.read())
def test_wms_multidate_getmap(ows_server):
    # This one will only work with specially prepared test data, sorry.
    wms = WebMapService(url=ows_server.url + "/wms",
                        version="1.3.0",
                        timeout=120)

    img = wms.getmap(
        layers=["ls8_usgs_level1_scene_layer"],
        styles=["ndvi_delta"],
        srs="EPSG:4326",
        bbox=(145.75, -44.2, 148.69, -42.11),
        size=(150, 150),
        format="image/png",
        transparent=True,
        time="2019-01-30,2019-03-03",
    )
    assert img.info()["Content-Type"] == "image/png"
Exemple #27
0
def main():
    wms = WebMapService(
        'http://maps.nzoss.org.nz/mapserv?template=openlayers&service=wms&request=getCapabilities',
        version='1.1.0')

    layer_name = 'default'
    layer = wms[layer_name]

    img = wms.getmap(layers=[layer_name],
                     srs=layer.crsOptions[0],
                     bbox=(layer.boundingBox[0], layer.boundingBox[1],
                           layer.boundingBox[2], layer.boundingBox[3]),
                     size=(500, 400),
                     format='image/png',
                     transparent=False)

    with open('nz_oss_wms.png', 'wb') as out:
        out.write(img.read())
def download_tile(wms_service, layer_name, longitude, latitude,
                  width_in_pixels, dop_resolution, image_format):
    wms = WebMapService(wms_service, version='1.1.1')
    width_in_meters = width_in_pixels / float(dop_resolution)
    width_from_center = width_in_meters / 2.0
    # This creates a rectangular bounding box for every tile
    bounding_box = (longitude - width_from_center,
                    latitude - width_from_center,
                    longitude + width_from_center,
                    latitude + width_from_center)

    return wms.getmap(layers=[layer_name],
                      styles=['default'],
                      srs='EPSG:25832',
                      bbox=bounding_box,
                      size=(width_in_pixels, width_in_pixels),
                      format=image_format,
                      transparent=True)
Exemple #29
0
def wms_xarray(name, url,layer, bbox, layerstyle, layertime, layerformat, layersizex, layersizey):
    logger.info('Loading WMS to xarray')
    y_max = bbox[0]
    x_min = bbox[1]
    y_min = bbox[2]
    x_max = bbox[3]

    #TODO - check image exists before recreating
    infile = name+'.tif'
    outfile = name+'_georef.tif'

    wms = WebMapService(url, version='1.3.0')
    crs = sorted(wms[layer].crsOptions)
    time = wms[layer].timepositions
    for i in time:
        logger.info('WMS layer time: '+str(i))
    for i in crs:
        logger.info('CRS: '+str(i))
    
    output = wms.getmap(layers=[layer],
                styles=[layerstyle],
                srs='EPSG:4326',
                bbox=(x_min, y_min, x_max, y_max),
                size=(layersizex, layersizey),
                format='image/'+layerformat,
                # TODO remove specific time reference
                time=layertime
                )
            
    with open(infile, 'wb') as out:
        out.write(output.read())
        
   
    # TODO write as python rather than system call
    epsg ='EPSG:4326'
    logger.info('gdal_translate -a_srs '+epsg+' -a_ullr '+str(x_min)+' '+str(y_min)+' '+str(x_max)+' '+str(y_max)+' '+infile+' '+outfile)
    #logger.info('gdal_translate','-a_srs', epsg, '-a_ullr', str(x_min), str(y_max), str(x_max), str(y_min), infile, outfile)
    #os.system('gdal_translate -a_srs '+epsg+' -a_ullr '+str(x_min)+' '+str(y_max)+' '+str(x_max)+' '+str(y_min)+' '+infile+' '+outfile)
    subprocess.call(['gdal_translate','-a_srs', epsg, '-a_ullr', str(x_min), str(y_max), str(x_max), str(y_min), infile, outfile])
    logger.info("Background image georeferencing complete") 
    ds = xr.open_rasterio(outfile)
    logger.info("Background image loaded to xarray for plotting "+str(ds.shape))
    
    return(ds)
Exemple #30
0
    def generate(self):

        #self.source['lon'].extend(self.source['lat'])
        #print('-'*30)
        #print(self.source['lon'])
        wms = WebMapService(self.source['serverUrl'],
                            version=self.source['serverVersion'])
        img = wms.getmap(layers=[self.source['layer']],
                         styles=[self.source['palette']],
                         srs='EPSG:4326',
                         bbox=(self.source['lon'][0], self.source['lat'][0],
                               self.source['lon'][1], self.source['lat'][1]),
                         size=(600, 600),
                         format='image/png',
                         transparent=False,
                         time=self.source['time'],
                         logscale=True)
        out = open(self.fullPath, 'wb')
        out.write(img.read())
        out.close()
        #print(img.geturl())
        return self.fullPath
Exemple #31
0
def request_image(bbox, size, wms_url, wms_layer, wms_srs, wms_version,
                  wms_format, retries):

    for i in range(retries):
        try:
            wms = WebMapService(wms_url, version=wms_version)
            wms_img = wms.getmap(layers=[wms_layer],
                                 srs=wms_srs,
                                 bbox=bbox,
                                 size=size,
                                 format=wms_format,
                                 transparent=True)
            break
        except ReadTimeout as e:
            if i != retries - 1:
                print("ReadTimeout, trying again..")
            else:
                raise e

    img = mpimg.imread(BytesIO(wms_img.read()))

    return img
Exemple #32
0
def retrieve_map_owslib(uri, bbox, srs, size, image_format, styles, layers, wms=None):
    """Retrieve image of a map from wms server using owslib."""
    
    print 'Use owslib method'
    if not wms:
        # Get the wms object
        wms = WebMapService(uri)

    # This is important to make sure they have the same length
    if len(styles) != len(layers):
        styles = [''] * len(layers)

    image = wms.getmap(
        layers=layers,
        styles=styles,
        srs=srs,
        bbox=bbox,
        size=size,
        format=image_format,
        transparent=True
    )

    return image
Exemple #33
0
def retrieve_new_map(lat, lon, radius, width, heigth):

    bbox = getBoundingBox(lat, lon, radius)

    wms = WebMapService('http://www.ign.es/wms-inspire/pnoa-ma', version='1.3.0')
    bbox = getBoundingBox(lat, lon, radius)
    print(bbox)
    img = wms.getmap(layers=['OI.OrthoimageCoverage'],
                     styles=['default'],
                     srs='EPSG:4326',
                     bbox=(bbox),
                     size=(width, heigth),
                     format='image/png',
                     transparent=True)

    with open('images/tmp.png', 'wb') as f:
        f.write(img.read())

    ImageUtils.prepareInitialImage(img.read(), width, heigth)
    opencv_image = cv2.imread("images/imageWithDisclaimer.png", 1)

    image = {'bytes': opencv_image, 'bbox': bbox, 'size': (width, heigth)}
    return image
Exemple #34
0
def get_dop(bbox, size):
    from owslib.wms import WebMapService
    wms = WebMapService('http://geodaten.bayern.de/ogc/ogc_dop200_oa.cgi?', version='1.1.1')
    
    img = wms.getmap(
        layers=['adv_dop200c'],
        srs='EPSG:4326', # WGS84
        #srs='EPSG:31468', # GK4
        bbox=bbox,
        size=size,
        format='image/png'
    )
    
    # TODO 
    import cStringIO
    imgIO = cStringIO.StringIO(img.read())
    try:
        img = Image.open(imgIO)
        #img.show()
    except:
        print imgIO.read();   
        raise
    
    return img
Exemple #35
0
def test_wms_multiproduct_getmap(ows_server, multiproduct_name):
    # Use owslib to confirm that we have a somewhat compliant WMS service
    wms = WebMapService(url=ows_server.url + "/wms",
                        version="1.3.0",
                        timeout=120)

    # Run test against dedicated multiproduct
    test_layer = wms.contents[multiproduct_name]

    bbox = test_layer.boundingBoxWGS84

    img = wms.getmap(
        layers=[multiproduct_name],
        styles=[],
        srs="EPSG:3577",
        bbox=pytest.helpers.enclosed_bbox(bbox),
        size=(150, 150),
        format="image/png",
        transparent=True,
        time=test_layer.timepositions[len(test_layer.timepositions) //
                                      2].strip(),
    )
    assert img
    assert img.info()['Content-Type'] == 'image/png'
Exemple #36
0
def dl_wms_highres(shape, fname, res=2000, rgb=True, padding=100):
    bounds = shape.total_bounds

    if rgb:
        url = f'https://service.pdok.nl/hwh/luchtfotorgb/wms/v1_0?&request=GetCapabilities&service=wms'
        lyr_name = 'Actueel_ortho25'
    else:
        url = f'https://service.pdok.nl/hwh/luchtfotocir/wms/v1_0?&request=GetCapabilities&service=wms'
        lyr_name = 'Actueel_ortho25IR'
    wms = WebMapService(url, version='1.3.0')
    data = wms.getmap(layers=[lyr_name],
                      styles=['default'],
                      srs=str(shape.crs),
                      bbox=tuple(bounds),
                      size=(res, res),
                      format='image/png')

    img = Image.open(urllib.request.urlopen(data.geturl())).convert('RGB')
    raster = reshape_as_raster(np.asarray(img))

    transform = rio.transform.from_bounds(bounds[0], bounds[1], bounds[2],
                                          bounds[3], raster.shape[1],
                                          raster.shape[2])

    profile = {
        'driver': 'GTiff',
        'dtype': 'uint8',
        'nodata': None,
        'width': raster.shape[1],
        'height': raster.shape[2],
        'count': raster.shape[0],
        'crs': str(shape.crs),
        'transform': transform
    }
    with rio.open(fname, 'w', **profile) as dst:
        dst.write(raster)
Exemple #37
0
    def __generate_pngL (self, mapName, style, leggend, testo, testop, testol, station, testos=""):
        wms = WebMapService(GenerateImmage.GEOSERVER_WMS, version='1.1.1')
        #print "WMS end connect"
        #print mapName
        # print station, self.STATIONS
        #img = wms.getmap(layers=["mpba:dtm_alps_all", mapName, self.BORDO, station], styles=["fbk_digital_terrain_model_0_4000", style,self.BORDOS,self.STATIONS ], srs='EPSG:32632', bbox=(610167.0,5057545.0,750567.0,5159345.0),size=(1400, 1000),format='image/png',  transparent=False)
        #img = wms.getmap(layers=['mpba:aspect', mapName, self.BORDO, station], styles=["rasterA", style,self.BORDOS,self.STATIONS ], srs='EPSG:32632', bbox=(610167.0,5057545.0,750567.0,5159345.0),size=(1400, 1000),format='image/png',  transparent=False)
        print mapName, station, self.STATIONS , style, self.BORDOS
        img = wms.getmap(layers=[ mapName, self.BORDO, station], styles=[ style,self.BORDOS,self.STATIONS ], srs='EPSG:32632', bbox=(610167.0,5057545.0,750567.0,5159345.0),size=(1400, 1000),format='image/png',  transparent=False)
        #img = wms.getmap(layers=[ self.BORDO, station], styles=[self.BORDOS,self.STATIONS ], srs='EPSG:32632', bbox=(610167.0,5057545.0,750567.0,5159345.0),size=(1400, 1000),format='image/png',  transparent=False)
        leggend = self.getLeggend(leggend, mapName)
        #self.saveLayerAsImage(img, "/geostore/test/img.png")

        #self.saveLayerAsImage(leggend, "/geostore/test/legged.png")

        buffL = StringIO.StringIO()
        buffL.write(leggend.read())
        buffL.seek(0)
        legendPNG = Image.open(buffL)

        buffM= StringIO.StringIO()
        #print img.read()
        buffM.write(img.read())
        buffM.seek(0)
        ofset=200
        mapPNG = Image.new("RGB", (1400, 1100+ofset), "white")

        mapPNG1 = Image.open(buffM)
        mapPNG.paste(mapPNG1, (00, 0+ofset))

        mapPNG.paste(legendPNG, (1200, 50+ofset))#1200,100
        draw = ImageDraw.Draw(mapPNG)
        font = ImageFont.truetype(self.FONT, 36)
        font1 = ImageFont.truetype(self.FONT, 30)
        font2 = ImageFont.truetype(self.FONT, 16)

        testoLF="@PAT-Climatrentino"

        #draw.text((50, 30),"Precipitazione Maggio ",(0,0,0),font=font)
        w, h = draw.textsize(testo,font=font)
        draw.text(((1400-w)/2, 30),testo,(0,0,0),font=font)
        #draw.text((50, 60),"Periodo di riferimento 1961 - 1990",(0,0,0),font=font1)
        w, h = draw.textsize(testop,font=font1)
        draw.text(((1400-w)/2, 80), testop, (0,0,0), font=font1)

        draw.text((1200, 0+ofset), testos, (0,0,0), font=font2)
        draw.text((1200, 30+ofset), testol, (0,0,0), font=font2)


        w, h = draw.textsize(testoLF,font=font1)
        draw.text(((1400-w)/2, 130), testoLF, (0,0,0), font=font1)

        #mapPNG.show()
        #####################################
        #Test Simboli
        img1 = Image.open(os.path.join(GenerateImmage.BASE_DIR, 'PAT.png'))  ### TODO: mettere nei settings
        x,y=img1.size
        div=3
        img1=img1.resize((x/div,y/div))
        #print img1
        mapPNG.paste(img1, (50, 30), mask=img1)

        img2 = Image.open(os.path.join(GenerateImmage.BASE_DIR, 'p.png')) ### TODO: mettere nei settings
        x,y=img2.size
        div=1
        img2=img2.resize((x/div,y/div))
        #print img1
        mapPNG.paste(img2, (1200,30 ) , mask=img2)

        #
        #####################################

        #mapPNG.save("/tmp/combinata1.png")
        return mapPNG
Exemple #38
0
def get_map(
        ogc_server_location: str,
        layers: List,
        bbox: List,
        wms_version: str = settings.OGC_SERVER["default"].get("WMS_VERSION", "1.1.1"),
        mime_type: str = "image/png",
        styles: List = None,
        width: int = 240,
        height: int = 200,
        max_retries: int = 3,
        retry_delay: int = 1,
):
    """
    Function fetching an image from OGC server.
    For the requests to the configured OGC backend (ogc_server_settings.LOCATION) the function tries to generate
    an access_token and attach it to the URL.
    If access_token is not added ant the request is against Geoserver Basic Authentication is used instead.
    If image retrieval fails, function retries to fetch the image max_retries times, waiting
    retry_delay seconds between consecutive requests.

    :param ogc_server_location: OGC server URL
    :param layers: layers which should be fetched from the OGC server
    :param bbox: area's bounding box in format: [west, east, south, north, CRS]
    :param wms_version: WMS version of the query (default: 1.1.1)
    :param mime_type: mime type of the returned image
    :param styles: styles, which OGC server should use for rendering an image
    :param width: width of the returned image
    :param height: height of the returned image
    :param max_retries: maximum number of retries before skipping retrieval
    :param retry_delay: number of seconds waited between retries
    :returns: retrieved image
    """

    ogc_server_settings = OGC_Servers_Handler(settings.OGC_SERVER)["default"]

    if ogc_server_location is not None:
        thumbnail_url = ogc_server_location
    else:
        thumbnail_url = ogc_server_settings.LOCATION

    if thumbnail_url.startswith(ogc_server_settings.PUBLIC_LOCATION):
        thumbnail_url = thumbnail_url.replace(ogc_server_settings.PUBLIC_LOCATION, ogc_server_settings.LOCATION)

    wms_endpoint = ""
    additional_kwargs = {}
    if thumbnail_url == ogc_server_settings.LOCATION:
        # add access token to requests to Geoserver (logic based on the previous implementation)
        username = ogc_server_settings.credentials.username
        user = get_user_model().objects.filter(username=username).first()
        if user:
            access_token = get_or_create_token(user)
            if access_token and not access_token.is_expired():
                additional_kwargs['access_token'] = access_token.token

        # add WMS endpoint to requests to Geoserver
        wms_endpoint = getattr(ogc_server_settings, "WMS_ENDPOINT") or "ows"

    # prepare authorization for WMS service
    headers = {}
    if thumbnail_url.startswith(ogc_server_settings.LOCATION):
        if "access_token" not in additional_kwargs.keys():
            # for the Geoserver backend, use Basic Auth, if access_token is not provided
            _user, _pwd = ogc_server_settings.credentials
            encoded_credentials = base64.b64encode(f"{_user}:{_pwd}".encode("UTF-8")).decode("ascii")
            headers["Authorization"] = f"Basic {encoded_credentials}"
        else:
            headers["Authorization"] = f"Bearer {additional_kwargs['access_token']}"

    wms = WebMapService(
        f"{thumbnail_url}{wms_endpoint}",
        version=wms_version,
        headers=headers)

    image = None
    for retry in range(max_retries):
        try:
            # fetch data
            image = wms.getmap(
                layers=layers,
                styles=styles,
                srs=bbox[-1] if bbox else None,
                bbox=[bbox[0], bbox[2], bbox[1], bbox[3]] if bbox else None,
                size=(width, height),
                format=mime_type,
                transparent=True,
                timeout=getattr(ogc_server_settings, "TIMEOUT", None),
                **additional_kwargs,
            )

            # validate response
            if not image or "ServiceException" in str(image.read()):
                raise ThumbnailError(
                    f"Fetching partial thumbnail from {thumbnail_url} failed with response: {str(image)}"
                )

        except Exception as e:
            if retry + 1 >= max_retries:
                logger.exception(e)
                return

            time.sleep(retry_delay)
            continue
        else:
            break

    return image.read()
Exemple #39
0
from owslib.wms import WebMapService 
wms = WebMapService('http://mesonet.agron.iastate.edu/cgi-bin/wms/nexrad/n0r.cgi', version= '1.1.1') 
print('type: ', wms.identification.type) 
print('version: ', wms.identification.version) 
print('title: ', wms.identification.title) 
print('abstract: ', wms.identification.abstract) 
print('contents: ', list(wms.contents)) 
print('content title: ', wms['nexrad-n0r-900913-m05m'].title) 
print('queryable: ', wms['nexrad-n0r-900913-m05m'].queryable) 
print('opaque: ', wms['nexrad-n0r-900913-m05m'].opaque) 
print('bounding box: ', wms['nexrad-n0r-900913-m05m'].boundingBox) 
print('bounding box wgs84: ', wms['nexrad-n0r-900913-m05m'].boundingBoxWGS84) 
print('crs options: ', wms['nexrad-n0r-900913-m05m'].crsOptions) 
print('styles: ', wms['nexrad-n0r-900913-m05m'].styles) 
print([op.name for op in wms.operations]) 
print('methods: ', wms.getOperationByName('GetMap').methods) 
print('format options: ', wms.getOperationByName('GetMap').formatOptions) 
img = wms.getmap(	layers=['nexrad-n0r-m50m'],
					styles=[''],
					srs='EPSG:4326',
					bbox=(-120,23,-65,40),
					size=(1200,800),
					format='image/jpeg',
					transparent=True
			) 
out = open('WeatherFrontUS.jpg', 'wb')
out.write(img.read())
out.close() 	
print('done')
Exemple #40
0
    def update_thumbnail(self):
        print 'Generating thumbnail for layer id %s' % self.id
        if not self.has_valid_bbox():
            raise ValueError('Extent for this layer is invalid, cannot generate thumbnail')
            return None
        format_error_message = 'This layer does not expose valid formats (png, jpeg) to generate the thumbnail'
        img = None
        if self.service.type == 'OGC_WMS':
            ows = WebMapService(self.service.url)
            op_getmap = ows.getOperationByName('GetMap')
            image_format = 'image/png'
            if image_format not in op_getmap.formatOptions:
                if 'image/jpeg' in op_getmap.formatOptions:
                    image_format = 'image/jpeg'
                else:
                    raise NotImplementedError(format_error_message)
            img = ows.getmap(
                layers=[self.name],
                srs='EPSG:4326',
                bbox=(
                    float(self.bbox_x0),
                    float(self.bbox_y0),
                    float(self.bbox_x1),
                    float(self.bbox_y1)
                ),
                size=(50, 50),
                format=image_format,
                transparent=True
            )
            if 'ogc.se_xml' in img.info()['Content-Type']:
                raise ValueError(img.read())
                img = None
        elif self.service.type == 'OGC_WMTS':

            ows = WebMapTileService(self.service.url)
            ows_layer = ows.contents[self.name]
            image_format = 'image/png'
            if image_format not in ows_layer.formats:
                if 'image/jpeg' in ows_layer.formats:
                    image_format = 'image/jpeg'
                else:
                    raise NotImplementedError(format_error_message)
            img = ows.gettile(
                                layer=self.name,
                                tilematrixset=ows_layer.tilematrixsets[0],
                                tilematrix='0',
                                row='0',
                                column='0',
                                format=image_format
                            )
        elif self.service.type == 'WM':
            ows = WebMapService(self.url, username=settings.WM_USERNAME, password=settings.WM_PASSWORD)
            op_getmap = ows.getOperationByName('GetMap')
            image_format = 'image/png'
            if image_format not in op_getmap.formatOptions:
                if 'image/jpeg' in op_getmap.formatOptions:
                    image_format = 'image/jpeg'
                else:
                    raise NotImplementedError(format_error_message)
            img = ows.getmap(
                layers=[self.name],
                srs='EPSG:4326',
                bbox=(
                    float(self.bbox_x0),
                    float(self.bbox_y0),
                    float(self.bbox_x1),
                    float(self.bbox_y1)
                ),
                size=(50, 50),
                format=image_format,
                transparent=True
            )
            if 'ogc.se_xml' in img.info()['Content-Type']:
                raise ValueError(img.read())
                img = None
        elif self.service.type == 'WARPER':
            ows = WebMapService(self.url)
            op_getmap = ows.getOperationByName('GetMap')
            image_format = 'image/png'
            if image_format not in op_getmap.formatOptions:
                if 'image/jpeg' in op_getmap.formatOptions:
                    image_format = 'image/jpeg'
                else:
                    raise NotImplementedError(format_error_message)
            img = ows.getmap(
                layers=[self.name],
                srs='EPSG:4326',
                bbox=(
                    float(self.bbox_x0),
                    float(self.bbox_y0),
                    float(self.bbox_x1),
                    float(self.bbox_y1)
                ),
                size=(50, 50),
                format=image_format,
                transparent=True
            )
            if 'ogc.se_xml' in img.info()['Content-Type']:
                raise ValueError(img.read())
                img = None
        elif self.service.type == 'ESRI_MapServer':
            try:
                image = None
                arcserver = ArcMapService(self.service.url)
                bbox = '%s, %s, %s, %s' % (
                    float(self.bbox_x0),
                    float(self.bbox_y0),
                    float(self.bbox_x1),
                    float(self.bbox_y1)
                )

                image = arcserver.ExportMap(
                    bbox=bbox,
                    layers='show:' + self.name,
                    transparent='true',
                    dpi='96',
                    format='jpg'
                )
            except Exception, e:
                print e
            name = re.sub('[^\w\-_\. ]', '_', self.name)
            thumbnail_file_name = '%s%s.jpg' % ('/tmp/', name)
            image.save(thumbnail_file_name)
            img = open(thumbnail_file_name, 'r')
            os.remove(thumbnail_file_name)
                           kbl_dir=dir_kbl, kbl_scale=8, resampling="average", fill_nodata=True,
                           mosaic_filename=dem_filename)
else:
    dem = caf.tif2ar(dem_filename)[0]

# OG data
print("{} - Gathering OG data...".format(dt.now()))
for label, layer in zip(["OGP", "OGF", "ODP_1000", "ODF_1000"],
                        ['Overstromingsgevaarkaarten-PLUVIAAL:overstroombaar_gebied_PLU_noCC',
                         'Overstromingsgevaarkaarten-FLUVIAAL:overstroombaar_gebied_FLU_noCC',
                         'Overstromingsgevaarkaarten-PLUVIAAL:waterdiepte_PLU_noCC_T1000',
                         'Overstromingsgevaarkaarten-FLUVIAAL:waterdiepte_FLU_noCC_T1000']):
    og_filename = os.path.join(dir_location, "{}.tif".format(label))
    if not os.path.exists(og_filename):
        wms_og = WMS("http://geoservice.waterinfo.be/OGRK/wms", timeout=120)
        img = wms_og.getmap(layers=[layer], srs=utm, bbox=roi_extent, size=roi_shape, format="image/GeoTiff")
        with open(og_filename, "wb") as out:
            out.write(img.read())
        with rasterio.open(og_filename) as src:
            og_profile = src.profile
            og_layer = src.read(1)
            if "OG" in label:
                og_layer = (og_layer == 1) * 3 + (og_layer == 2) * 2 + (og_layer == 3) * 1
            elif "OD" in label:
                og_layer = (og_layer == 1) * 13 + (og_layer == 2) * 38 + (og_layer == 3) * 75 + (og_layer == 4) * \
                           150 + (og_layer == 5) * 200
        with rasterio.open(og_filename, "w", **og_profile) as dst:
            dst.write(og_layer.astype(og_profile["dtype"]), indexes=1)
    else:
        og_layer = caf.tif2ar(og_filename)[0]
    if label == "OGP":
Exemple #42
0
def getbasemap(project_proj, xmin, xmax, ymin, ymax):
    '''
    Reproject coordinates of the corners of the cropped mesh so that they
    can be relatable to the base map - if a different project is used for
    the the geometry then it can be redefined using the project_proj variable.
    Get the basemap. Here the owslib module will be taken advantage of in
    order to download a high quality basemap for the unstructured gird to
    have geographical context
    '''
    source = osr.SpatialReference()
    source.ImportFromProj4(project_proj)
    
    target = osr.SpatialReference()
    target.ImportFromProj4('+proj=longlat +ellps=WGS84 +datum=WGS84 +no_defs')
    
    transform = osr.CoordinateTransformation(source, target)
    
    pointone = "POINT ("+str(xmin)+" "+str(ymax)+")"
    pointtwo = "POINT ("+str(xmax)+" "+str(ymin)+")"
    
    
    point = ogr.CreateGeometryFromWkt(pointone)
    point.Transform(transform)
    
    outpoint = point.GetPoint()

    
    point2 = ogr.CreateGeometryFromWkt(pointtwo)
    point2.Transform(transform)
    
    outpoint2 = point2.GetPoint()

    print('Bounding coordinates have been reprojected')
    
    ##This is the WMS server that is being used
    wms = WebMapService('http://raster.nationalmap.gov/arcgis/services/Orthoimagery/USGS_EROS_Ortho_NAIP/ImageServer/WMSServer?request=GetCapabilities&service=WMS')
    
    ##Calculate buffers to impose on the base map - tring 35% on y and 65% on each side
    
    #Highest resolution that the WMS server allows for download
    resx = 5000
    resy = 3816
    
    #The buffer ration of the national map
    bufx = 0.75
    bufy = 0.6
    
    mapxmin = outpoint[0] - (outpoint2[0]-outpoint[0])*bufx
    mapxmax = outpoint2[0] + (outpoint2[0]-outpoint[0])*bufx
    
    mapymin = outpoint2[1] - (outpoint[1]-outpoint2[1])*bufy
    mapymax = outpoint[1] + (outpoint[1]-outpoint2[1])*bufy

    mapbounds = open("mapbounds.txt", "w")
    mapbounds.write(str(mapxmin)+','+str(mapxmax)+','+str(mapymin)+','+str(mapymin))
    mapbounds.close()   
    
    overlayx = ((outpoint2[0]-outpoint[0])*bufx)/(mapxmax-mapxmin)*resx
    overlayy = ((outpoint[1]-outpoint2[1])*bufy)/(mapymax-mapymin)*resy
    overlayx = int(overlayx)
    overlayy = int(overlayy)
    
    fgnewsizex = ((outpoint2[0]-outpoint[0]))/(mapxmax-mapxmin)*resx
    fgnewsizex = int(fgnewsizex)
    fgnewsizey = ((outpoint[1]-outpoint2[1]))/(mapymax-mapymin)*resy
    fgnewsizey = int(fgnewsizey)
    
    fgnewsizexy = (fgnewsizex,fgnewsizey)

    
    ##Here the WMS Request parameters are defined including the bounding box.
    img = wms.getmap(   layers=['0'],
                         styles=[],
                         srs='EPSG:4326',
                         bbox=(mapxmin, mapymin, mapxmax, mapymax),
                         size=(resx, resy),
                         format='image/jpeg',
                         transparent=True
                         )
    out = open('./nationalmap.jpg', 'wb')
    out.write(img.read())
    out.close()
    return fgnewsizexy, overlayx, overlayy, mapxmin, mapxmax, mapymin, mapymax
Exemple #43
0
def getTitlePage(feature_info, crdppf_wms, nomcom, commune, pdfconfig, translations):

    temp_path = pkg_resources.resource_filename('crdppf', 'static/public/temp_files/')

    # the dictionnary for the document
    reportInfo = {}
    reportInfo['type'] = '[officiel]'

    # the dictionnary for the parcel
    feature_info['no_EGRID'] = 'to be defined'
    #feature_info['lastUpdate'] = datetime.now()
    feature_info['operator'] = 'F.Voisard - SITN'
    today= datetime.now()

    # Create property highlight sld
    sld = u"""<?xml version="1.0" encoding="UTF-8"?>
<sld:StyledLayerDescriptor version="1.0.0" xmlns="http://www.opengis.net/ogc" xmlns:sld="http://www.opengis.net/sld" xmlns:ogc="http://www.opengis.net/ogc" xmlns:gml="http://www.opengis.net/gml" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://www.opengis.net/sld http://schemas.opengis.net/sld/1.0.0/StyledLayerDescriptor.xsd">
<sld:NamedLayer>
<sld:Name>parcelles</sld:Name>
<sld:UserStyle>
<sld:Name>propertyIsEqualTo</sld:Name>
<sld:Title>propertyIsEqualTo</sld:Title>
<sld:FeatureTypeStyle>
<sld:Rule>
<ogc:Filter>
<ogc:PropertyIsEqualTo>
<ogc:PropertyName>idemai</ogc:PropertyName>
<ogc:Literal>"""
    sld += str(feature_info['idemai'])
    sld += u"""</ogc:Literal>
</ogc:PropertyIsEqualTo>
</ogc:Filter>
<sld:PolygonSymbolizer>
<sld:Stroke>
<sld:CssParameter name="stroke">#ff0000</sld:CssParameter>
<sld:CssParameter name="stroke-opacity">1</sld:CssParameter>
<sld:CssParameter name="stroke-width">5</sld:CssParameter>
</sld:Stroke>
</sld:PolygonSymbolizer>
<sld:TextSymbolizer>
<sld:Label>
<ogc:PropertyName>nummai</ogc:PropertyName>
</sld:Label> 
<sld:Font>
<sld:CssParameter name="font-family">pdfconfig.fontfamily</sld:CssParameter> 
<sld:CssParameter name="font-weight">bold</sld:CssParameter> 
<sld:CssParameter name="font-size">8</sld:CssParameter> 
</sld:Font>
<sld:Fill>
<sld:CssParameter name="fill">#000000</sld:CssParameter> 
</sld:Fill>
</sld:TextSymbolizer>
</sld:Rule>
</sld:FeatureTypeStyle>
</sld:UserStyle>
</sld:NamedLayer>
</sld:StyledLayerDescriptor>"""


    sldfile = open(temp_path+'sld_'+'siteplan'+'.xml', 'w')
    sldfile.write(sld)
    sldfile.close()

    layers = [
        'parcelles',
        'mo22_batiments',
        'mo21_batiments_provisoires',
        'mo23_batiments_projetes',
        'ag1_parcellaire_provisoire',
        'mo9_immeubles',
        'mo5_point_de_detail',
        'mo7_obj_divers_lineaire',
        'mo7_obj_divers_couvert',
        'mo7_obj_divers_piscine',
        'mo7_obj_divers_cordbois',
        'mo4_pfa_1',
        'mo4_pfp_3',
        'mo4_pfp_1_2',
        'la3_limites_communales',
        'mo22_batiments'
    ]

    scale = feature_info['printFormat']['scale']*2
    # SitePlan/Plan de situation/Situationsplan
    map_params = {'width':feature_info['printFormat']['mapWidth'],'height':feature_info['printFormat']['mapHeight']}
    map_params['bboxCenterX'] = (feature_info['BBOX']['maxX']+feature_info['BBOX']['minX'])/2
    map_params['bboxCenterY'] = (feature_info['BBOX']['maxY']+feature_info['BBOX']['minY'])/2
    #to recenter the map on the bbox of the feature, with the right scale and add at least 10% of space we calculate a wmsBBOX
    wmsBBOX = {}
    wmsBBOX['centerY'] =  int(map_params['bboxCenterY'])
    wmsBBOX['centerX'] =  int(map_params['bboxCenterX'])
    wmsBBOX['minX'] = int(wmsBBOX['centerX'] - (160*scale/1000/2))
    wmsBBOX['maxX'] = int(wmsBBOX['centerX']+(160*scale/1000/2))
    wmsBBOX['minY'] = int(wmsBBOX['centerY']-(90*scale/1000/2))
    wmsBBOX['maxY'] = int(wmsBBOX['centerY']+(90*scale/1000/2))

    #wms = WebMapService('http://sitn.ne.ch/mapproxy/service', version='1.1.1')
    wms = WebMapService(crdppf_wms, version='1.1.1')
    #layers = 'plan_ville_c2c'

    map = wms.getmap(
        layers=layers,
        sld = pdfconfig.sld_url + '/sld_' + 'siteplan' + '.xml',
        srs='EPSG:21781',
        bbox=(wmsBBOX['minX'],wmsBBOX['minY'],wmsBBOX['maxX'],wmsBBOX['maxY']),
        size=(1600,900),
        format='image/png',
        transparent=False
    )

    out = open(temp_path + 'siteplan.png', 'wb')
    out.write(map.read())
    out.close()

    mappath = temp_path + 'siteplan.png'

    # Create PDF extract
    pdf = ExtractPDF(commune, pdfconfig, translations) 

    # START TITLEPAGE
    pdf.add_page()
    pdf.set_margins(*pdfconfig.pdfmargins)
    path = pkg_resources.resource_filename('crdppf', 'utils\\')

    # PageTitle
    pdf.set_y(45)
    pdf.set_font(*pdfconfig.textstyles['title1'])
    if reportInfo['type'] =='certified':
        pdf.multi_cell(0, 9, translations["certifiedextracttitlelabel"])
    elif reportInfo['type'] =='reduced':
        pdf.multi_cell(0, 9, translations['reducedextracttitlelabel'])
    elif reportInfo['type'] =='reducedcertified':
        pdf.multi_cell(0, 9, translations['reducedcertifiedextracttitlelabel'])
    else:
        pdf.multi_cell(0, 9, translations['normalextracttitlelabel'])
    pdf.set_font(*pdfconfig.textstyles['title2'])
    #pdf.multi_cell(0, 7, translations['extractsubtitlelabel'])
    pdf.multi_cell(0, 7, translations['extractsubtitlelabel'])
    pdf.ln()
    
    map = pdf.image(temp_path+'siteplan.png', 25, 80, 160, 90)

    y=pdf.get_y()
    pdf.rect(25, 80, 160, 90, '')
    pdf.set_y(y+105)
    # First infoline
    pdf.set_font(*pdfconfig.textstyles['bold'])
    pdf.cell(45, 5, translations['propertylabel'], 0, 0, 'L')

    pdf.set_font(*pdfconfig.textstyles['normal'])
    if feature_info['nomcad'] is not None:
        pdf.cell(50, 5, feature_info['nummai'].encode('iso-8859-1')+str(' (')+feature_info['nomcad'].encode('iso-8859-1')+str(') ')+str(' - ')+feature_info['type'].encode('iso-8859-1'), 0, 1, 'L')
    else : 
        pdf.cell(50, 5, feature_info['nummai'].encode('iso-8859-1'), 0, 1, 'L')
    
     # Second infoline : Area and EGRID
    pdf.set_font(*pdfconfig.textstyles['bold'])
    pdf.cell(45, 5, translations['propertyarealabel'], 0, 0, 'L')
    pdf.set_font(*pdfconfig.textstyles['normal'])
    pdf.cell(50, 5, str(feature_info['area'])+str(' m2').encode('iso-8859-1'), 0, 0, 'L')
    pdf.set_font(*pdfconfig.textstyles['bold'])
    pdf.cell(35, 5, translations['EGRIDlabel'], 0, 0, 'L')
    pdf.set_font(*pdfconfig.textstyles['normal'])
    pdf.cell(50, 5, feature_info['no_EGRID'].encode('iso-8859-1'), 0, 1, 'L')

    # Third infoline : Adresse/localisation
    pdf.set_font(*pdfconfig.textstyles['bold'])
    pdf.cell(45, 5, translations['addresslabel'], 0, 0, 'L')
    pdf.set_font(*pdfconfig.textstyles['normal'])
    pdf.cell(50, 5, str('Placeholder').encode('iso-8859-1'), 0, 1, 'L')

     # Fourth infoline : municipality and BFS number
    pdf.set_font(*pdfconfig.textstyles['bold'])
    pdf.cell(45, 5,  translations['municipalitylabel']+str(' (')+translations['federalmunicipalitynumberlabel']+str(')'), 0, 0, 'L')
    pdf.set_font(*pdfconfig.textstyles['normal'])
    pdf.cell(50, 5, feature_info['nomcom'].encode('iso-8859-1')+str(' (')+str(feature_info['nufeco']).encode('iso-8859-1')+str(')'), 0, 0, 'L')

    # Creation date and operator
    y= pdf.get_y()
    pdf.set_y(y+10)
    pdf.set_font(*pdfconfig.textstyles['bold'])
    pdf.cell(45, 5, translations['extractoperatorlabel'], 0, 0, 'L')
    pdf.set_font(*pdfconfig.textstyles['normal'])
    pdf.cell(70, 5, feature_info['operator'].encode('iso-8859-1'), 0, 1, 'L')

    y= pdf.get_y()
    pdf.set_y(y+5)
    pdf.set_font(*pdfconfig.textstyles['bold'])
    pdf.cell(0, 5, translations['signaturelabel'], 0, 0, 'L')

    pdf.set_y(250)
    pdf.set_font(*pdfconfig.textstyles['bold'])
    pdf.cell(0, 5, translations['disclaimerlabel'], 0, 1, 'L')
    pdf.set_font(*pdfconfig.textstyles['normal'])
    pdf.multi_cell(0, 5, translations['disclaimer'], 0, 1, 'L')

    # END TITLEPAGE

    return pdf
from owslib.wms import WebMapService
url='http://geoportal.cuzk.cz/WMS_ZM10_PUB/WMService.aspx?service=WMS&request=getCapabilities'
wms = WebMapService(url)
print (u'{}\n{}{}\n{}'.format(wms.identification.title,
                              wms.identification.abstract,
                              wms.provider.name,
                              wms.provider.contact.address))

print (wms.contents)

layer = 'GR_ZM10'
print ('{}\n{}'.format(wms.contents[layer].boundingBox,
                       wms.contents[layer].boundingBoxWGS84))

img = wms.getmap(
    layers=[layer],
    size=[800, 600],
    srs="EPSG:5514",
    bbox=[-950003, -1250003, -399990, -899996],
    format="image/png"
)
with open('data/wms_download.png', 'wb') as out:
    out.write(img.read())
Exemple #45
0
def get_records(post_code_for_bounding_box):

    pc_info = utils.post_codes[int(post_code_for_bounding_box)]

    print "Getting images for: %s - %s, %s" % (post_code_for_bounding_box, pc_info['suburb'], pc_info['state'])

    lower_corner = '%s %s' % (pc_info['lon'], pc_info['lat'])
    upper_corner = '%f %f' % (float(pc_info['lon']) + 1.0, float(pc_info['lat']) + 0.5)

    body = """<?xml version="1.0" encoding="UTF-8"?>
<csw:GetRecords xmlns:gml="http://www.opengis.net/gml"
xmlns:ogc="http://www.opengis.net/ogc"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns:csw="http://www.opengis.net/cat/csw/2.0.2"
outputSchema="http://www.opengis.net/cat/csw/2.0.2"
outputFormat="application/xml" version="2.0.2" service="CSW"
resultType="results" maxRecords="10" nextRecord="0"
xsi:schemaLocation="http://www.opengis.net/cat/csw/2.0.2
http://schemas.opengis.net/csw/2.0.2/CSW-discovery.xsd">
<csw:Query typeNames="csw:Record">
<csw:ElementSetName>full</csw:ElementSetName>
<csw:Constraint version="1.1.0">
<ogc:Filter>
<ogc:And>
<ogc:PropertyIsLike escape="\" singleChar="_" wildCard="%">
<ogc:PropertyName>Title</ogc:PropertyName>
<ogc:Literal>%Landsat%</ogc:Literal>
</ogc:PropertyIsLike>
<ogc:BBOX>
<ogc:PropertyName>ows:BoundingBox</ogc:PropertyName>
<gml:Envelope>
<gml:lowerCorner>""" + lower_corner + """</gml:lowerCorner> 
<gml:upperCorner>""" + upper_corner + """</gml:upperCorner>
</gml:Envelope>
</ogc:BBOX>
</ogc:And>
</ogc:Filter>
</csw:Constraint>
<ogc:SortBy>
<ogc:SortProperty>
<ogc:PropertyName>apiso:TempExtent_begin</ogc:PropertyName>
<ogc:SortOrder>ASC</ogc:SortOrder>
</ogc:SortProperty>
</ogc:SortBy>
</csw:Query>
</csw:GetRecords>"""

    headers = {
        'Accept-Encoding': 'gzip,deflate',
        'Content-Type': 'text/xml;charset=UTF-8',
        'Content-Length': len(body),
        'Host': HOST_NAME,
        'Connection': 'Keep-Alive',
        'User-Agent': 'GovHack - Team A Kicking Wheel'
        }

    #request_path = '%s?%s&format=json' % (HOST_PATH, query_params)
    request_path = '%s?request=GetRecords' % (HOST_PATH)

    conn = httplib.HTTPConnection(HOST_NAME, HOST_PORT)
    conn.request('POST', request_path, body, headers)
    response = conn.getresponse()

    print 'GetRecords: %d %s' % (response.status, response.reason)

    if response.status == 200:
        data = response.read()
        conn.close()

        result_tree = ET.ElementTree(ET.fromstring(data))

        namespaces = {
            'csw': 'http://www.opengis.net/cat/csw/2.0.2',
            'dc': 'http://purl.org/dc/elements/1.1/',
            'ows': 'http://www.opengis.net/ows',
            }
        results = result_tree.getroot().findall(
            'csw:SearchResults/csw:Record', namespaces=namespaces
            )

        for result in results:
            get_capabilities_response = result.find(
                "dc:URI[@protocol='OGC:WMS-1.3.0-http-get-capabilities']",
                namespaces=namespaces
                )
            wms_url = get_capabilities_response.text
            print 'Record URL: %s' % wms_url

            service = WebMapService(wms_url, version='1.1.1')

            bounding_box_strs = lower_corner.split(' ') + upper_corner.split(' ')
            bounding_box = tuple([float(i) for i in bounding_box_strs])

            print 'Bounding Box: ' + str(bounding_box)

            img = service.getmap(
                layers=['FalseColour741'], styles=[''],
                srs='EPSG:4326', bbox=bounding_box, size=(1036, 746),
                format='image/png'#, transparent=True
                )

            identifier = result.find('dc:identifier', namespaces=namespaces).text
            directory = 'landsat_images/%s' % pc_info['suburb']

            if not os.path.exists(directory):
                os.makedirs(directory)

            file_name = '%s/%s.png' % (directory, identifier)

            print 'Writing file: %s' % file_name

            out = open(file_name, 'wb')
            out.write(img.read())
            out.close()
Exemple #46
0
for ii in range(0, no_tiles_x):
    print("\n")
    sys.stdout.write(str(ii))
    for jj in range(0, no_tiles_y):
        ll_x_ = x_min + ii * dx
        ll_y_ = y_min + jj * dy
        bbox = (ll_x_, ll_y_, ll_x_ + dx, ll_y_ + dy)
        filename = "{}{}_{}_{}_{}.jpg".format(TILE_FOLDER, bbox[0], bbox[1],
                                              bbox[2], bbox[3])
        if os.path.isfile(filename):
            sys.stdout.write('.')
            continue

        img = wms.getmap(layers=['Actueel_ortho25'],
                         srs='EPSG:28992',
                         bbox=bbox,
                         size=(256, 256),
                         format='image/jpeg',
                         transparent=True)
        out = open(filename, 'wb')
        out.write(img.read())
        out.close()
        sys.stdout.write('.')

# ## 1b. Downloading the shapefiles

# In[ ]:

if not os.path.exists(DATA_FOLDER):
    os.makedirs(DATA_FOLDER)

for url in URLS_SHAPEFILES:
Exemple #47
0
def WmsSources (my_input,my_sheet,my_creds):


        def check_blank(content):
            """
            Uses PIL (Python Imaging Library to check if an image is a single colour - i.e. blank
            Images are loaded via a binary content string
            Checks for blank images based on the answer at:
            http://stackoverflow.com/questions/1110403/how-can-i-check-for-a-blank-image-in-qt-or-pyqt
            """

            im = Image.open(content)
            # we need to force the image to load (PIL uses lazy-loading)
            # otherwise get the following error: AttributeError: 'NoneType' object has no attribute 'bands'
            im.load() 
            bands = im.split()

            # check if the image is completely white or black, if other background colours are used
            # these should be accounted for
            is_all_white = all(band.getextrema() == (255, 255)  for band in bands)
            is_all_black = all(band.getextrema() == (0, 0)  for band in bands)

            is_blank = (is_all_black or is_all_white)

            return is_blank

        #define functions
        # in this wrapper class you can use a string list instead of a full string like Im doing
        class StdOutWrapper:
                lines = []
                def write(self,txt):
                        self.lines.append(txt)
                def get_text(self):
                        return str('\n'.join(self.lines))

        #define ScaleHint based bbox
        def  calculateBounds(center, scaleDenominator, size_b):
            resolution = 1 / ((1 / scaleDenominator) * 4374754 * 72)
            halfWDeg = (size_b[0] * resolution) / 2 #width
            halfHDeg = (size_b[1] * resolution) / 2 #height
            return(
                center['lon'] - halfWDeg,
                center['lat'] - halfHDeg,
                center['lon'] + halfWDeg,
                center['lat'] + halfHDeg
                                )


        #Set Var
        size_b=(300, 250)
        my_sheet="WMSTEST"
        my_temp="checkwms_result/"
        my_creds="kompost-08abcd76df4c.json"
        my_data={"WMS_IDENT_TITLE":"n.a.","URL":"n.a","Layer":"n.a","Test_OGC":"n.a.","Test_Speed":"-9999","Test_Browser":"n.a.","MapGeo_Link":"n.a.","OtherError":""}


        #wms = WebMapService('http://wms.zh.ch/OrthoZHWMS?SERVICE=WMS&Request=GetCapabilities')
        #wms = WebMapService('https://ows.terrestris.de/osm/service')
        #my_input='https://wms.geo.gl.ch/'
        #my_input=('https://wms.geo.admin.ch/')
        #my_input='http://wms.geo.gr.ch/wms/admineinteilung'
        #wms = WebMapService('http://wms.vd.ch/public/services/wmsVD/Mapserver/Wmsserver')

        #check if server exists at all on level httpp
        try:
                request = requests.get(my_input)
                if request.status_code == 200:
                        #READ WMS
                            mystdout = StdOutWrapper() #-> undocument for production, when testing promtpt is messed up
                            sys.stdout = mystdout      #-> undocument for production, when testing promtpt is messed up
                            sys.stderr = mystdout      #-> undocument for production, when testing promtpt is messed up 
                            try:
                                wms = WebMapService(my_input)
                                my_data["OtherError"]= my_data["OtherError"]+" "+str(mystdout.get_text()) #-> undocument for production, when testing promtpt is meesed 
                                sys.stdout = sys.__stdout__ #-> undocument for production, when testing promtpt is meesed 
                                sys.stderr = sys.__stderr__ #-> undocument for production, when testing promtpt is meesed 

                                #filling in global variables
                                my_data["WMS_IDENT_TITLE"]= wms.identification.title
                                my_data["URL"]=wms.url

                                #playground for debugging
                                wms.identification.type
                                wms.identification.title
                                wmsurl=wms.url
                                list(wms.contents)
                                layers=list(wms.contents)

                                #JUST FOR TESTING FAST each WMS THIS LINE BELOW
                                #######layers=layers[0:1]#remove this line

                                #loop through layers of WMS

                                for i in layers:
                                        #Layer Name
                                        my_data["Layer"]= wms[i].title
                                        center_orig={'lat':(wms[i].boundingBoxWGS84[1]+wms[i].boundingBoxWGS84[3])/2,'lon':(wms[i].boundingBoxWGS84[0]+wms[i].boundingBoxWGS84[2])/2} #image center coordinates
                                        #Map.geo.admin.ch URL
                                        my_data["MapGeo_Link"] = r"https://map.geo.admin.ch/?bgLayer=ch.swisstopo.pixelkarte-grau&layers=WMS||"+wms[i].title+"||"+wms.url+"?||"+wms[i].title+"||"+wms.identification.version
                                        if wms[i].scaleHint is None:
                                                bbox_b=(wms[i].boundingBoxWGS84)
                                                #print('full extent taken')
                                        else:
                                                minscale=((float(wms[i].scaleHint['min'])/sqrt(2))*75/2.54*100)
                                                maxscale=((float(wms[i].scaleHint['max'])/sqrt(2))*75/2.54*100)
                                                bbox_b=(calculateBounds(center_orig,(maxscale-1000),size_b))
                                        #print('maxscale taken')
                                        #try:
                                        #print(wms[i].title)
                                        img = wms.getmap(   layers=[i],
                                                    srs=wms[i].crsOptions[0],
                                                    #srs='EPSG:4326',
                                                    bbox=bbox_b,
                                                    size=size_b,
                                                    #format=wms.getOperationByName('GetMap').formatOptions[0]
                                                    format='image/jpeg'
                                                    )
                                        #the follwoing url has been called
                                        #my_geturl=(img.geturl())
                                        outfile=my_temp+"getmapimage.jpeg"
                                        out = open(outfile, 'wb')
                                        out.write(img.read())
                                        out.close()
                                        #response time
                                        my_data["Test_Speed"]=requests.get(img.geturl()).elapsed.total_seconds()
                                        resp=requests.get(img.geturl())

                        #TEST OGC
                                        #check if image is empty
                                        if resp.headers['content-type'] == 'image/jpeg':
                                                # a PNG image was returned
                                                is_blank = check_blank(outfile)
                                                if is_blank:
                                                        print(wms[i].title+" : blank image but OK")
                                                        log_file = open(my_temp+"_deadwms.txt", 'a+')
                                                        log_file.write(wms[i].title+" : blank image"+"\n")
                                                        log_file.close()
                                                        my_data["Test_OGC"]="ok (blank_image)"
                                                        os.remove(outfile)
                                                        #send to gmail
                                                        PushToGspread.ExportToGoogle(my_data,my_sheet,my_creds)
                                                else:
                                                        #print("The image contains data.")
                                                        print(wms[i].title+" : OK")
                                                        my_data["Test_OGC"]="ok"
                                                        os.remove(outfile)
                                                        #send to gmail
                                                        PushToGspread.ExportToGoogle(my_data,my_sheet,my_creds)

                                        else:
                                                # if there are errors then these can be printed out here
                                                print(resp.content)
                                                #send to gmail
                                                PushToGspread.ExportToGoogle(my_data,my_sheet,my_creds)
                                    
                            except Exception as e:
                                    sys.stdout = sys.__stdout__ #-> undocument for production, when testing promtpt is messed 
                                    sys.stderr = sys.__stderr__ #-> undocument for production, when testing promtpt is messed 
                                    my_data["OtherError"]= my_data["OtherError"]+" "+str(mystdout.get_text()) #-> undocument for production, when testing promtpt is messed 
                                    log_file = open(my_temp+"_deadwms.txt", 'a+')
                                    log_file.write(my_input+", "+str(e)+"\n")
                                    log_file.close()
                                    print (e)
                                    my_data["Test_OGC"]= e
                                    my_data["URL"]= my_input
                                    #send to gmail
                                    PushToGspread.ExportToGoogle(my_data,my_sheet,my_creds)
                else:
                        log_file = open(my_temp+"_deadwms.txt", 'a+')
                        log_file.write(" 404 Client Error: Not Found for url:"+my_input+"\n")
                        log_file.close()
                        print ("404 Client Error: Not Found for url: "+my_input)
                        my_data["Test_OGC"]= "404 Client Error: Not Found for url: "+my_input
                        my_data["URL"]= my_input
                        my_data["OtherError"]= my_data["OtherError"]+"404 Client Error: Not Found for url: "+my_input
                        PushToGspread.ExportToGoogle(my_data,my_sheet,my_creds)
        except Exception as e_request:
                log_file = open(my_temp+"_deadwms.txt", 'a+')
                log_file.write(str(e_request)+" "+my_input+"\n")
                log_file.close()
                print (e_request)
                my_data["Test_OGC"]= str(e_request)+" "+my_input
                my_data["URL"]= my_input
                my_data["OtherError"]= my_data["OtherError"]+str(e_request)+" "+my_input
                PushToGspread.ExportToGoogle(my_data,my_sheet,my_creds)

        print(my_input+"..done")
import numpy as np
from pyspark import SparkContext
from pyproj import Proj

c = crs.Crs('EPSG:3857')
wms = WebMapService('http://www.ign.es/wms-inspire/pnoa-ma', version='1.3.0')

box = 1000  # m?
x = 236814  #m?
y = 5068880  #m?
picsize = 512

img = wms.getmap(
    layers=['OI.OrthoimageCoverage'],
    styles=[],
    srs='EPSG:3857',
    bbox=(x - box, y - box, x + box, y + box),
    size=(picsize, picsize),  #W,H px
    format='image/png',
    transparent=False)
with open('image.png', 'wb') as out:
    out.write(img.read())


def green_detection():
    img = cv2.imread('image.png')
    # Green fields detection
    hsv = cv2.cvtColor(img, cv2.COLOR_BGR2HSV)

    #Threshold for detecting green
    lower = np.array([15, 58, 15])
    upper = np.array([100, 240, 100])
class WmsApi:
    def __init__(self, zoom_level=19):
        self.logger = logging.getLogger(__name__)
        self.current_directory = os.path.dirname(os.path.realpath(__file__))
        self.env = self._read_env()
        self.auth = self.set_auth()
        self.zoom_level = zoom_level

        self._auth_monkey_patch(self.auth)

        from owslib.wms import WebMapService

        self.wms = WebMapService(url=self.env("URL"), version=self.env("VERSION"))

    def set_auth(self):
        user = self.env("NTLM_USER")
        password = self.env("NTLM_PASSWORD")
        return HttpNtlmAuth(user, password) if user is not None and password is not None else None

    def _read_env(self):
        env = environ.Env(
            NTLM_USER=(str, None),
            NTLM_PASSWORD=(str, None),
            URL=(str, None),
            SRS=(str, None),
            VERSION=(str, None),
            LAYER=(str, None),
        )
        current = environ.Path(self.current_directory)
        environ.Env.read_env(current(".env"))
        self._settings_check(env)
        return env

    def _settings_check(self, env):
        if env("URL") is None or env("SRS") is None or env("VERSION") is None or env("LAYER") is None:
            error_message = "You have to set all URL, SRS, LAYER and VERSION in your .env config file."
            self.logger.error(error_message)
            raise Exception(error_message)

    @staticmethod
    def _auth_monkey_patch(auth):
        AuthMonkeyPatch(auth)

    def get_image(self, bbox):
        size = self._calculate_image_size(bbox, self.zoom_level)
        image = self._get(
            layers=[self.env("LAYER")], srs=self.env("SRS"), bbox=self._box(bbox), size=size, format="image/jpeg"
        )
        return image

    @staticmethod
    def _calculate_image_size(bbox, zoom_level):
        meters_per_pixel = geo_helper.meters_per_pixel(zoom_level, bbox.bottom)
        width_meter = bbox.node_left_down().get_distance_in_meter(bbox.node_right_down())
        height_meter = bbox.node_left_down().get_distance_in_meter(bbox.node_left_up())
        height = int(height_meter / meters_per_pixel)
        width = int(width_meter / meters_per_pixel)
        return width, height

    def _get(self, **kwargs):
        img = self.wms.getmap(**kwargs)
        return Image.open(BytesIO(img.read()))

    @staticmethod
    def _box(bbox):
        node_left_down = bbox.node_left_down()
        node_right_up = bbox.node_right_up()
        return node_left_down.longitude, node_left_down.latitude, node_right_up.longitude, node_right_up.latitude
Exemple #50
0
def getwms(service_url):

	# add default
	def_service_url = 'http://climate4impact.eu/impactportal/ImpactService?source=http://opendap.knmi.nl/knmi/thredds/dodsC/CLIPC/tier1_indicators/icclim_cerfacs/CDD/MPI-M-MPI-ESM-LR_rcp45_r1i1p1_SMHI-RCA4_v1/CDD_DEC_MPI-M-MPI-ESM-LR_rcp45_r1i1p1_SMHI-RCA4_v1_EUR-11_2006-2100.nc'
	if service_url is None:
		service_url = def_service_url


	# create wms
	wms = WebMapService( service_url , version='1.1.1')

	print "wms url: " , service_url
	print "type: "    , wms.identification.type
	print "version: " , wms.identification.version 
	print "title: "   , wms.identification.title
	print "abstract: ", wms.identification.abstract


	print "contents: " , list(wms.contents)

	print ""
	layer = list(wms.contents)[0]
	#print layer
	print "title: "   , wms[layer].title
	bb1 = wms[layer].boundingBox
	bb2 = wms[layer].boundingBoxWGS84
	print "bbox:      "   , bb1
	print "bboxWGS84: "   , bb2
	bb = bb1

	print "crsOptions: "   , wms[layer].crsOptions
	print "styles: "   
	for s in wms[layer].styles:
		print "  " , s

	print ""
	print "operations:" , [op.name for op in wms.operations]

	#print wms.getOperationByName('GetMap').methods
	#print wms.getOperationByName('GetMap').formatOptions


	bl = list(wms.contents)[1]
	ol = list(wms.contents)[3]

	
	print "wms.getmap"
	img = wms.getmap( layers=[bl,layer,ol] ,
		#styles=[style],
		srs='EPSG:3411', 
		bbox=(bb[0], bb[1], bb[2], bb[3]),
		size=(500, 500), 
		format='image/png',
		transparent = True)

	#print "url of image: " , img.geturl()

	img_name = 'image_wms_vdd.png'
	print "open img file: " , img_name

	out = open( img_name, 'wb')
	out.write(img.read())
	out.close()
	print "end"

	f = Image.open( img_name ).show()

	return wms
class WMSDataServiceToReclineJS():

    def __init__(self, url, version="1.1.1"):
        self.wms = WebMapService(url, version=version)
        self.type = self.wms.identification.type
        self.version = self.wms.identification.version
        self.title = self.wms.identification.title
        self.abstract = self.wms.identification.abstract
        self.size = (256, 256)

    def get_layers(self):
        return list(self.wms.contents)

    def get_srs(self, layer, srs='EPSG:4326'):
        thisSRS = srs
        thisLayer = self.wms[layer]
        srsList = thisLayer.crsOptions
        if thisSRS in srsList:
            return thisSRS
        else:
            return thisSRS + " not found"

    def get_service_operations(self):
        thisWMS = self.wms.operations
        return [op.name for op in thisWMS]

    def get_service_methods(self):
        thisWMS = self.wms.getOperationByName('GetMap').methods
        return thisWMS

    def get_GET_url(self):
        methods = self.get_service_methods()
        return methods['Get']['url']

    def get_service_format_options(self, format='image/png'):
        thisFormat = format
        thisWMS = self.wms.getOperationByName('GetMap').formatOptions
        if thisFormat in thisWMS:
            return thisFormat
        else:
            return thisWMS

    def get_layer_details(self, layer):
        keys = ['layer', 'bbox', 'srs', 'format']
        thisLayer = self.wms[layer]
        bbox = thisLayer.boundingBoxWGS84
        thisSRS = self.get_srs(layer)
        return dict(zip(keys,[thisLayer,bbox,thisSRS,'none']))

    def get_service_url(self, layer):
        thisFormat = self.get_service_format_options(format)
        layer_details = self.get_layer_details(layer)
        serviceURL = self.wms.getmap(layers=[layer],
                                srs=layer_details['srs'],
                                bbox=layer_details['bbox'],
                                size=self.size,
                                format=thisFormat)
        return serviceURL

    def hack_up_a_layer_name(self, data_dict):
        data = data_dict.get("resource")
        if data.get("layer_name"):
            return data.get("layer_name")
        elif data.get("layer"):
            return data.get("layer")
        elif data.get("layers"):
            return data.get("layers")
        else:
            try:
                layer_list = self.get_layers()
                return layer_list[0]
            except:
                return "Sorry, can't find a layer!"

    def recline_ogc_wms(self, data_dict):
        data = data_dict
        keys = ["layer", "url"]
        layer = self.hack_up_a_layer_name(data)
        url = self.get_GET_url()
        return dict(zip(keys,[layer,url]))

    def ogc_wms_variables(self, data_dict):
        data
    def perform_request(self):
        """
        Perform the drilldown.
        See https://github.com/geopython/OWSLib/blob/
        master/tests/doctests/wms_GeoServerCapabilities.txt
        """
        wms = None

        # 1. Test capabilities doc, parses
        result = Result(True, 'Test Capabilities')
        result.start()
        try:
            wms = WebMapService(self._resource.url)
            title = wms.identification.title
            self.log('response: title=%s' % title)
        except Exception as err:
            result.set(False, str(err))

        result.stop()
        self.result.add_result(result)

        # 2. Test layers
        # TODO: use parameters to work on less/more drilling
        # "full" could be all layers.
        result = Result(True, 'Test Layers')
        result.start()
        try:
            # Pick a random layer
            layer_name = random.sample(wms.contents.keys(), 1)[0]
            layer = wms[layer_name]

            # TODO Only use EPSG:4326, later random CRS
            if 'EPSG:4326' in layer.crsOptions \
                    and layer.boundingBoxWGS84:

                # Search GetMap operation
                get_map_oper = None
                for oper in wms.operations:
                    if oper.name == 'GetMap':
                        get_map_oper = oper
                        break

                format = None
                for format in get_map_oper.formatOptions:
                    if format.startswith('image/'):
                        break

                # format = random.sample(get_map_oper.formatOptions, 1)[0]

                self.log('testing layer: %s' % layer_name)
                layer_bbox = layer.boundingBoxWGS84
                wms.getmap(layers=[layer_name],
                           styles=[''],
                           srs='EPSG:4326',
                           bbox=(layer_bbox[0],
                                 layer_bbox[1],
                                 layer_bbox[2],
                                 layer_bbox[3]),
                           size=(256, 256),
                           format=format,
                           transparent=False)

                self.log('WMS GetMap: format=%s' % format)
                # Etc, to be finalized

        except Exception as err:
            result.set(False, str(err))

        result.stop()

        # Add to overall Probe result
        self.result.add_result(result)
Exemple #53
0
def getMap(restriction_layers,topicid,crdppf_wms,map_params,pdf_format):
    """Produces the map and the legend for each layer of an restriction theme
    """

    # Name of the pdf file - should be individualized with a timestamp or ref number
    pdf_name = 'extract'
    # Path to the output folder of the pdf
    temp_path = pkg_resources.resource_filename('crdppf', 'static/public/temp_files/')

    #pdf_path = pkg_resources.resource_filename('crdppf','static\public\pdf\\')

    # Map scale
    scale = pdf_format['scale']

    # List with the base layers of the map - the restriction layers get added to the list
    layers = [
        'parcelles',
        'mo22_batiments',
        'mo21_batiments_provisoires',
        'mo23_batiments_projetes',
        'ag1_parcellaire_provisoire',
        'mo9_immeubles',
        'mo5_point_de_detail',
        #~ 'mo14_servitudes_g_surf',
        #~ 'mo14_servitudes_g_lig',
        #~ 'mo14_servitudes_g_pts',
        #~ 'mo14_servitudes_a_surf',
        #~ 'mo14_servitudes_a_lig',
        #~ 'mo14_servitudes_c_surf',
        #~ 'mo14_servitudes_c_surf_autre',
        #~ 'mo14_servitudes_c_lig',
        'mo7_obj_divers_lineaire',
        'mo7_obj_divers_couvert',
        'mo7_obj_divers_piscine',
        'mo7_obj_divers_cordbois',
        'mo4_pfa_1',
        'mo4_pfp_3',
        #'mo9_immeubles_txt_rappel',
        'mo4_pfp_1_2',  
        'la3_limites_communales',
        'mo22_batiments'
    ]

    # temp var to hold the parameters of the legend
    legend_layers = []
    # temp var for the path to the created legend
    legend_path = []

    # Adding each layer of the restriction to the WMS
    for layer in restriction_layers:
        # Compile the layer list for the wms
        layers.append(layer.layername)

        # in the same time create the legend graphic for each layer and write it to disk
        legend = open(temp_path+str('legend_')+str(layer.layername)+'.png', 'wb')
        img = urllib.urlopen(crdppf_wms+ \
            str('?TRANSPARENT=TRUE&SERVICE=WMS&VERSION=1.1.1&REQUEST=GetLegendGraphic&FORMAT=image%2Fpng&LAYER=' \
            +str(layer.layername)))

        legend.write(img.read())
        legend.close()
        legend_path.append(temp_path+str('legend_')+str(layer.layername))

    # to recenter the map on the bbox of the feature, compute the best scale and add at least 10% of space we calculate a wmsBBOX
    wmsBBOX = {}
    wmsBBOX['centerY'] = int(map_params['bboxCenterY'])
    wmsBBOX['centerX'] = int(map_params['bboxCenterX'])
    # From the center point add and substract half the map distance in X and Y direction to get BBOX min/max coords
    wmsBBOX['minX'] = int(wmsBBOX['centerX']-(pdf_format['width']*scale/1000/2))
    wmsBBOX['maxX'] = int(wmsBBOX['centerX']+(pdf_format['width']*scale/1000/2))
    wmsBBOX['minY'] = int(wmsBBOX['centerY']-(pdf_format['height']*scale/1000/2))
    wmsBBOX['maxY'] = int(wmsBBOX['centerY']+(pdf_format['height']*scale/1000/2))

    # call the WMS and write the map to file
    wms = WebMapService(crdppf_wms, version='1.1.1')

    map = wms.getmap(
        layers=layers,
        srs='EPSG:21781',
        bbox=(wmsBBOX['minX'],wmsBBOX['minY'],wmsBBOX['maxX'],wmsBBOX['maxY']),
        size=(map_params['width'], map_params['height']),
        format='image/png',
        transparent=False
    )

    out = open(temp_path+pdf_name+str(topicid)+'.png', 'wb')
    out.write(map.read())
    out.close()
    mappath = temp_path + pdf_name + str(topicid) + '.png'
        
    return mappath, legend_path
Exemple #54
0
class WMS(object):
    """
            Class to deal with Web Map Services (WMS) in GemGIS
    """
    def __init__(self, **kwargs):
        """Loading the Web Map Service
        Kwargs:
            path: alternative file path for Web Map Service
        Returns:
            wms: owslib.map.wms111.WebMapService_1_1_1
         """

        # Load URL of WMS service, if not provided use a default service
        url = kwargs.get('url', 'https://ows.terrestris.de/osm/service?')

        # Setting attributes of the WMS object
        try:
            self.object = WebMapService(url)
        except SSLError:
            print(
                "gemgis: SSL Error, potentially related to missing module - try:\n\n pip install -U openssl \n\n"
            )
            raise

        self.url = url
        self.type = self.object.identification.type
        self.version = self.object.identification.version
        self.title = self.object.identification.title
        self.abstract = self.object.identification.abstract
        self.contents = list(self.object.contents)
        self.operations = self.object.operations

    def __getitem__(self, item):
        return self.object[item]

    def getOperationByName(self, name):
        return self.object.getOperationByName(name)

    def getmap(self, **kwargs):
        """Extracting a tile from a WMS service

        Kwargs:
            See subsequent functions

        Return:
            map: Image Overlay to be displayed on the Map
        """

        # Create Map Object
        map = self.getmap_object(**kwargs)

        # Convert Map Object to array
        array = self.convert_map_to_array(map=map, format='tiff')

        # Convert array to Image Overlay
        map = self.convert_array_to_image_overlay(array, **kwargs)

        return map

    def getmap_object(self, **kwargs):
        """
            Request imagery
            Args:
                wms: owslib.map.wms111.WebMapService_1_1_1
            Kwargs:
                layers list - List of content layer names
                styles: list - Optional list of named styles, must be the same length as the layers list
                srs: string - A spatial reference system identifier.
                extent: tuple - (left, bottom, right, top) in srs units, can be the same as for GemPy
                size: tuple - (width, height) in pixels.
                format: string - Output image format such as 'image/jpeg'.
                transparent: bool - Optional. Transparent background if True.

            Returns:
                image: numpy.ndarray
            """

        layer = [kwargs.get('layer', list(self.contents)[0])]
        print('Layer: %s' % layer)

        # Setting the Style of the Layer
        # If WMS contains no style, styles is set to None
        if 'styles' not in kwargs.keys() and 'layer' not in kwargs.keys():
            if not self[list(self.contents)[0]].styles:
                styles = None
            elif self[list(self.contents)[0]].styles:
                style = \
                    [(key, self[list(self.contents)[0]].styles[key]) for key in self[list(self.contents)[0]].styles][0][
                        0]
                styles = [style]
        elif 'styles' not in kwargs.keys() and 'layer' in kwargs.keys():
            # Dictionary of any but the first layer cannot be accessed right now, needs to be fixed
            if not self[list(self.contents)[0]].styles:
                styles = None
            else:
                style = \
                [(key, self[list(self.contents)[0]].styles[key]) for key in self[list(self.contents)[0]].styles][0][0]
                styles = [style]
        elif 'styles' in kwargs.keys() and 'layer' in kwargs.keys():
            styles = kwargs.get('styles', None)
        print('Style: %s' % styles)

        crs = kwargs.get('crs', 'EPSG:4326')
        extent = kwargs.get('extent', (5, 49, 10, 52))

        if extent[0] > extent[2]:
            raise ValueError('First x-coordinate must be smaller than second')
        if extent[1] > extent[3]:
            raise ValueError('First y-coordinate must be smaller than second')

        size = kwargs.get('size', (2000, 2000))
        form = kwargs.get('form',
                          self.getOperationByName('GetMap').formatOptions[1])

        map = self.object.getmap(layers=layer,
                                 styles=styles,
                                 srs=crs,
                                 bbox=extent,
                                 size=size,
                                 format=form,
                                 transparent=True)

        return map

    def convert_map_to_array(self, **kwargs):
        """Converting a map Object into an array

        Kwargs:
            map: map object to be converted
            layer: string - layer to be converted
            format: string - format of map
        """

        # Selection whether a map object or an array is being converted to an array
        if 'map' in kwargs.keys():
            map = kwargs.get('map', None)
            map = io.BytesIO(map.read())
        elif 'layer' in kwargs.keys():

            map = self.getmap_object(**kwargs)

        # Define format
        if 'format' not in kwargs.keys():
            array = plt.imread(map)
        else:
            format = kwargs.get('format', 'png')
            array = plt.imread(map, format=format)

        return array

    def convert_array_to_image_overlay(self, array, **kwargs):
        """Converting an array to an Image Overlay

        Args:
            array: ndarray - array to be converted to an Image Overlay
        Kwargs:
            layer: string - layer of WMS Service
            extent: tuple - extent of the map
            crs: string - crs of the layer
        """

        layer = kwargs.get('layer', 'WMS Layer')
        extent = kwargs.get('extent', (5, 49, 10, 52))
        crs = kwargs.get('crs', None)

        # If the extent is not provided in WGS 84 coordinates, it will be transformed
        if 'crs' in kwargs.keys():
            if crs is None or crs == 'EPSG:4326':
                extent_transf = extent
            else:
                proj_custom = Proj(init=crs)
                proj_deafult = Proj(init='epsg:4326')

                extent_transf = np.zeros(4)
                extent_transf[0], extent_transf[1] = transform(
                    proj_custom, proj_deafult, extent[0], extent[1])
                extent_transf[2], extent_transf[3] = transform(
                    proj_custom, proj_deafult, extent[2], extent[3])
        else:
            extent_transf = extent

        # Array is saved as PNG File for the Image Overlay
        plt.imsave('%s.png' % layer, array)

        # Creating Image Overlay
        map = ImageOverlay(url='%s.png' % layer,
                           name=[layer][0],
                           bounds=((extent_transf[1], extent_transf[0]),
                                   (extent_transf[3], extent_transf[2])))

        return map

    def save_as_tiff(self, array, path, show_image=True, **kwargs):
        """Save WMS array as TIFF

        Args:
            array: ndarray - array to be saved as tiff
            path: string - path and filename of the tiff
            show_image: bool - show image of the saved file, default is true
        """

        array = np.flipud(array)
        im = Image.fromarray(array)
        im.save(path, 'TIFF', **kwargs)
        print('File saved successfully')

        if show_image is True:
            display(im)
#!/usr/bin/python
# -*- coding: UTF-8 -*-
__author__ = 'Juergen Weichand'

from owslib.wms import WebMapService
wms = WebMapService('http://geoserv.weichand.de:8080/geoserver/wms')

# GetMap (image/jpeg)
response = wms.getmap(
    layers=['bvv:gmd_ex'],
    srs='EPSG:31468',
    bbox=(4500000,5500000,4505000,5505000),
    size=(500,500),
    format='image/jpeg')

out = open('/tmp/getmap-response.jpeg', 'wb')
out.write(response.read())
out.close()

# GetFeatureInfo (text/html)
response = wms.getfeatureinfo(
    layers=['bvv:gmd_ex'],
    srs='EPSG:31468',
    bbox=(4500000,5500000,4505000,5505000),
    size=(500,500),
    format='image/jpeg',
    query_layers=['bvv:gmd_ex'],
    info_format="text/html",
    xy=(250,250))

out = open('/tmp/getfeatureinfo-response.html', 'wb')
Exemple #56
0
    print(wms['vector:AS_LULC50K_1112'].crsOptions)
    print(wms['vector:AS_LULC50K_1112'].styles)
    '''
    # download WMS image for the study area
    # consider spliting large area - high resolution/large data may end with download failure
    pixsize = 0.0002  # put the desired resolution (in the SRS unit), 0.0002 deg = approx. 20m
    xmax = 93.02335   # put the desired extent - x maximum
    xmin = 92.1496    # put the desired extent - x minimum
    ymax = 26.6149    # put the desired extent - y maximum
    ymin = 25.7929    # put the desired extent - y minimum
    xwidth = math.ceil((xmax-xmin)/pixsize)
    ywidth = math.ceil((ymax-ymin)/pixsize)
    img = wms.getmap(layers=['vector:AS_LULC50K_1112'],
            srs = 'EPSG:4326',
            bbox = (xmin,ymin,xmax,ymax),
            size = (xwidth,ywidth),
            format = 'image/png',
            transparent = False  # true means more bytes
        ) 
    out = open('studyarea.png', 'wb')
    out.write(img.read())
    out.close()

    # write an appropriate world file:
    write_worldfile("studyarea.png",pixsize,xmax,ymax)
    
    # VRT is my typical approach to turn XY data to geospatial 
    write_vrt4point("villages.csv","x","y") 
    # optional: with GDAL/OGR, convert the VRT output from above to geojson & KML
    os.system('ogr2ogr -skipfailures -overwrite -f "GeoJSON" villages.geojson villages.vrt')
    os.system('ogr2ogr -skipfailures -overwrite -f "LIBKML" villages.kml villages.vrt')