Exemple #1
0
def element_to_string(element, encoding=None, xml_declaration=False):
    """
    Returns a string from a XML object

    Parameters
    ----------
    - element: etree Element
    - encoding (optional): encoding in string form. 'utf-8', 'ISO-8859-1', etc.
    - xml_declaration (optional): whether to include xml declaration

    """

    output = None

    if encoding is None:
        encoding = "ISO-8859-1"

    if etree.__name__ == 'lxml.etree':
        if xml_declaration:
            if encoding in ['unicode', 'utf-8']:
                output = '<?xml version="1.0" encoding="utf-8" standalone="no"?>\n%s' % \
                       etree.tostring(element, encoding='unicode')
            else:
                output = etree.tostring(element, encoding=encoding, xml_declaration=True)
        else:
                output = etree.tostring(element)
    else:
        if xml_declaration:
            output = '<?xml version="1.0" encoding="%s" standalone="no"?>\n%s' % (encoding,
                   etree.tostring(element, encoding=encoding))
        else:
            output = etree.tostring(element)

    return output
Exemple #2
0
    def getrecordbyid(self, ids=[], esn="full", outputschema="gmd", **kw):
        from owslib.csw import namespaces
        csw = self._ows(**kw)
        kwa = {
            "esn": esn,
            "outputschema": namespaces[outputschema],
            }
        # Ordinary Python version's don't support the metadata argument
        log.info('Making CSW request: getrecordbyid %r %r', ids, kwa)
        csw.getrecordbyid(ids, **kwa)
        if csw.exceptionreport:
            err = 'Error getting record by id: %r' % \
                  csw.exceptionreport.exceptions
            #log.error(err)
            raise CswError(err)
        if not csw.records:
            return
        record = self._xmd(csw.records.values()[0])

        ## strip off the enclosing results container, we only want the metadata
        #md = csw._exml.find("/gmd:MD_Metadata")#, namespaces=namespaces)
        # Ordinary Python version's don't support the metadata argument
        md = csw._exml.find("/{http://www.isotc211.org/2005/gmd}MD_Metadata")
        mdtree = etree.ElementTree(md)
        try:
            record["xml"] = etree.tostring(mdtree, pretty_print=True, xml_declaration=True)
        except TypeError:
            # API incompatibilities between different flavours of elementtree
            try:
                record["xml"] = etree.tostring(mdtree)
            except AssertionError:
                record["xml"] = etree.tostring(md)
        record["tree"] = mdtree
        return record
Exemple #3
0
    def __init__(self, md):
        """constructor"""

        if hasattr(md, "getroot"):  # standalone document
            self.xml = etree.tostring(md.getroot())
        else:  # part of a larger document
            self.xml = etree.tostring(md)

        self.header = HeaderSection(md)
        self.data = DataSection(md)
Exemple #4
0
    def __init__(self, md):
        if hasattr(md, 'getroot'):  # standalone document
            self.xml = etree.tostring(md.getroot())
        else:  # part of a larger document
            self.xml = etree.tostring(md)

        self.idinfo = Idinfo(md)
        self.eainfo = Eainfo(md)
        self.distinfo = Distinfo(md)
        self.metainfo = Metainfo(md)

        if self.idinfo.datasetid:
            self.identifier = self.idinfo.datasetid
Exemple #5
0
    def describerecord(self, typename='csw:Record', format=outputformat):
        """

        Construct and process DescribeRecord request

        Parameters
        ----------

        - typename: the typename to describe (default is 'csw:Record')
        - format: the outputFormat (default is 'application/xml')
 
        """

        # construct request

        nsmap = {
                'csw' : ns.get_namespace('csw'),
                'xsi' : ns.get_namespace('xsi'),
            }

        node0 = setrootelement('csw:DescribeRecord', nsmap)
        node0.set('outputFormat', format)
        node0.set('schemaLanguage',ns.get_namespace('xs2'))
        node0.set('service', self.service)
        node0.set('version', self.version)
        node0.set(nsp('xsi:schemaLocation'), schema_location)
        etree.SubElement(node0, nsp('csw:TypeName')).text = typename
        self.request = xml2string(etree.tostring(node0))

        self._invoke()
Exemple #6
0
    def getrecordbyid(self, id=[], esn='full', outputschema=namespaces['csw'], format=outputformat):
        """

        Construct and process a GetRecordById request

        Parameters
        ----------

        - id: the list of Ids
        - esn: the ElementSetName 'full', 'brief' or 'summary' (default is 'full')
        - outputschema: the outputSchema (default is 'http://www.opengis.net/cat/csw/2.0.2')
        - format: the outputFormat (default is 'application/xml')

        """

        # construct request 
        node0 = self._setrootelement('csw:GetRecordById')
        node0.set('outputSchema', outputschema)
        node0.set('outputFormat', format)
        node0.set('version', self.version)
        node0.set('service', self.service)
        node0.set(util.nspath_eval('xsi:schemaLocation', namespaces), schema_location)
        for i in id:
            etree.SubElement(node0, util.nspath_eval('csw:Id', namespaces)).text = i
        etree.SubElement(node0, util.nspath_eval('csw:ElementSetName', namespaces)).text = esn
        self.request = util.xml2string(etree.tostring(node0))

        self._invoke()
 
        if self.exceptionreport is None:
            self.results = {}
            self.records = {}
            self._parserecords(outputschema, esn)
Exemple #7
0
    def _invoke(self):
        # do HTTP request

        if isinstance(self.request, basestring):  # GET KVP
            self.response = urlopen(self.request, timeout=self.timeout).read()
        else:
            self.request = cleanup_namespaces(self.request)
            self.request = util.xml2string(etree.tostring(self.request))

            self.response = util.http_post(self.url, self.request, self.lang, self.timeout)

        # parse result see if it's XML
        self._exml = etree.parse(StringIO.StringIO(self.response))

        # it's XML.  Attempt to decipher whether the XML response is CSW-ish """
        valid_xpaths = [
            util.nspath_eval("ows:ExceptionReport", namespaces),
            util.nspath_eval("csw:Capabilities", namespaces),
            util.nspath_eval("csw:DescribeRecordResponse", namespaces),
            util.nspath_eval("csw:GetDomainResponse", namespaces),
            util.nspath_eval("csw:GetRecordsResponse", namespaces),
            util.nspath_eval("csw:GetRecordByIdResponse", namespaces),
            util.nspath_eval("csw:HarvestResponse", namespaces),
            util.nspath_eval("csw:TransactionResponse", namespaces),
        ]

        if self._exml.getroot().tag not in valid_xpaths:
            raise RuntimeError, "Document is XML, but not CSW-ish"

        # check if it's an OGC Exception
        val = self._exml.find(util.nspath_eval("ows:Exception", namespaces))
        if val is not None:
            raise ows.ExceptionReport(self._exml, self.owscommon.namespace)
        else:
            self.exceptionreport = None
Exemple #8
0
    def getrecordbyid(self, id=[], esn="full", outputschema=namespaces["csw"], format=outputformat):
        """

        Construct and process a GetRecordById request

        Parameters
        ----------

        - id: the list of Ids
        - esn: the ElementSetName 'full', 'brief' or 'summary' (default is 'full')
        - outputschema: the outputSchema (default is 'http://www.opengis.net/cat/csw/2.0.2')
        - format: the outputFormat (default is 'application/xml')

        """

        # construct request
        node0 = etree.Element(util.nspath("GetRecordById", namespaces["csw"]))
        node0.set("outputSchema", outputschema)
        node0.set("outputFormat", format)
        node0.set("version", self.version)
        node0.set("service", self.service)
        node0.set(util.nspath("schemaLocation", namespaces["xsi"]), schema_location)
        for i in id:
            etree.SubElement(node0, util.nspath("Id", namespaces["csw"])).text = i
        etree.SubElement(node0, util.nspath("ElementSetName", namespaces["csw"])).text = esn
        self.request = util.xml2string(etree.tostring(node0))

        self._invoke()

        if self.exceptionreport is None:
            self.results = {}
            self.records = {}
            self._parserecords(outputschema, esn)
