def main(map_file): map = mapscript.mapObj(map_file) map.web.metadata.set("ows_onlineresource", "http://dummy.org/") ows_req = mapscript.OWSRequest() ows_req.type = mapscript.MS_GET_REQUEST ows_req.setParameter("SERVICE", "WMS") ows_req.setParameter("VERSION", "1.1.0") ows_req.setParameter("REQUEST", "GetCapabilities") mapscript.msIO_installStdoutToBuffer() dispatch_status = map.OWSDispatch(ows_req) if dispatch_status != mapscript.MS_SUCCESS: print("An error occurred") content_type = mapscript.msIO_stripStdoutBufferContentType() mapscript.msIO_stripStdoutBufferContentHeaders() result = mapscript.msIO_getStdoutBufferBytes() # [('Content-Type', 'application/vnd.ogc.wms_xml; charset=UTF-8'), ('Content-Length', '11385')] response_headers = [('Content-Type', content_type), ('Content-Length', str(len(result)))] assert int(response_headers[1][1]) > 0 dom = xml.dom.minidom.parseString(result) print(dom.toprettyxml(indent="", newl=""))
def testWFSPostRequest(self): """OWSRequestTestCase.testLoadWMSRequest: OWS can POST a WFS request""" self.map.web.metadata.set("ows_onlineresource", "http://dummy.org/") request = mapscript.OWSRequest() request.contenttype = "application/xml" post_data = """<wfs:GetFeature xmlns:wfs="http://www.opengis.net/wfs" xmlns:ogc="http://www.opengis.net/ogc" service="WFS" version="1.1.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"> <wfs:Query typeName="*:POINT" xmlns:feature="http://www.openplans.org/topp"> <ogc:Filter> <ogc:PropertyIsEqualTo> <ogc:PropertyName>FID</ogc:PropertyName> <ogc:Literal>1</ogc:Literal> </ogc:PropertyIsEqualTo> </ogc:Filter> </wfs:Query> </wfs:GetFeature> """ qs = "" # additional parameters can be passed via the querystring request.loadParamsFromPost(post_data, qs) mapscript.msIO_installStdoutToBuffer() status = self.map.OWSDispatch(request) assert status == mapscript.MS_SUCCESS, status mapscript.msIO_stripStdoutBufferContentHeaders() result = mapscript.msIO_getStdoutBufferBytes() dom = xml.dom.minidom.parseString(result) assert len(dom.getElementsByTagName('ms:POINT')) == 1
def main(map_file): map = mapscript.mapObj(map_file) map.setMetaData("ows_onlineresource", "http://dummy.org/") ows_req = mapscript.OWSRequest() ows_req.type = mapscript.MS_GET_REQUEST ows_req.setParameter("SERVICE", "WMS") ows_req.setParameter("VERSION", "1.1.0") ows_req.setParameter("REQUEST", "GetCapabilities") mapscript.msIO_installStdoutToBuffer() dispatch_status = map.OWSDispatch(ows_req) if dispatch_status != mapscript.MS_SUCCESS: print("An error occurred") content_type = mapscript.msIO_stripStdoutBufferContentType() mapscript.msIO_stripStdoutBufferContentHeaders() result = mapscript.msIO_getStdoutBufferBytes() # [('Content-Type', 'application/vnd.ogc.wms_xml; charset=UTF-8'), ('Content-Length', '11385')] response_headers = [('Content-Type', content_type), ('Content-Length', str(len(result)))] assert int(response_headers[1][1]) > 0 dom = xml.dom.minidom.parseString(result) print(dom.toprettyxml(indent="", newl=""))
map = mapscript.mapObj('../../tests/test.map') map.setMetaData("ows_onlineresource", "http://dummy.org/") ows_req = mapscript.OWSRequest() ows_req.type = mapscript.MS_GET_REQUEST ows_req.setParameter("SERVICE", "WMS") ows_req.setParameter("VERSION", "1.1.0") ows_req.setParameter("REQUEST", "GetCapabilities") mapscript.msIO_installStdoutToBuffer() dispatch_status = map.OWSDispatch(ows_req) if dispatch_status: status = '200 OK' else: status = '500 Internal Server Error' content_type = mapscript.msIO_stripStdoutBufferContentType() mapscript.msIO_stripStdoutBufferContentHeaders() result = mapscript.msIO_getStdoutBufferBytes() try: # MapServer 6.0: mapscript.msCleanup() except: # MapServer 6.1: mapscript.msCleanup(1) response_headers = [('Content-Type', content_type), ('Content-Length', str(len(result)))]
def get(self, request, *args, **kwargs): """ Html GET method of WmsView. This view renders WMS requests into corresponding responses using the attached WmsMap class. Responses are mainly images and xml files. """ # Create response response = HttpResponse() # Setup wms request object ows_request = mapscript.OWSRequest() # If tile kwargs were provided, add tile parameters to request tileparams = self.tilemode() if tileparams: # Get image format from url format = {'.png': 'image/png', '.jpg': 'image/jpeg'}[self.kwargs.get('format')] # Return empty image if tile cant be found if not self.tile_exists(*tileparams): # Get image type and size imagetype = 'PNG' if format == 'image/png' else 'JPEG' imagesize = 256, 256 response['Content-Type'] = format # Create image and save it to response im = Image.new("RGBA", imagesize, (0, 0, 0, 0)) im.save(response, imagetype) return response else: tilebounds = self.get_tile_bounds(*tileparams) # Get layer name layers = self.kwargs.get('layers') # Setup wms parameter object request_data = { 'SERVICE': 'WMS', 'REQUEST': 'GetMap', 'VERSION': '1.1.1', 'TRANSPARENT': 'true', 'HEIGHT': '256', 'WIDTH': '256', 'SRS': 'EPSG:3857', 'FORMAT': format, 'LAYERS': layers, 'BBOX': tilebounds, } request.GET = dict(request.GET.items() + request_data.items()) # Set ows parameters from request data for param, value in request.GET.items(): ows_request.setParameter(param, value) # Instantiate WmsMap class self.wmsmap = self.map_class(request, **kwargs) # Dynamically use host for declaring service endpoint onlineresource = request.build_absolute_uri().split('?')[0] + '?' self.wmsmap.map_object.setMetaData('wms_onlineresource', onlineresource) # Dispatch map rendering self.wmsmap.map_object.OWSDispatch(ows_request) # Strip buffer from headers mapscript.msIO_stripStdoutBufferContentHeaders() # Store contenttype contenttype = mapscript.msIO_stripStdoutBufferContentType() # Write data to response response.write(mapscript.msIO_getStdoutBufferBytes()) # Set contenttype response['Content-Type'] = contenttype return response
ows_req.setParameter("SERVICE", "WMS"); ows_req.setParameter("VERSION", "1.3.0"); ows_req.setParameter("REQUEST", "GetCapabilities"); # %% [markdown] # We use the msIO methods to capture the response the request # that is sent to ```stdout```. # The response is typically an HTTP response with HTTP content headers. # We can strip these out using MapScript # %% mapscript.msIO_installStdoutToBuffer() map.OWSDispatch(ows_req) content_type = mapscript.msIO_stripStdoutBufferContentType() # remove the content type header from the XML mapscript.msIO_stripStdoutBufferContentHeaders() # Strip all Content-* headers result = mapscript.msIO_getStdoutBufferBytes() print(result) # %% [markdown] # We can also retrieve images from a WMS service. # Rather than setting lots of individual parameters we can simply load them from # a string in the same format was would be sent via a web client. # %% # First let's get the extent of the map to use in the request extent = map.extent print(extent) bbox = "BBOX={},{},{},{}".format(extent.minx, extent.miny, extent.maxx, extent.maxy) querystring = "SERVICE=WMS&REQUEST=GetMap&VERSION=1.3.0&LAYERS=lakespy2&CRS=EPSG:26915&FORMAT=image/png&WIDTH=400&HEIGHT=400&{}".format(bbox)
map = mapscript.mapObj('../../tests/test.map') map.setMetaData( "ows_onlineresource", "http://dummy.org/" ) ows_req = mapscript.OWSRequest() ows_req.type = mapscript.MS_GET_REQUEST ows_req.setParameter( "SERVICE", "WMS" ); ows_req.setParameter( "VERSION", "1.1.0" ); ows_req.setParameter( "REQUEST", "GetCapabilities" ); mapscript.msIO_installStdoutToBuffer() dispatch_status = map.OWSDispatch(ows_req) if dispatch_status: status = '200 OK' else: status = '500 Internal Server Error' content_type = mapscript.msIO_stripStdoutBufferContentType() mapscript.msIO_stripStdoutBufferContentHeaders() result = mapscript.msIO_getStdoutBufferBytes() try: # MapServer 6.0: mapscript.msCleanup() except: # MapServer 6.1: mapscript.msCleanup(1) response_headers = [('Content-Type', content_type), ('Content-Length', str(len(result)))]
def get(self, request, *args, **kwargs): """ Html GET method of WmsView. This view renders WMS requests into corresponding responses using the attached WmsMap class. Responses are mainly images and xml files. """ # Create response response = HttpResponse() # Setup wms request object ows_request = mapscript.OWSRequest() # If tile kwargs were provided, add tile parameters to request tileparams = self.tilemode() if tileparams: # Get image format from url format = { '.png': 'image/png', '.jpg': 'image/jpeg' }[self.kwargs.get('format')] # Return empty image if tile cant be found if not self.tile_exists(*tileparams): # Get image type and size imagetype = 'PNG' if format == 'image/png' else 'JPEG' imagesize = 256, 256 response['Content-Type'] = format # Create image and save it to response im = Image.new("RGBA", imagesize, (0, 0, 0, 0)) im.save(response, imagetype) return response else: tilebounds = self.get_tile_bounds(*tileparams) # Get layer name layers = self.kwargs.get('layers') # Setup wms parameter object request_data = { 'SERVICE': 'WMS', 'REQUEST': 'GetMap', 'VERSION': '1.1.1', 'TRANSPARENT': 'true', 'HEIGHT': '256', 'WIDTH': '256', 'SRS': 'EPSG:3857', 'FORMAT': format, 'LAYERS': layers, 'BBOX': tilebounds, } request.GET = dict(request.GET.items() + request_data.items()) # Set ows parameters from request data for param, value in request.GET.items(): ows_request.setParameter(param, value) # Instantiate WmsMap class self.wmsmap = self.map_class(request, **kwargs) # Dynamically use host for declaring service endpoint onlineresource = request.build_absolute_uri().split('?')[0] + '?' self.wmsmap.map_object.setMetaData('wms_onlineresource', onlineresource) # Dispatch map rendering self.wmsmap.map_object.OWSDispatch(ows_request) # Strip buffer from headers mapscript.msIO_stripStdoutBufferContentHeaders() # Store contenttype contenttype = mapscript.msIO_stripStdoutBufferContentType() # Write data to response response.write(mapscript.msIO_getStdoutBufferBytes()) # Set contenttype response['Content-Type'] = contenttype return response