def _generateRequest(dataSetURI, algorithm, method, varID, verbose):
    """
    Takes a dataset uri, algorithm, method, and datatype. This function will generate a simple XML document
    to make the request specified. (Only works for ListOpendapGrids and GetGridTimeRange). 
    
    Will return a list containing the info requested for (either data types or time range).
    """
    
    POST = WebProcessingService(WPS_Service, verbose=verbose)
    
    xmlGen = gdpXMLGenerator()
    root = xmlGen.getXMLRequestTree(dataSetURI, algorithm, method, varID, verbose)           
     
    request = etree.tostring(root)
    
    execution = POST.execute(None, [], request=request)
    
    _execute_request._check_for_execution_errors(execution)
    
    if method == 'getDataSetTime':
        seekterm = '{xsd/gdptime-1.0.xsd}time'
    elif method == 'getDataType':
        seekterm = '{xsd/gdpdatatypecollection-1.0.xsd}name'
    elif method == 'getDataLongName':
        seekterm = '{xsd/gdpdatatypecollection-1.0.xsd}description'
    elif method == 'getDataUnits':
        seekterm = '{xsd/gdpdatatypecollection-1.0.xsd}unitsstring'
    
    return _parseXMLNodesForTagText(execution.response, seekterm)
Exemple #10
0
def cleanup_namespaces(element):
    """ Remove unused namespaces from an element """
    if etree.__name__ == 'lxml.etree':
        etree.cleanup_namespaces(element)
        return element
    else:
        return etree.fromstring(etree.tostring(element))
Exemple #11
0
def uploadShapeFile(filePath):
    """
    Given a file, this function encodes the file and uploads it onto geoserver.
    """

    # encodes the file, opens it, reads it, and closes it
    # returns a filename in form of: filename_copy.zip
    filePath = _encodeZipFolder(filePath)
    if filePath is None:
        return

    filehandle = open(filePath, 'r')
    filedata = filehandle.read()
    filehandle.close()
    os.remove(filePath)  # deletes the encoded file

    # this if for naming the file on geoServer
    filename = filePath.split("/")
    # gets rid of filepath, keeps only filename eg: file.zip
    filename = filename[len(filename) - 1]
    filename = filename.replace("_copy.zip", "")

    xml_gen = gdpXMLGenerator()
    root = xml_gen.getUploadXMLtree(filename, upload_URL, filedata)

    # now we have a complete XML upload request
    upload_request = etree.tostring(root)
    post = WebProcessingService(WPS_Service)
    execution = post.execute(None, [], request=upload_request)
    monitorExecution(execution)
    return "upload:" + filename
Exemple #12
0
    def describerecord(self, typename='csw:Record', format=outputformat):
        """

        Construct and process DescribeRecord request

        Parameters
        ----------

        - typename: the typename to describe (default is 'csw:Record')
        - format: the outputFormat (default is 'application/xml')
 
        """

        # construct request
        node0 = etree.Element(util.nspath('DescribeRecord', namespaces['csw']))
        node0.set('service', self.service)
        node0.set('version', self.version)
        node0.set('outputFormat', format)
        node0.set('schemaLanguage', namespaces['xs2'])
        node0.set(util.nspath('schemaLocation', namespaces['xsi']), schema_location)
        etree.SubElement(node0, util.nspath('TypeName', namespaces['csw'])).text = typename
        self.request = util.xml2string(etree.tostring(node0))

        # invoke
        self.response = util.http_post(self.url, self.request, self.lang)
Exemple #13
0
 def _generateRequest(self, dataSetURI, algorithm, method, varID=None, verbose=False):
     """
     Takes a dataset uri, algorithm, method, and datatype. This function will generate a simple XML document
     to make the request specified. (Only works for ListOpendapGrids and GetGridTimeRange). 
     
     Will return a list containing the info requested for (either data types or time range).
     """
     
     wps_Service = 'http://cida.usgs.gov/gdp/utility/WebProcessingService'
     POST = WebProcessingService(wps_Service, verbose=False)
     
     xmlGen = gdpXMLGenerator()
     root = xmlGen.getXMLRequestTree(dataSetURI, algorithm, method, varID, verbose)           
     
     # change standard output to not display waiting status
     if not verbose:
         old_stdout = sys.stdout
         result = StringIO()
         sys.stdout = result   
     request = etree.tostring(root)
     
     execution = POST.execute(None, [], request=request)
     if method == 'getDataSetTime':
         seekterm = 'time'
     else:
         seekterm = 'name'
     if not verbose:
         sys.stdout = old_stdout
     return self._parseXMLNodesForTagText(execution.response, seekterm)
def test_wps_request3():
    # Supply process input arguments
    polygon = [(-102.8184, 39.5273), (-102.8184, 37.418), (-101.2363, 37.418),
               (-101.2363, 39.5273), (-102.8184, 39.5273)]
    featureCollection = GMLMultiPolygonFeatureCollection([polygon])
    processid = 'gov.usgs.cida.gdp.wps.algorithm.FeatureWeightedGridStatisticsAlgorithm'
    inputs = [("FEATURE_ATTRIBUTE_NAME", "the_geom"),
              ("DATASET_URI", "dods://igsarm-cida-thredds1.er.usgs.gov:8080/thredds/dodsC/dcp/conus_grid.w_meta.ncml"),
              ("DATASET_ID", "ccsm3_a1b_tmax"),
              ("TIME_START", "1960-01-01T00:00:00.000Z"),
              ("TIME_END", "1960-12-31T00:00:00.000Z"),
              ("REQUIRE_FULL_COVERAGE", "true"),
              ("DELIMITER", "COMMA"),
              ("STATISTICS", "MEAN"),
              ("STATISTICS", "MINIMUM"),
              ("STATISTICS", "MAXIMUM"),
              ("STATISTICS", "WEIGHT_SUM"),
              ("STATISTICS", "VARIANCE"),
              ("STATISTICS", "STD_DEV"),
              ("STATISTICS", "COUNT"),
              ("GROUP_BY", "STATISTIC"),
              ("SUMMARIZE_TIMESTEP", "false"),
              ("SUMMARIZE_FEATURE_ATTRIBUTE", "false"),
              ("FEATURE_COLLECTION", featureCollection)]
    output = "OUTPUT"
    # build XML request for WPS process execution
    execution = WPSExecution()
    requestElement = execution.buildRequest(processid, inputs, output=output)
    request = etree.tostring(requestElement)
    # Compare to cached XML request
    _request = open(resource_file('wps_USGSExecuteRequest3.xml'), 'rb').read()
    assert compare_xml(request, _request) is True
Exemple #15
0
    def __init__(self, url, lang='en-US', version='2.0.2', timeout=10, skip_caps=False):
        """

        Construct and process a GetCapabilities request

        Parameters
        ----------

        - url: the URL of the CSW
        - lang: the language (default is 'en-US')
        - version: version (default is '2.0.2')
        - timeout: timeout in seconds
        - skip_caps: whether to skip GetCapabilities processing on init (default is False)

        """

        self.url = url
        self.lang = lang
        self.version = version
        self.timeout = timeout
        self.service = 'CSW'
        self.exceptionreport = None

        if not skip_caps:  # process GetCapabilities
            # construct request
            nsmap = {
                'csw' : ns.get_namespace('csw'),
                'ows' : ns.get_versioned_namespace('ows',_ows_version),
                'xsi' : ns.get_namespace('xsi'),
            }

            node0 = setrootelement('csw:GetCapabilities', nsmap)

            node0.set('service', self.service)
            node0.set(nsp('xsi:schemaLocation'), schema_location)

            tmp = etree.SubElement(node0, nsp_ows('ows:AcceptVersions'))
            etree.SubElement(tmp, nsp_ows('ows:Version')).text = self.version
            tmp2 = etree.SubElement(node0, nsp_ows('ows:AcceptFormats'))
            etree.SubElement(tmp2, nsp_ows('ows:OutputFormat')).text = outputformat
            self.request = xml2string(etree.tostring(node0))
    
            self._invoke()
    
            if self.exceptionreport is None:
                # ServiceIdentification
                val = self._exml.find(nsp_ows('ows:ServiceIdentification'))
                self.identification=ows.ServiceIdentification(val,ns.get_versioned_namespace('ows', _ows_version))
                # ServiceProvider
                val = self._exml.find(nsp_ows('ows:ServiceProvider'))
                self.provider=ows.ServiceProvider(val)
                # ServiceOperations metadata 
                op = self._exml.find(nsp_ows('ows:OperationsMetadata'))
                self.operations = ows.OperationsMetadata(op, ns.get_versioned_namespace('ows', _ows_version)).operations
        
                # FilterCapabilities
                val = self._exml.find(nsp('ogc:Filter_Capabilities'))
                self.filters=fes.FilterCapabilities(val)
Exemple #16
0
    def harvest(self, source, resourcetype, resourceformat=None, harvestinterval=None, responsehandler=None):
        """

        Construct and process a Harvest request

        Parameters
        ----------

        - source: a URI to harvest
        - resourcetype: namespace identifying the type of resource
        - resourceformat: MIME type of the resource
        - harvestinterval: frequency of harvesting, in ISO8601
        - responsehandler: endpoint that CSW should responsd to with response

        """

        # construct request
        node0 = etree.Element(util.nspath('Harvest', namespaces['csw']))
        node0.set('version', self.version)
        node0.set('service', self.service)
        node0.set(util.nspath('schemaLocation', namespaces['xsi']), schema_location)
        etree.SubElement(node0, util.nspath('Source', namespaces['csw'])).text = source
        etree.SubElement(node0, util.nspath('ResourceType', namespaces['csw'])).text = resourcetype
        if resourceformat is not None:
            etree.SubElement(node0, util.nspath('ResourceFormat', namespaces['csw'])).text = resourceformat
        if harvestinterval is not None:
            etree.SubElement(node0, util.nspath('HarvestInterval', namespaces['csw'])).text = harvestinterval
        if responsehandler is not None:
            etree.SubElement(node0, util.nspath('ResponseHandler', namespaces['csw'])).text = responsehandler
       
        self.request = util.xml2string(etree.tostring(node0))

        self.response = util.http_post(self.url, self.request, self.lang)

        # parse result
        self._response = etree.parse(StringIO.StringIO(self.response))

        # check for exceptions
        self._isexception(self._response, self.owscommon.namespace)

        self.results = {}

        if self.exceptionreport is None:
            val = self._response.find(util.nspath('Acknowledgement', namespaces['csw']))
            if util.testXMLValue(val) is not None:
                ts = val.attrib.get('timeStamp')
                self.timestamp = util.testXMLValue(ts, True)
                id = val.find(util.nspath('RequestId', namespaces['csw']))
                self.id = util.testXMLValue(id) 
            else:
                self._parsetransactionsummary()

            self.results['inserted'] = []

            for i in self._response.findall(util.nspath('TransactionResponse/InsertResult', namespaces['csw'])):
                for j in i.findall(util.nspath('BriefRecord', namespaces['csw']) + '/' + util.nspath('identifier', namespaces['dc'])):
                    self.results['inserted'].append(util.testXMLValue(j))
Exemple #17
0
    def process_spreadsheet(self):
        """
        Iterates through the inputted CSV, detects NEW_ fields, and executes
        update functions as needed.
        """
        for row in self.reader:
            log.debug(row)
            self.row_changed = False
            self.tree_changed = False
            self.record_etree = False

            self.row = row
            if "uuid" in row:
                self.uuid = row["uuid"]
            elif "UUID" in row:
                self.uuid = row["UUID"]
            else:
                sys.exit("No uuid column found. Must be named uuid or UUID.")

            if self.uuid == "DELETED" or self.uuid == "SKIP":
                continue

            log.debug(self.uuid)

            if "schema" in row:
                self.schema = row["schema"]
            else:
                log.info("No 'schema' column. Defaulting to iso19139.")
                self.schema = "iso19139"

            self.get_record_by_id(self.uuid)
            self.record_etree = self._get_etree_for_record(self.uuid)

            for field in self.fieldnames:
                if field.lower() == "uuid":
                    continue

                if field in self.field_handlers[self.schema] and field in row:
                    self.field_handlers[self.schema][field].__call__(
                        self.uuid,
                        row[field]
                    )

            if self.uuid in self.records and self.tree_changed:
                log.debug("replacing entire XML")
                new_xml = etree.tostring(self.record_etree)
                self.row_changed = True
                self.csw.transaction(
                    ttype="update",
                    typename='csw:Record',
                    record=new_xml,
                    identifier=self.uuid)
                time.sleep(2)
               #  self.update_timestamp(self.uuid)
                log.info("Updated: {uuid}\n\n".format(uuid=self.uuid))
            else:
                log.info("No change: {uuid}\n\n".format(uuid=self.uuid))
Exemple #18
0
    def __init__(self, url, lang='en-US', version='2.0.2'):
        """

        Construct and process a GetCapabilities request

        Parameters
        ----------

        - url: the URL of the CSW
        - lang: the language (default is 'en-US')
        - version: version (default is '2.0.2')

        """

        self.url = url
        self.lang = lang
        self.version = version
        self.service = 'CSW'
        self.exceptionreport = None
        self.owscommon = OwsCommon('1.0.0')

        # construct request
        node0 = etree.Element(util.nspath('GetCapabilities', namespaces['csw']))
        node0.set('service', self.service)
        node0.set(util.nspath('schemaLocation', namespaces['xsi']), schema_location)
        tmp = etree.SubElement(node0, util.nspath('AcceptVersions', namespaces['ows']))
        etree.SubElement(tmp, util.nspath('Version', namespaces['ows'])).text = self.version
        tmp2 = etree.SubElement(node0, util.nspath('AcceptFormats', namespaces['ows']))
        etree.SubElement(tmp2, util.nspath('OutputFormat', namespaces['ows'])).text = outputformat
        self.request = util.xml2string(etree.tostring(node0))

        # invoke
        self.response = util.http_post(self.url, self.request, self.lang)

        # parse result
        self._capabilities = etree.parse(StringIO.StringIO(self.response))

        # check for exceptions
        self._isexception(self._capabilities, self.owscommon.namespace)

        if self.exceptionreport is None:
            # ServiceIdentification
            val = self._capabilities.find(util.nspath('ServiceIdentification', namespaces['ows']))
            self.identification=ServiceIdentification(val,self.owscommon.namespace)
            # ServiceProvider
            val = self._capabilities.find(util.nspath('ServiceProvider', namespaces['ows']))
            self.provider=ServiceProvider(val,self.owscommon.namespace)
            # ServiceOperations metadata 
            self.operations=[]
            for elem in self._capabilities.findall(util.nspath('OperationsMetadata/Operation', namespaces['ows'])):
                self.operations.append(OperationsMetadata(elem, self.owscommon.namespace))
    
            # FilterCapabilities
            val = self._capabilities.find(util.nspath('Filter_Capabilities', namespaces['ogc']))
            self.filters=FilterCapabilities(val)
Exemple #19
0
    def test_wfs_build_getfeature_request_bbox_filter(self):
        """Test the owsutil.wfs_build_getfeature_request method with an
        attribute filter, a box and a geometry_column.

        Test whether the XML of the WFS GetFeature call is generated correctly.

        """
        query = PropertyIsEqualTo(propertyname='gemeente',
                                  literal='Herstappe')
        filter_request = FilterRequest()
        filter_request = filter_request.setConstraint(query)
        try:
            filter_request = etree.tostring(filter_request,
                                            encoding='unicode')
        except LookupError:
            # Python2.7 without lxml uses 'utf-8' instead.
            filter_request = etree.tostring(filter_request,
                                            encoding='utf-8')

        xml = owsutil.wfs_build_getfeature_request(
            'dov-pub:Boringen', filter=filter_request,
            location=Within(Box(151650, 214675, 151750, 214775)),
            geometry_column='geom')
        assert clean_xml(etree.tostring(xml).decode('utf8')) == clean_xml(
            '<wfs:GetFeature xmlns:wfs="http://www.opengis.net/wfs" '
            'xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" '
            'service="WFS" version="1.1.0" '
            'xsi:schemaLocation="http://www.opengis.net/wfs '
            'http://schemas.opengis.net/wfs/1.1.0/wfs.xsd"> <wfs:Query '
            'typeName="dov-pub:Boringen"> <ogc:Filter> <ogc:And> '
            '<ogc:PropertyIsEqualTo> '
            '<ogc:PropertyName>gemeente</ogc:PropertyName> '
            '<ogc:Literal>Herstappe</ogc:Literal> </ogc:PropertyIsEqualTo> '
            '<ogc:Within> <ogc:PropertyName>geom</ogc:PropertyName> '
            '<gml:Envelope xmlns:gml="http://www.opengis.net/gml" '
            'srsDimension="2" '
            'srsName="http://www.opengis.net/gml/srs/epsg.xml#31370"> '
            '<gml:lowerCorner>151650.000000 214675.000000</gml:lowerCorner> '
            '<gml:upperCorner>151750.000000 214775.000000</gml:upperCorner> '
            '</gml:Envelope> </ogc:Within> </ogc:And> </ogc:Filter> '
            '</wfs:Query> </wfs:GetFeature>')
    def xml(self):
        io_doc_tree = self._getRootElement()

        offering = SOSElement('offering', nsmap=namespaces_io)
        offering.text = self._obs['offering']

        io_doc_tree.append(offering)

        io_doc_tree.append(self._getObservationElement())

        #wmc_doc_tree.append(self._getLayerListElement())
        return etree.tostring(io_doc_tree, pretty_print = True)
Exemple #21
0
 def wrapper(self, propertyname=None, literal=None):
     if propertyname and literal:
         tag = method(
             self,
             propertyname=self.data2literal(propertyname),
             literal=self.data2literal(literal),
         )
         return etree.tostring(tag.toXML()).decode("utf-8")
     elif not propertyname and not literal:
         return "value"
     else:
         raise Exception("Filter literal error")
Exemple #22
0
    def __init__(self, url, lang='en-US', version='2.0.2', timeout=10, skip_caps=False):
        """

        Construct and process a GetCapabilities request

        Parameters
        ----------

        - url: the URL of the CSW
        - lang: the language (default is 'en-US')
        - version: version (default is '2.0.2')
        - timeout: timeout in seconds
        - skip_caps: whether to skip GetCapabilities processing on init (default is False)

        """

        self.url = url
        self.lang = lang
        self.version = version
        self.timeout = timeout
        self.service = 'CSW'
        self.exceptionreport = None
        self.owscommon = ows.OwsCommon('1.0.0')

        if not skip_caps:  # process GetCapabilities
            # construct request
            node0 = self._setrootelement('csw:GetCapabilities')
            node0.set('service', self.service)
            node0.set(util.nspath_eval('xsi:schemaLocation', namespaces), schema_location)
            tmp = etree.SubElement(node0, util.nspath_eval('ows:AcceptVersions', namespaces))
            etree.SubElement(tmp, util.nspath_eval('ows:Version', namespaces)).text = self.version
            tmp2 = etree.SubElement(node0, util.nspath_eval('ows:AcceptFormats', namespaces))
            etree.SubElement(tmp2, util.nspath_eval('ows:OutputFormat', namespaces)).text = outputformat
            self.request = util.xml2string(etree.tostring(node0))
    
            self._invoke()
    
            if self.exceptionreport is None:
                # ServiceIdentification
                val = self._exml.find(util.nspath_eval('ows:ServiceIdentification', namespaces))
                self.identification=ows.ServiceIdentification(val,self.owscommon.namespace)
                # ServiceProvider
                val = self._exml.find(util.nspath_eval('ows:ServiceProvider', namespaces))
                self.provider=ows.ServiceProvider(val,self.owscommon.namespace)
                # ServiceOperations metadata 
                self.operations=[]
                for elem in self._exml.findall(util.nspath_eval('ows:OperationsMetadata/ows:Operation', namespaces)):
                    self.operations.append(ows.OperationsMetadata(elem, self.owscommon.namespace))
        
                # FilterCapabilities
                val = self._exml.find(util.nspath_eval('ogc:Filter_Capabilities', namespaces))
                self.filters=fes.FilterCapabilities(val)
Exemple #23
0
    def __init__(self, url, lang='en-US', version='2.0.2', timeout=10, skip_caps=False):
        """

        Construct and process a GetCapabilities request

        Parameters
        ----------

        - url: the URL of the CSW
        - lang: the language (default is 'en-US')
        - version: version (default is '2.0.2')
        - timeout: timeout in seconds
        - skip_caps: whether to skip GetCapabilities processing on init (default is False)

        """

        self.url = url
        self.lang = lang
        self.version = version
        self.timeout = timeout
        self.service = 'CSW'
        self.exceptionreport = None
        self.owscommon = ows.OwsCommon('1.0.0')

        if not skip_caps:  # process GetCapabilities
            # construct request
            node0 = self._setrootelement('csw:GetCapabilities')
            node0.set('service', self.service)
            node0.set(util.nspath_eval('xsi:schemaLocation', namespaces), schema_location)
            tmp = etree.SubElement(node0, util.nspath_eval('ows:AcceptVersions', namespaces))
            etree.SubElement(tmp, util.nspath_eval('ows:Version', namespaces)).text = self.version
            tmp2 = etree.SubElement(node0, util.nspath_eval('ows:AcceptFormats', namespaces))
            etree.SubElement(tmp2, util.nspath_eval('ows:OutputFormat', namespaces)).text = outputformat
            self.request = util.xml2string(etree.tostring(node0))
    
            self._invoke()
    
            if self.exceptionreport is None:
                # ServiceIdentification
                val = self._exml.find(util.nspath_eval('ows:ServiceIdentification', namespaces))
                self.identification=ows.ServiceIdentification(val,self.owscommon.namespace)
                # ServiceProvider
                val = self._exml.find(util.nspath_eval('ows:ServiceProvider', namespaces))
                self.provider=ows.ServiceProvider(val,self.owscommon.namespace)
                # ServiceOperations metadata 
                self.operations=[]
                for elem in self._exml.findall(util.nspath_eval('ows:OperationsMetadata/ows:Operation', namespaces)):
                    self.operations.append(ows.OperationsMetadata(elem, self.owscommon.namespace))
        
                # FilterCapabilities
                val = self._exml.find(util.nspath_eval('ogc:Filter_Capabilities', namespaces))
                self.filters=fes.FilterCapabilities(val)
Exemple #24
0
def test_wps_request11_bbox():
    processid = "bbox"
    bbox = BoundingBoxDataInput([51.9, 7.0, 53.0, 8.0])
    inputs = [("bbox", bbox)]

    # Build XML request for WPS process execution
    execution = WPSExecution()
    requestElement = execution.buildRequest(processid, inputs)
    request = etree.tostring(requestElement)

    # Compare to cached XML request
    _request = open(resource_file('wps_EmuExecuteRequest11.xml'), 'rb').read()
    assert compare_xml(request, _request) is True
Exemple #25
0
    def test_point(self):
        """Test the default Point type.

        Test whether the generated XML is correct.

        """
        point = Point(110680, 202030)
        xml = point.get_element()

        assert clean_xml(etree.tostring(xml).decode('utf8')) == clean_xml(
            '<gml:Point srsDimension="2" '
            'srsName="http://www.opengis.net/gml/srs/epsg.xml#31370">'
            '<gml:pos>110680.000000 202030.000000</gml:pos></gml:Point>')
Exemple #26
0
def element_to_string(element, encoding=None):
    """
    Returns a string from a XML object

    Parameters
    ----------
    - xml:                 etree Element
    - encoding (optional): encoding in string form. 'utf-8', 'ISO-8859-1', etc.

    """
    if encoding is None:
        encoding = "ISO-8859-1"

    if etree.__name__ == 'lxml.etree':
        if encoding in ['unicode', 'utf-8']:
            return '<?xml version="1.0" encoding="utf-8" standalone="no"?>\n%s' % \
                   etree.tostring(element, encoding='unicode')
        else:
            return etree.tostring(element, encoding=encoding, xml_declaration=True)
    else:
        return '<?xml version="1.0" encoding="%s" standalone="no"?>\n%s' % (encoding,
               etree.tostring(element, encoding=encoding))
Exemple #27
0
def test_wps_request9():
    # Process input/output arguments
    processid = "helloworld"
    inputs = [("user", 'Pingu')]

    # Build XML request for WPS process execution
    execution = WPSExecution()
    requestElement = execution.buildRequest(processid, inputs)
    request = etree.tostring(requestElement)

    # Compare to cached XML request
    _request = open(resource_file('wps_EmuExecuteRequest9.xml'), 'rb').read()
    assert compare_xml(request, _request) is True
Exemple #28
0
    def test_point_wgs84(self):
        """Test the Point type with WGS84 coordinates.

        Test whether the generated XML is correct.

        """
        point = Point(3.8071, 51.1270, epsg=4326)
        xml = point.get_element()

        assert clean_xml(etree.tostring(xml).decode('utf8')) == clean_xml(
            '<gml:Point srsDimension="2" '
            'srsName="http://www.opengis.net/gml/srs/epsg.xml#4326">'
            '<gml:pos>3.807100 51.127000</gml:pos></gml:Point>')
Exemple #29
0
def element_to_string(element, encoding=None):
    """
    Returns a string from a XML object

    Parameters
    ----------
    - xml:                 etree Element
    - encoding (optional): encoding in string form. 'utf-8', 'ISO-8859-1', etc.

    """
    if encoding is None:
        encoding = "ISO-8859-1"

    if etree.__name__ == 'lxml.etree':
        if encoding in ['unicode', 'utf-8']:
            return '<?xml version="1.0" encoding="utf-8" standalone="no"?>\n%s' % \
                   etree.tostring(element, encoding='unicode')
        else:
            return etree.tostring(element, encoding=encoding, xml_declaration=True)
    else:
        return '<?xml version="1.0" encoding="%s" standalone="no"?>\n%s' % (encoding,
               etree.tostring(element, encoding=encoding))
Exemple #30
0
def run_wps(res_ids,gap):

    if (gap ==''):
        gap = "linear"

    # checks if there is two resource IDs
    resources = res_ids.split("_")

    process_id = 'org.n52.wps.server.r.series_gap_filler_3'
    process_input = [('resource_id',str(resources[0])),('fill_function',str(gap))]

    #setting the WPS URL is set in app.py
    url_wps = GapFillerTool.wps_url + '/WebProcessingService'
    my_engine = WebProcessingService(url_wps, verbose=False, skip_caps=True)
    my_process = my_engine.describeprocess(process_id)

    #executing the process..
    # build execution

    execution = WPSExecution(version=my_engine.version, url=my_engine.url, username=my_engine.username,
                             password=my_engine.password, verbose=my_engine.verbose)

    requestElement = execution.buildRequest(process_id, process_input, 'output')

    request = etree.tostring(requestElement)
    #set store executeresponse to false

    request = request.replace('storeExecuteResponse="true"', 'storeExecuteResponse="false"')

    execution = my_engine.execute(process_id, process_input, 'output', request)

    monitorExecution(execution)

    status = execution.status


    # if the status is successful...
    if status == 'ProcessSucceeded':
        outputs = execution.processOutputs
        output0 = outputs[0]
        reference0 = output0.reference

        # retrieve the data from the reference
        output_data = requests.get(reference0)
        resp = HttpResponse(output_data, content_type="application/json")

        return resp

    else:
        return JsonResponse({'status': 'wps request failed'})
Exemple #31
0
def get_krw_wfs_features(typename, extent=None, owmnaam=None):
    wfs = get_krw_wfs()
    kwargs = {'typename': typename, 'outputFormat': 'json'}
    if extent is not None:
        kwargs['bbox'] = [extent[0], extent[2], extent[1], extent[3]]
    if owmnaam is not None:
        if extent is not None:
            raise (Exception('Either supply extent or ownnaam, not both'))
        filter = PropertyIsEqualTo(propertyname='OWMNAAM', literal=owmnaam)
        kwargs['filter'] = etree.tostring(filter.toXML()).decode("utf-8")
    response = wfs.getfeature(**kwargs).read()
    gdf = gpd.GeoDataFrame.from_features(json.loads(response)['features'])
    gdf = gdf.set_index('OWMNAAM')
    return gdf
Exemple #32
0
 def filter_comp_like(self, propertyname=None, literal=None):
     if propertyname and literal:
         tag = PropertyIsLike(
             propertyname=self.data2literal(propertyname),
             literal=self.data2literal(literal),
             wildCard="*",
             singleChar=".",
             escapeChar="!",
         )
         return etree.tostring(tag.toXML()).decode("utf-8")
     elif not propertyname and not literal:
         return "value"
     else:
         raise Exception("Filter error")
Exemple #33
0
    def __init__(self, url, lang="en-US", version="2.0.2", timeout=10):
        """

        Construct and process a GetCapabilities request

        Parameters
        ----------

        - url: the URL of the CSW
        - lang: the language (default is 'en-US')
        - version: version (default is '2.0.2')
        - timeout: timeout in seconds

        """

        self.url = url
        self.lang = lang
        self.version = version
        self.timeout = timeout
        self.service = "CSW"
        self.exceptionreport = None
        self.owscommon = OwsCommon("1.0.0")

        # construct request
        node0 = etree.Element(util.nspath("GetCapabilities", namespaces["csw"]))
        node0.set("service", self.service)
        node0.set(util.nspath("schemaLocation", namespaces["xsi"]), schema_location)
        tmp = etree.SubElement(node0, util.nspath("AcceptVersions", namespaces["ows"]))
        etree.SubElement(tmp, util.nspath("Version", namespaces["ows"])).text = self.version
        tmp2 = etree.SubElement(node0, util.nspath("AcceptFormats", namespaces["ows"]))
        etree.SubElement(tmp2, util.nspath("OutputFormat", namespaces["ows"])).text = outputformat
        self.request = util.xml2string(etree.tostring(node0))

        self._invoke()

        if self.exceptionreport is None:
            # ServiceIdentification
            val = self._exml.find(util.nspath("ServiceIdentification", namespaces["ows"]))
            self.identification = ServiceIdentification(val, self.owscommon.namespace)
            # ServiceProvider
            val = self._exml.find(util.nspath("ServiceProvider", namespaces["ows"]))
            self.provider = ServiceProvider(val, self.owscommon.namespace)
            # ServiceOperations metadata
            self.operations = []
            for elem in self._exml.findall(util.nspath("OperationsMetadata/Operation", namespaces["ows"])):
                self.operations.append(OperationsMetadata(elem, self.owscommon.namespace))

            # FilterCapabilities
            val = self._exml.find(util.nspath("Filter_Capabilities", namespaces["ogc"]))
            self.filters = FilterCapabilities(val)
Exemple #34
0
def test_wps_request5():
    # Process input/output arguments
    processid = "reprojectCoords"
    inputs = [("coords", "http://rsg.pml.ac.uk/wps/testdata/coords.txt"),
              ("outputSRS", "EPSG:32630"), ("inputSRS", "EPSG:4326")]

    # build XML request for WPS process execution
    execution = WPSExecution()
    requestElement = execution.buildRequest(processid, inputs)
    request = etree.tostring(requestElement)

    # Compare to cached XML request
    _request = open(resource_file('wps_PMLExecuteRequest5.xml'), 'rb').read()
    assert compare_xml(request, _request) is True
Exemple #35
0
    def getdomain(self, dname, dtype='parameter'):
        """

        Construct and process a GetDomain request

        Parameters
        ----------

        - dname: the value of the Parameter or Property to query
        - dtype: whether to query a parameter (parameter) or property (property)

        """

        # construct request
        dtypename = 'ParameterName'
        node0 = etree.Element(util.nspath_eval('csw:GetDomain', namespaces))
        node0.set('service', self.service)
        node0.set('version', self.version)
        node0.set(util.nspath_eval('xsi:schemaLocation', namespaces),
                  schema_location)
        if dtype == 'property':
            dtypename = 'PropertyName'
        etree.SubElement(node0,
                         util.nspath_eval('csw:%s' % dtypename,
                                          namespaces)).text = dname
        self.request = util.xml2string(etree.tostring(node0))

        self._invoke()

        if self.exceptionreport is None:
            self.results = {}

            val = self._exml.find(
                util.nspath_eval('csw:DomainValues',
                                 namespaces)).attrib.get('type')
            self.results['type'] = util.testXMLValue(val, True)

            val = self._exml.find(
                util.nspath_eval('csw:DomainValues/csw:%s' % dtypename,
                                 namespaces))
            self.results[dtype] = util.testXMLValue(val)

            # get the list of values associated with the Domain
            self.results['values'] = []

            for f in self._exml.findall(
                    util.nspath_eval(
                        'csw:DomainValues/csw:ListOfValues/csw:Value',
                        namespaces)):
                self.results['values'].append(util.testXMLValue(f))
Exemple #36
0
    def test_wfs_build_getfeature_request_sortby_multi(self):
        """Test the owsutil.wfs_build_getfeature_request method with a
        sortby containing multiple sort properties.

        Test whether the XML of the WFS GetFeature call is generated correctly.

        """
        sort_by = SortBy([
            SortProperty('diepte_tot_m', 'DESC'),
            SortProperty('datum_aanvang', 'ASC')
        ])

        try:
            sort_by = etree.tostring(sort_by.toXML(), encoding='unicode')
        except LookupError:
            # Python2.7 without lxml uses 'utf-8' instead.
            sort_by = etree.tostring(sort_by.toXML(), encoding='utf-8')

        xml = owsutil.wfs_build_getfeature_request(
            'dov-pub:Boringen',
            propertyname=['fiche', 'diepte_tot_m'],
            sort_by=sort_by)

        assert clean_xml(etree.tostring(xml).decode('utf8')) == clean_xml(
            '<wfs:GetFeature xmlns:wfs="http://www.opengis.net/wfs" '
            'xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" '
            'service="WFS" version="1.1.0" '
            'xsi:schemaLocation="http://www.opengis.net/wfs '
            'http://schemas.opengis.net/wfs/1.1.0/wfs.xsd"><wfs:Query '
            'typeName="dov-pub:Boringen"><wfs:PropertyName>fiche</wfs'
            ':PropertyName><wfs:PropertyName>diepte_tot_m</wfs:PropertyName'
            '><ogc:Filter/><ogc:SortBy><ogc:SortProperty><ogc:PropertyName'
            '>diepte_tot_m</ogc:PropertyName><ogc:SortOrder>DESC</ogc'
            ':SortOrder></ogc:SortProperty><ogc:SortProperty><ogc'
            ':PropertyName>datum_aanvang</ogc:PropertyName><ogc:SortOrder>ASC'
            '</ogc:SortOrder></ogc:SortProperty></ogc:SortBy></wfs:Query>'
            '</wfs:GetFeature>')
Exemple #37
0
    def __init__(self, ft=None):
        if ft is None:
            self.xml = None
            self.identifier = None
            self.typename = None
            self.definition = None
            self.isabstract = None
            self.aliases = []
            self.attributes = []
        else:
            if hasattr(ft, 'getroot'):  # standalone document
                self.xml = etree.tostring(ft.getroot())
            else:  # part of a larger document
                self.xml = etree.tostring(ft)

            val = ft.attrib['uuid']
            self.identifier = util.testXMLValue(val, attrib=True)

            val = ft.find(util.nspath_eval('gfc:typeName/gco:LocalName', namespaces))
            self.typename = util.testXMLValue(val)

            val = ft.find(util.nspath_eval('gfc:definition/gco:CharacterString', namespaces))
            self.definition = util.testXMLValue(val)

            self.isabstract = None
            val = ft.find(util.nspath_eval('gfc:isAbstract/gco:Boolean', namespaces))
            val = util.testXMLValue(val)
            if val is not None:
                self.isabstract = util.getTypedValue('boolean', val)

            self.aliases = []
            for i in ft.findall(util.nspath_eval('gfc:aliases/gco:LocalName', namespaces)):
                self.aliases.append(util.testXMLValue(i))

            self.attributes = []
            for i in ft.findall(util.nspath_eval('gfc:carrierOfCharacteristics/gfc:FC_FeatureAttribute', namespaces)):
                self.attributes.append(FC_FeatureAttribute(i))
def test_wps_request5():
    # Process input/output arguments
    processid = "reprojectCoords"
    inputs = [("coords", "http://rsg.pml.ac.uk/wps/testdata/coords.txt"),
              ("outputSRS", "EPSG:32630"),
              ("inputSRS", "EPSG:4326")]

    # build XML request for WPS process execution
    execution = WPSExecution()
    requestElement = execution.buildRequest(processid, inputs)
    request = etree.tostring(requestElement)

    # Compare to cached XML request
    _request = open(resource_file('wps_PMLExecuteRequest5.xml'), 'rb').read()
    assert compare_xml(request, _request) is True
Exemple #39
0
    def test_box(self):
        """Test the default Box type.

        Test whether the generated XML is correct.

        """
        box = Box(94720, 186910, 112220, 202870)
        xml = box.get_element()

        assert clean_xml(etree.tostring(xml).decode('utf8')) == clean_xml(
            '<gml:Envelope srsDimension="2" '
            'srsName="http://www.opengis.net/gml/srs/epsg.xml#31370">'
            '<gml:lowerCorner>94720.000000 186910.000000</gml:lowerCorner>'
            '<gml:upperCorner>112220.000000 202870.000000</gml:upperCorner>'
            '</gml:Envelope>')
def test_wps_request6():
    # Process input/output arguments

    processid = "v.net.path"
    inputs = [("input", "http://rsg.pml.ac.uk/wps/example/graph.gml"),
              ("file", "1 -960123.1421801624 4665723.56559387 -101288.65106088226 5108200.011823481")]

    # Build XML request for WPS process execution
    execution = WPSExecution()
    requestElement = execution.buildRequest(processid, inputs)
    request = etree.tostring(requestElement)

    # Compare to cached XML request
    _request = open(resource_file('wps_PMLExecuteRequest6.xml'), 'rb').read()
    assert compare_xml(request, _request) is True
Exemple #41
0
def test_wps_request4():
    # Process input/ouutput arguments
    processid = "reprojectImage"
    inputs = [("inputImage", "http://rsg.pml.ac.uk/wps/testdata/elev_srtm_30m.img"),
              ("outputSRS", "EPSG:4326")]
    output = "outputImage"

    # build XML request for WPS process execution
    execution = WPSExecution()
    requestElement = execution.buildRequest(processid, inputs, output=output)
    request = etree.tostring(requestElement)

    # Compare to cached XML request
    _request = open(resource_file('wps_PMLExecuteRequest4.xml'), 'rb').read()
    assert compare_xml(request, _request) is True
Exemple #42
0
 def filter_comp_beetwen(self, propertyname=None, lower=None, upper=None):
     if propertyname and lower and upper:
         tag = PropertyIsBetween(
             propertyname=self.data2literal(propertyname),
             lower=self.data2literal(lower),
             upper=self.data2literal(upper),
         )
         return etree.tostring(tag.toXML()).decode("utf-8")
     elif not propertyname and not lower and not upper:
         return [
             "lower value",
             "upper value",
         ]
     else:
         raise Exception("Filter error")
Exemple #43
0
    def getrecordbyid(self, ids=[], esn="full", outputschema="gmd", **kw):
        from owslib.csw import namespaces
        csw = self._ows(**kw)
        kwa = {
            "esn": esn,
            "outputschema": namespaces[outputschema],
            }
        # Ordinary Python version's don't support the metadata argument
        log.info('Making CSW request: getrecordbyid %r %r', ids, kwa)
        csw.getrecordbyid(ids, **kwa)
        if csw.exceptionreport:
            err = 'Error getting record by id: %r' % \
                  csw.exceptionreport.exceptions
            #log.error(err)
            raise CswError(err)
        if not csw.records:
            return
        record = self._xmd(list(csw.records.values())[0])

        ## strip off the enclosing results container, we only want the metadata
        #md = csw._exml.find("/gmd:MD_Metadata")#, namespaces=namespaces)
        # Ordinary Python version's don't support the metadata argument
        md = csw._exml.find("/{http://www.isotc211.org/2005/gmd}MD_Metadata")
        mdtree = etree.ElementTree(md)
        try:
            record["xml"] = etree.tostring(mdtree, pretty_print=True, encoding=str)
        except TypeError:
            # API incompatibilities between different flavours of elementtree
            try:
                record["xml"] = etree.tostring(mdtree, pretty_print=True, encoding=str)
            except AssertionError:
                record["xml"] = etree.tostring(md, pretty_print=True, encoding=str)

        record["xml"] = '<?xml version="1.0" encoding="UTF-8"?>\n' + record["xml"]
        record["tree"] = mdtree
        return record
Exemple #44
0
def test_wps_request8():
    # Process input/ouutput arguments
    processid = "wordcount"
    textdoc = ComplexDataInput("Alice was beginning to get very tired ...")
    inputs = [("text", textdoc), ]
    outputs = [("output", True), ]

    # Build XML request for WPS process execution
    execution = WPSExecution()
    requestElement = execution.buildRequest(processid, inputs, output=outputs, async=True, lineage=True)
    request = etree.tostring(requestElement)

    # Compare to cached XML request
    _request = open(resource_file('wps_EmuExecuteRequest8.xml'), 'rb').read()
    assert compare_xml(request, _request) is True
Exemple #45
0
    def test_box_wgs84(self):
        """Test the Box type with WGS84 coordinates.

        Test whether the generated XML is correct.

        """
        box = Box(3.6214, 50.9850, 3.8071, 51.1270, epsg=4326)
        xml = box.get_element()

        assert clean_xml(etree.tostring(xml).decode('utf8')) == clean_xml(
            '<gml:Envelope srsDimension="2" '
            'srsName="http://www.opengis.net/gml/srs/epsg.xml#4326">'
            '<gml:lowerCorner>3.621400 50.985000</gml:lowerCorner>'
            '<gml:upperCorner>3.807100 51.127000</gml:upperCorner>'
            '</gml:Envelope>')
Exemple #46
0
def element_to_string(element, encoding=None, xml_declaration=False):
    """
    Returns a string from a XML object

    Parameters
    ----------
    - element: etree Element
    - encoding (optional): encoding in string form. 'utf-8', 'ISO-8859-1', etc.
    - xml_declaration (optional): whether to include xml declaration

    """

    output = None

    if encoding is None:
        encoding = "ISO-8859-1"

    if etree.__name__ == 'lxml.etree':
        if xml_declaration:
            if encoding in ['unicode', 'utf-8']:
                output = '<?xml version="1.0" encoding="utf-8" standalone="no"?>\n%s' % \
                       etree.tostring(element, encoding='unicode')
            else:
                output = etree.tostring(element,
                                        encoding=encoding,
                                        xml_declaration=True)
        else:
            output = etree.tostring(element)
    else:
        if xml_declaration:
            output = '<?xml version="1.0" encoding="%s" standalone="no"?>\n%s' % (
                encoding, etree.tostring(element, encoding=encoding))
        else:
            output = etree.tostring(element)

    return output
Exemple #47
0
def _inspire_validator(uuid):
    global _csw
    # Check the layer is in the GeoNetwork catalog and points back to get_absolute_url
    if(_csw is None): # Might need to re-cache, nothing equivalent to _wms.contents?
        _csw = get_csw()

    _csw.getrecordbyid([uuid], outputschema=namespaces["gmd"])
    md_metadata = _csw._exml.find('//'+util.nspath('MD_Metadata', namespaces['gmd']))
    body = etree.tostring(md_metadata)
    mp = MultipartParam('dataFile',body,filename='test.xml',filetype='text/xml',filesize=len(body))
    datagen, headers = multipart_encode([mp])
    request = urllib2.Request("http://www.inspire-geoportal.eu/INSPIREValidatorService/resources/validation/inspire", datagen, headers)
    register_openers()
    response = urllib2.urlopen(request).read()
    return response
def test_wps_request4():
    # Process input/ouutput arguments
    processid = "reprojectImage"
    inputs = [("inputImage", "http://rsg.pml.ac.uk/wps/testdata/elev_srtm_30m.img"),
              ("outputSRS", "EPSG:4326")]
    output = "outputImage"

    # build XML request for WPS process execution
    execution = WPSExecution()
    requestElement = execution.buildRequest(processid, inputs, output=[(output, True)])
    request = etree.tostring(requestElement)

    # Compare to cached XML request
    _request = open(resource_file('wps_PMLExecuteRequest4.xml'), 'rb').read()
    assert compare_xml(request, _request) is True
def test_wps_request8():
    # Process input/ouutput arguments
    processid = "wordcount"
    textdoc = ComplexDataInput("Alice was beginning to get very tired ...")
    inputs = [("text", textdoc), ]
    outputs = [("output", True), ]

    # Build XML request for WPS process execution
    execution = WPSExecution()
    requestElement = execution.buildRequest(processid, inputs, output=outputs, mode=ASYNC, lineage=True)
    request = etree.tostring(requestElement)

    # Compare to cached XML request
    _request = open(resource_file('wps_EmuExecuteRequest8.xml'), 'rb').read()
    assert compare_xml(request, _request) is True
Exemple #50
0
    def test_multipolygon_multiple_31370_stable(self):
        """Test the Within filter with a GML containing multiple
        multipolygon geometries in EPSG:31370.

        Test whether the generated XML is correct.

        """
        with open('tests/data/util/location/multipolygon_multiple_31370.gml',
                  'r') as gml_file:
            gml = gml_file.read()

        for i in range(10):
            f = GmlFilter(gml, Within)
            f.set_geometry_column('geom')
            xml = f.toXML()

            assert clean_xml(etree.tostring(xml).decode('utf8')) == clean_xml(
                '<ogc:Or><ogc:Within><ogc:PropertyName>geom</ogc'
                ':PropertyName><gml:MultiSurface '
                'srsName="urn:ogc:def:crs:EPSG::31370"><gml:surfaceMember'
                '><gml:Polygon><gml:exterior><gml:LinearRing><gml:posList'
                '>107564.578273715 196646.993956647 107785.195986354 '
                '196386.980223894 107966.417678878 197143.383810084 '
                '107564.578273715 '
                '196646.993956647</gml:posList></gml:LinearRing'
                '></gml:exterior></gml:Polygon></gml:surfaceMember><gml'
                ':surfaceMember><gml:Polygon><gml:exterior><gml:LinearRing'
                '><gml:posList>108384.015492088 197214.29664629 '
                '108447.04912427 196489.40987619 108785.854897252 '
                '197269.45107445 108384.015492088 '
                '197214.29664629</gml:posList></gml:LinearRing'
                '></gml:exterior></gml:Polygon></gml:surfaceMember></gml'
                ':MultiSurface></ogc:Within><ogc:Within><ogc:PropertyName'
                '>geom</ogc:PropertyName><gml:MultiSurface '
                'srsName="urn:ogc:def:crs:EPSG::31370"><gml:surfaceMember'
                '><gml:Polygon><gml:exterior><gml:LinearRing><gml:posList'
                '>108588.874796681 195015.998723923 108911.922161617 '
                '194251.71593371 109195.573506438 195134.186784266 '
                '108588.874796681 '
                '195015.998723923</gml:posList></gml:LinearRing'
                '></gml:exterior></gml:Polygon></gml:surfaceMember><gml'
                ':surfaceMember><gml:Polygon><gml:exterior><gml:LinearRing'
                '><gml:posList>109140.419078278 195748.764698045 '
                '109400.432811031 195307.529272768 109597.412911602 '
                '195772.402310114 109140.419078278 '
                '195748.764698045</gml:posList></gml:LinearRing'
                '></gml:exterior></gml:Polygon></gml:surfaceMember></gml'
                ':MultiSurface></ogc:Within></ogc:Or>')
Exemple #51
0
    def test_equals_point(self):
        """Test the Equals spatial filter with a Point location.

        Test whether the generated XML is correct.

        """
        equals = Equals(Point(150000, 150000))
        equals.set_geometry_column('geom')
        xml = equals.toXML()

        assert clean_xml(etree.tostring(xml).decode('utf8')) == clean_xml(
            '<ogc:Equals><ogc:PropertyName>geom</ogc:PropertyName>'
            '<gml:Point srsDimension="2" '
            'srsName="http://www.opengis.net/gml/srs/epsg.xml#31370">'
            '<gml:pos>150000.000000 150000.000000</gml:pos></gml:Point>'
            '</ogc:Equals>')
Exemple #52
0
def test_wps_request7():
    # Process input/ouutput arguments
    processid = "wordcount"
    textdoc = ComplexDataInput("http://emu.readthedocs.org/en/latest/index.html")
    inputs = [("text", textdoc), ]
    outputs = [("output", True)]

    # Build XML request for WPS process execution, sync request
    execution = WPSExecution()
    requestElement = execution.buildRequest(processid, inputs, output=outputs, mode=SYNC, lineage=False)
    request = etree.tostring(requestElement)

    # Compare to cached XML request
    _request = open(resource_file('wps_EmuExecuteRequest7.xml'), 'rb').read()
    print(request)
    assert compare_xml(request, _request) is True
Exemple #53
0
    def __init__(self, elem, namespace=DEFAULT_OWS_NAMESPACE):
        self.exceptions = []
        for i in elem.findall(util.nspath('Exception', namespace)):
            tmp = {}
            val = i.attrib.get('exceptionCode')
            tmp['exceptionCode'] = util.testXMLValue(val, True)
            val = i.attrib.get('locator')
            tmp['locator'] = util.testXMLValue(val, True)
            val = i.find(util.nspath('ExceptionText', namespace))
            tmp['ExceptionText'] = util.testXMLValue(val)
            self.exceptions.append(tmp)

        # set topmost stacktrace as return message
        self.code = self.exceptions[0]['exceptionCode']
        self.locator = self.exceptions[0]['locator']
        self.msg = self.exceptions[0]['ExceptionText']
        self.xml = etree.tostring(elem)
Exemple #54
0
    def test_wfs_build_getfeature_request_onlytypename(self):
        """Test the owsutil.wfs_build_getfeature_request method with only a
        typename specified.

        Test whether the XML of the WFS GetFeature call is generated correctly.

        """
        xml = owsutil.wfs_build_getfeature_request('dov-pub:Boringen')
        assert clean_xml(etree.tostring(xml).decode('utf8')) == clean_xml(
            '<wfs:GetFeature xmlns:wfs="http://www.opengis.net/wfs" '
            'xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" '
            'service="WFS" version="1.1.0" '
            'xsi:schemaLocation="http://www.opengis.net/wfs '
            'http://schemas.opengis.net/wfs/1.1.0/wfs.xsd"><wfs:Query '
            'typeName="dov-pub:Boringen"><ogc:Filter '
            'xmlns:ogc="http://www.opengis.net/ogc"/></wfs:Query></wfs'
            ':GetFeature>')
Exemple #55
0
    def test_intersects_box(self):
        """Test the Intersects spatial filter with a Box location.

        Test whether the generated XML is correct.

        """
        intersects = Intersects(Box(94720, 186910, 112220, 202870))
        intersects.set_geometry_column('geom')
        xml = intersects.toXML()

        assert clean_xml(etree.tostring(xml).decode('utf8')) == clean_xml(
            '<ogc:Intersects><ogc:PropertyName>geom</ogc:PropertyName>'
            '<gml:Envelope srsDimension="2" '
            'srsName="http://www.opengis.net/gml/srs/epsg.xml#31370">'
            '<gml:lowerCorner>94720.000000 186910.000000</gml:lowerCorner>'
            '<gml:upperCorner>112220.000000 202870.000000</gml:upperCorner>'
            '</gml:Envelope></ogc:Intersects>')
Exemple #56
0
    def test_withindistance_point(self):
        """Test the WithinDistance spatial filter with a Point location.

        Test whether the generated XML is correct.

        """
        withindistance = WithinDistance(Point(150000, 150000), 100)
        withindistance.set_geometry_column('geom')
        xml = withindistance.toXML()

        assert clean_xml(etree.tostring(xml).decode('utf8')) == clean_xml(
            '<ogc:DWithin><ogc:PropertyName>geom</ogc:PropertyName>'
            '<gml:Point srsDimension="2" '
            'srsName="http://www.opengis.net/gml/srs/epsg.xml#31370">'
            '<gml:pos>150000.000000 150000.000000</gml:pos></gml:Point>'
            '<gml:Distance units="meter">100.000000</gml:Distance>'
            '</ogc:DWithin>')
Exemple #57
0
    def harvest(self, source, resourcetype, resourceformat=None, harvestinterval=None, responsehandler=None):
        """

        Construct and process a Harvest request

        Parameters
        ----------

        - source: a URI to harvest
        - resourcetype: namespace identifying the type of resource
        - resourceformat: MIME type of the resource
        - harvestinterval: frequency of harvesting, in ISO8601
        - responsehandler: endpoint that CSW should responsd to with response

        """

        # construct request
        node0 = self._setrootelement('csw:Harvest')
        node0.set('version', self.version)
        node0.set('service', self.service)
        node0.set(util.nspath_eval('xsi:schemaLocation', namespaces), schema_location)
        etree.SubElement(node0, util.nspath_eval('csw:Source', namespaces)).text = source
        etree.SubElement(node0, util.nspath_eval('csw:ResourceType', namespaces)).text = resourcetype
        if resourceformat is not None:
            etree.SubElement(node0, util.nspath_eval('csw:ResourceFormat', namespaces)).text = resourceformat
        if harvestinterval is not None:
            etree.SubElement(node0, util.nspath_eval('csw:HarvestInterval', namespaces)).text = harvestinterval
        if responsehandler is not None:
            etree.SubElement(node0, util.nspath_eval('csw:ResponseHandler', namespaces)).text = responsehandler
       
        self.request = util.xml2string(etree.tostring(node0))

        self._invoke()
        self.results = {}

        if self.exceptionreport is None:
            val = self._exml.find(util.nspath_eval('csw:Acknowledgement', namespaces))
            if util.testXMLValue(val) is not None:
                ts = val.attrib.get('timeStamp')
                self.timestamp = util.testXMLValue(ts, True)
                id = val.find(util.nspath_eval('csw:RequestId', namespaces))
                self.id = util.testXMLValue(id) 
            else:
                self._parsetransactionsummary()
                self._parseinsertresult()