Esempio n. 1
0
    def get_info_from_pf(self, layer_id, rows, measurements=False):
        import geoportailv3.PF
        pf = geoportailv3.PF.PF()
        features = []
        for row in rows:
            geometry = geojson_loads(row['st_asgeojson'])
            if 'textstring' in row:
                fid = row['textstring']
            else:
                fid = None
            f = self.to_feature(layer_id, fid, geometry, dict(row), "")

            attributes = f['attributes']
            attributes['PF'] = dict(
                pf.get_detail(attributes['code_commu'],
                              attributes['code_secti'],
                              int(attributes['num_cadast']),
                              int(attributes['code_sup'])))

            if measurements:
                attributes['measurements'] = pf.get_measurement_list(
                    attributes['num_cadast'], attributes['code_sup'],
                    attributes['code_secti'], attributes['code_commu'],
                    self.request.user, self.request.referer)

            features.append(f)

        return features
Esempio n. 2
0
    def test_valid_data(self):
        station = {
            'dec_lat_va': '45.0',
            'dec_long_va': '-100.0',
            'dec_coord_datum_cd': 'NAD83',
            'station_nm': 'Black River',
            'agency_cd': 'USGS',
            'site_no': '12345',
            'huc_cd': '03120312',
            'site_tp_cd': 'ST',

        }
        expected_properties = {
            'stationName': 'Black River',
            'agencyCode': 'USGS',
            'siteNumber': '12345',
            'hucCode': '03120312',
            'SiteTypeCode': 'ST',
            'SiteType': 'Stream',
            'siteId' : 'USGS-12345',
            'url': 'https://waterdata.usgs.gov/nwis/inventory?agency_code=USGS&site_no=12345'
        }
        result = geojson_loads(geojson_dumps(get_site_feature(station)))

        self.assertEqual(result['properties'], expected_properties)
        self.assertEqual(result['geometry']['type'], 'Point')
Esempio n. 3
0
    def test_two_params_in_list(self, m):
        headers = '\t'.join([
            'agency_cd', 'site_no', 'station_nm', 'site_tp_cd', 'dec_lat_va',
            'dec_long_va', 'coord_acy_cd', 'dec_coord_datum_cd', 'alt_va',
            'alt_acy_va', 'alt_datum_cd', 'huc_cd'
        ])
        site1 = '\t'.join([
            'USGS', '00336840', 'BISCUIT BROOK NTN SITE', 'AT', '41.9942589',
            '-74.5032094', 'S', 'NAD83', '2087', '4.3', 'NAVD88', '02040104'
        ])
        site2 = '\t'.join([
            'USGS', '01300450', 'BEAVER SWAMP BROOK AT RYE NY	ST', '40.98',
            '-73.7019444', 'S', 'NAD83', '49', '4.3', 'NAVD88', '02030102'
        ])

        m.get('https://waterservices.usgs.gov/nwis/site/',
              [{
                  'text': '\n'.join([headers, 'skip this line', site1])
              }, {
                  'text': '\n'.join([headers, 'skip this line', site2])
              }])

        result = tuple(
            site_geojson_generator([{
                'hucCd': '02'
            }, {
                'hucCd': '01'
            }]))
        feature = geojson_loads(''.join(result).replace('\n', ''))

        self.assertEqual(len(feature['features']), 2)
Esempio n. 4
0
    def get_additional_info_for_ng95(self, layer_id, rows):
        features = []
        dirname = "/sketch"

        for row in rows:
            geometry = geojson_loads(row['st_asgeojson'])
            if 'id' in row:
                fid = row['id']
            else:
                fid = None
            feature = self.to_feature(layer_id, fid, geometry, dict(row), "")

            feature['attributes']['has_sketch'] = False
            nom_croq = feature['attributes']['nom_croq']
            if nom_croq is not None:
                sketch_filepath = dirname + "/" + nom_croq + ".pdf"
                f = None
                try:
                    f = open(sketch_filepath, 'r')
                except:
                    try:
                        sketch_filepath = dirname + "/" + nom_croq + ".PDF"
                        f = open(sketch_filepath, 'r')
                    except:
                        f = None
                if f is not None:
                    feature['attributes']['has_sketch'] = True

                features.append(feature)

        return features
Esempio n. 5
0
    def get_additional_info_for_ng95(self, rows):
        features = []
        dirname = "/sketch"

        for row in rows:
            geometry = geojson_loads(row['st_asgeojson'])

            feature = self.to_feature(geometry, dict(row), "")

            feature['attributes']['has_sketch'] = False
            nom_croq = feature['attributes']['nom_croq']
            if nom_croq is not None:
                sketch_filepath = dirname + "/" + nom_croq + ".pdf"
                f = None
                try:
                    f = open(sketch_filepath, 'r')
                except:
                    try:
                        sketch_filepath = dirname + "/" + nom_croq + ".PDF"
                        f = open(sketch_filepath, 'r')
                    except:
                        f = None
                if f is not None:
                    feature['attributes']['has_sketch'] = True

                features.append(feature)

        return features
Esempio n. 6
0
    def test_valid_data(self):
        station = {
            'dec_lat_va': '45.0',
            'dec_long_va': '-100.0',
            'dec_coord_datum_cd': 'NAD83',
            'station_nm': 'Black River',
            'agency_cd': 'USGS',
            'site_no': '12345',
            'huc_cd': '03120312',
            'site_tp_cd': 'ST',
        }
        expected_properties = {
            'stationName':
            'Black River',
            'agencyCode':
            'USGS',
            'siteNumber':
            '12345',
            'hucCode':
            '03120312',
            'SiteTypeCode':
            'ST',
            'SiteType':
            'Stream',
            'siteId':
            'USGS-12345',
            'url':
            'https://waterdata.usgs.gov/nwis/inventory?agency_code=USGS&site_no=12345'
        }
        result = geojson_loads(geojson_dumps(get_site_feature(station)))

        self.assertEqual(result['properties'], expected_properties)
        self.assertEqual(result['geometry']['type'], 'Point')
Esempio n. 7
0
    def get_info_from_mymaps(self, rows, attributes_to_remove):
        features = []
        ids = []

        for row in rows:
            category_id = row['category_id']
            map_id = row['map_id']
            cur_id = str(map_id) + "--" + str(category_id)
            if cur_id not in ids:
                ids.append(cur_id)
                engine = sqlahelper.get_engine("mymaps")
                query = "select  ST_AsGeoJSON(ST_Collect (geometry)) as geometry\
                        , sum(ST_Length(geometry)) as length FROM\
                         public.feature_with_map_with_colors where\
                         category_id = %(category_id)d and map_id = '%(map_id)s'"\
                        % {'category_id': category_id, 'map_id': map_id}
                res = engine.execute(query)
                for feature in res.fetchall():
                    geometry = geojson_loads(feature['geometry'])
                    attributes = dict(row)
                    attributes['length'] = round(feature['length'] / 1000, 2)
                    self.remove_attributes(attributes,
                                           attributes_to_remove,
                                           "geometry")
                    features.append(self.to_feature(geometry, attributes, ""))

        return features
    def _ogc_getfeatureinfo(self, session, url, x, y, width, height, layer,
                            bbox, srs, layer_id):
        # session = get_session('development.ini', 'app')
        from c2cgeoportal_commons.models.main import Metadata

        body = {
            'SERVICE': 'WMS',
            'VERSION': '1.1.1',
            'REQUEST': 'GetFeatureInfo',
            'QUERY_LAYERS': layer,
            'LAYERS': layer,
            'STYLES': '',
            'INFO_FORMAT': 'application/json',
            'FEATURE_COUNT': '50',
            'X': x,
            'Y': y,
            'SRS': srs,
            'WIDTH': width,
            'HEIGHT': height,
            'BBOX': bbox
        }
        metadata = session.query(Metadata).filter(Metadata.item_id == layer_id).\
                   filter(Metadata.name == "ogc_layers").first()
        if metadata is not None:
            body['LAYERS'] = metadata.value

        metadata = session.query(Metadata).filter(Metadata.item_id == layer_id).\
                   filter(Metadata.name == "ogc_query_layers").first()
        if metadata is not None:
            body['QUERY_LAYERS'] = metadata.value

        metadata = session.query(Metadata).filter(Metadata.item_id == layer_id).\
                   filter(Metadata.name == "ogc_info_format").first()
        if metadata is not None:
            body['INFO_FORMAT'] = metadata.value
        ogc_info_srs = "epsg:2169"
        metadata = session.query(Metadata).filter(Metadata.item_id == layer_id).\
                   filter(Metadata.name == "ogc_info_srs").first()
        if metadata is not None:
            ogc_info_srs = metadata.value

        separator = "?"
        if url.find(separator) > 0:
            separator = "&"
        query = '%s%s%s' % (url, separator, urlencode(body))
        content = ""
        try:
            result = urllib.request.urlopen(query, None, 15)
            content = result.read()
        except Exception as e:
            log.exception(e)
            return []
        try:
            ogc_features = geojson_loads(content)
            return (dict((key, key) for key, value in ogc_features['features']
                         [0]['properties'].items()))
        except Exception as e:
            log.exception(e)
            return []
        return []
    def get_info_from_pf(self, layer_id, rows, measurements=False):
        import geoportailv3.PF
        pf = geoportailv3.PF.PF()
        features = []
        for row in rows:
            geometry = geojson_loads(row['st_asgeojson'])
            if 'textstring' in row:
                fid = row['textstring']
            else:
                fid = None
            f = self.to_feature(layer_id, fid,
                                geometry, dict(row), "")

            attributes = f['attributes']
            attributes['PF'] = dict(pf.get_detail(
                attributes['code_commu'],
                attributes['code_secti'],
                int(attributes['num_cadast']),
                int(attributes['code_sup'])))

            if measurements:
                attributes['measurements'] = pf.get_measurement_list(
                    attributes['num_cadast'],
                    attributes['code_sup'],
                    attributes['code_secti'],
                    attributes['code_commu'],
                    self.request.user,
                    self.request.referer)

            features.append(f)

        return features
    def get_info_from_mymaps(self, layer_id, rows, attributes_to_remove):
        features = []
        ids = []
        for row in rows:
            category_id = row['category_id']
            if 'id' in row:
                fid = row['id']
            else:
                fid = None

            map_id = row['map_id']
            cur_id = str(map_id) + "--" + str(category_id)
            if cur_id not in ids:
                ids.append(cur_id)
                geometry = geojson_loads(row['st_asgeojson'])
                if geometry['type'] == "LineString" or\
                   geometry['type'] == "MultiLineString":
                    session = self._get_session("mymaps")
                    query = "select  ST_AsGeoJSON(ST_Collect (geometry)) as geometry\
                            , sum(ST_Length(geometry)) as length FROM\
                             public.feature_with_map_with_colors where\
                             category_id = %(category_id)d and map_id = '%(map_id)s'"\
                            % {'category_id': category_id, 'map_id': map_id}
                    res = session.execute(query)
                    for feature in res.fetchall():
                        geometry = geojson_loads(feature['geometry'])
                        attributes = dict(row)
                        attributes['length'] = round(feature['length'] / 1000,
                                                     2)
                        self.remove_attributes(attributes,
                                               attributes_to_remove,
                                               "geometry")
                        features.append(
                            self.to_feature(layer_id, fid,
                                            geometry,
                                            attributes, ""))
                else:
                    attributes = dict(row)
                    self.remove_attributes(attributes,
                                           attributes_to_remove,
                                           "geometry")
                    features.append(
                        self.to_feature(layer_id, fid,
                                        geometry, attributes, ""))
        return features
Esempio n. 11
0
    def get_info_from_mymaps(self, layer_id, rows, attributes_to_remove):
        features = []
        ids = []
        for row in rows:
            category_id = row['category_id']
            if 'id' in row:
                fid = row['id']
            else:
                fid = None

            map_id = row['map_id']
            cur_id = str(map_id) + "--" + str(category_id)
            if cur_id not in ids:
                ids.append(cur_id)
                geometry = geojson_loads(row['st_asgeojson'])
                if geometry['type'] == "LineString" or\
                   geometry['type'] == "MultiLineString":
                    session = self._get_session("mymaps")
                    query = "select  ST_AsGeoJSON(ST_Collect (geometry)) as geometry\
                            , sum(ST_Length(geometry)) as length FROM\
                             public.feature_with_map_with_colors where\
                             category_id = %(category_id)d and map_id = '%(map_id)s'"\
                            % {'category_id': category_id, 'map_id': map_id}
                    res = session.execute(query)
                    for feature in res.fetchall():
                        geometry = geojson_loads(feature['geometry'])
                        attributes = dict(row)
                        attributes['length'] = round(feature['length'] / 1000,
                                                     2)
                        self.remove_attributes(attributes,
                                               attributes_to_remove,
                                               "geometry")
                        features.append(
                            self.to_feature(layer_id, fid,
                                            geometry,
                                            attributes, ""))
                else:
                    attributes = dict(row)
                    self.remove_attributes(attributes,
                                           attributes_to_remove,
                                           "geometry")
                    features.append(
                        self.to_feature(layer_id, fid,
                                        geometry, attributes, ""))
        return features
Esempio n. 12
0
    def reverse(self):
        """
        View used to get an adress from a coordinate.
        """

        easting = self.request.params.get("easting", None)
        northing = self.request.params.get("northing", None)

        if (
            easting is None
            or northing is None
            or len(easting) == 0
            or len(northing) == 0
            or re.match("^[0-9]*[.]{0,1}[0-9]*$", easting) is None
            or re.match("^[0-9]*[.]{0,1}[0-9]*$", northing) is None
        ):

            return HTTPBadRequest("Missing or invalid coordinates")

        distcol = func.ST_distance(
            WKTElement("POINT(%(x)s %(y)s)" % {"x": easting, "y": northing}, srid=2169), Address.geom
        )

        results = []

        for feature in (
            DBSession.query(
                Address.id_caclr_rue.label("id_caclr_rue"),
                Address.id_caclr_bat.label("id_caclr_bat"),
                Address.rue.label("rue"),
                Address.numero.label("numero"),
                Address.localite.label("localite"),
                Address.code_postal.label("code_postal"),
                func.ST_AsGeoJSON(Address.geom).label("geom"),
                distcol.label("distance"),
            )
            .order_by(distcol)
            .limit(1)
        ):
            results.append(
                {
                    "id_caclr_street": feature.id_caclr_rue,
                    "id_caclr_bat": feature.id_caclr_bat,
                    "street": feature.rue,
                    "number": feature.numero,
                    "locality": feature.localite,
                    "postal_code": feature.code_postal,
                    "distance": feature.distance,
                    "geom": geojson_loads(feature.geom),
                }
            )

        return {"count": len(results), "results": results}
Esempio n. 13
0
    def test_one_params_list(self, m):
        headers = '\t'.join(['agency_cd', 'site_no', 'station_nm', 'site_tp_cd', 'dec_lat_va', 'dec_long_va',
                             'coord_acy_cd', 'dec_coord_datum_cd', 'alt_va', 'alt_acy_va', 'alt_datum_cd',
                             'huc_cd'])
        site1 = '\t'.join(['USGS', '00336840', 'BISCUIT BROOK NTN SITE', 'AT', '41.9942589', '-74.5032094',
                           'S', 'NAD83', '2087', '4.3', 'NAVD88', '02040104'])

        m.get('https://waterservices.usgs.gov/nwis/site/', text='\n'.join([headers, 'skip this line', site1]))

        result = tuple(site_geojson_generator([{'hucCd': '01'}]))
        feature = geojson_loads(''.join(result).replace('\n', ''))

        self.assertEqual(len(feature['features']), 1)
Esempio n. 14
0
def _get_external_data(url, bbox=None, layer=None):
    body = {
        'f': 'pjson',
        'geometry': '',
        'geometryType': '',
        'inSR': '2169',
        'outSR': '',
        'returnGeometry': 'true',
        'spatialRel': '',
        'text': '',
        'where': '',
        'outFields': '*',
        'objectIds': ''
    }

    body['geometry'] = bbox
    body['geometryType'] = 'esriGeometryEnvelope'
    body['spatialRel'] = 'esriSpatialRelIntersects'

    if url.find("@") > -1:
        url = _get_url_with_token(url)
        if url is None:
            return None

    # construct url for get request
    separator = "?"
    if url.find(separator) > 0:
        separator = "&"
    query = '%s%s%s' % (url, separator, urlencode(body))

    try:
        result = urllib2.urlopen(query, None, 15)
        content = result.read()
    except:
        print query
        traceback.print_exc(file=sys.stdout)
        return []

    try:
        esricoll = geojson_loads(content)
    except:
        return []
    if 'fieldAliases' not in esricoll:
        print("Error with the layer:  %s using query : %s response: %s" %
              (layer, query, esricoll))
        return []
    else:
        return dict((value, key)
                    for key, value in esricoll['fieldAliases'].iteritems())
Esempio n. 15
0
    def _ogc_getfeatureinfo(
            self, url, x, y, width, height, layer, bbox, srs, layer_id,
            attributes_to_remove, columns_order):
        body = {
            'SERVICE': 'WMS',
            'VERSION': '1.1.1',
            'REQUEST': 'GetFeatureInfo',
            'QUERY_LAYERS': layer,
            'LAYERS': layer,
            'STYLES': '',
            'INFO_FORMAT': 'application/json',
            'FEATURE_COUNT': '50',
            'X': x,
            'Y': y,
            'SRS': srs,
            'WIDTH': width,
            'HEIGHT': height,
            'BBOX': bbox
        }
        separator = "?"
        if url.find(separator) > 0:
            separator = "&"
        query = '%s%s%s' % (url, separator, urlencode(body))

        content = ""
        try:
            result = urllib2.urlopen(query, None, 15)
            content = result.read()
        except Exception as e:
            log.exception(e)
            return []
        try:
            features = []
            ogc_features = geojson_loads(content)

            for feature in ogc_features['features']:
                f = self.to_feature(layer_id, None,
                                    feature['geometry'],
                                    feature['properties'],
                                    attributes_to_remove,
                                    columns_order)
                features.append(f)
            return features
        except Exception as e:
            log.exception(e)
            return []
        return []
def _get_external_data(url, bbox=None, layer=None):
    body = {'f': 'pjson',
            'geometry': '',
            'geometryType': '',
            'inSR': '2169',
            'outSR': '',
            'returnGeometry': 'true',
            'spatialRel': '',
            'text': '',
            'where': '',
            'outFields': '*',
            'objectIds': ''}

    body['geometry'] = bbox
    body['geometryType'] = 'esriGeometryEnvelope'
    body['spatialRel'] = 'esriSpatialRelIntersects'

    if url.find("@") > -1:
        url = _get_url_with_token(url)
        if url is None:
            return None

    # construct url for get request
    separator = "?"
    if url.find(separator) > 0:
        separator = "&"
    query = '%s%s%s' % (url, separator, urlencode(body))

    try:
        result = urllib2.urlopen(query, None, 15)
        content = result.read()
    except:
        print query
        traceback.print_exc(file=sys.stdout)
        return []

    try:
        esricoll = geojson_loads(content)
    except:
        return []
    if 'fieldAliases' not in esricoll:
        print("Error with the layer:  %s using query : %s response: %s"
              % (layer, query, esricoll))
        return []
    else:
        return dict((value, key)
                    for key, value in esricoll['fieldAliases'].iteritems())
    def _ogc_getfeatureinfo(
            self, url, x, y, width, height, layer, bbox, srs, layer_id,
            attributes_to_remove, columns_order):
        body = {
            'SERVICE': 'WMS',
            'VERSION': '1.1.1',
            'REQUEST': 'GetFeatureInfo',
            'QUERY_LAYERS': layer,
            'LAYERS': layer,
            'STYLES': '',
            'INFO_FORMAT': 'application/json',
            'FEATURE_COUNT': '50',
            'X': x,
            'Y': y,
            'SRS': srs,
            'WIDTH': width,
            'HEIGHT': height,
            'BBOX': bbox
        }
        separator = "?"
        if url.find(separator) > 0:
            separator = "&"
        query = '%s%s%s' % (url, separator, urlencode(body))

        content = ""
        try:
            result = urllib2.urlopen(query, None, 15)
            content = result.read()
        except Exception as e:
            log.exception(e)
            return []
        try:
            features = []
            ogc_features = geojson_loads(content)

            for feature in ogc_features['features']:
                f = self.to_feature(layer_id, None,
                                    feature['geometry'],
                                    feature['properties'],
                                    attributes_to_remove,
                                    columns_order)
                features.append(f)
            return features
        except Exception as e:
            log.exception(e)
            return []
        return []
Esempio n. 18
0
    def get_info_from_pf(self, rows):
        import geoportailv3.PF
        pf = geoportailv3.PF.PF()
        features = []
        for row in rows:
            geometry = geojson_loads(row['st_asgeojson'])

            f = self.to_feature(geometry, dict(row), "")

            d = f['attributes']
            d['PF'] = dict(pf.get_detail(d['code_commu'],
                                         d['code_secti'],
                                         int(d['num_cadast']),
                                         int(d['code_sup'])))
            features.append(f)

        return features
Esempio n. 19
0
    def get_feature_info(self):
        layers = self.request.params.get('layers', None)
        if layers is None:
            return HTTPBadRequest()
        big_box = self.request.params.get('box1', None)
        small_box = self.request.params.get('box2', None)
        if big_box is None or small_box is None:
            return HTTPBadRequest()

        luxgetfeaturedefinitions = self.get_lux_feature_definition(layers)
        coordinates_big_box = big_box.split(',')
        coordinates_small_box = small_box.split(',')

        results = []
        for luxgetfeaturedefinition in luxgetfeaturedefinitions:
            if (luxgetfeaturedefinition is not None and
                luxgetfeaturedefinition.engine is not None and
                luxgetfeaturedefinition.query is not None and
                    len(luxgetfeaturedefinition.query) > 0):
                engine = sqlahelper.get_engine(luxgetfeaturedefinition.engine)

                query_1 = luxgetfeaturedefinition.query
                if "WHERE" in query_1.upper():
                    query_1 = query_1 + " AND "
                else:
                    query_1 = query_1 + " WHERE "

                if "SELECT" in query_1.upper():
                    query_1 = query_1.replace(
                        "SELECT",
                        "SELECT ST_AsGeoJSON (%(geom)s), "
                        % {'geom': luxgetfeaturedefinition.geometry_column}, 1)

                else:
                    query_1 = "SELECT *,ST_AsGeoJSON(%(geom)s) FROM "\
                        % {'geom': luxgetfeaturedefinition.geometry_column} +\
                        query_1

                query_point = query_1 + "ST_Intersects( %(geom)s, "\
                    "ST_MakeEnvelope(%(left)s, %(bottom)s, %(right)s,"\
                    "%(top)s, 2169) ) AND ST_NRings(%(geom)s) = 0"\
                    % {'left': coordinates_big_box[0],
                       'bottom': coordinates_big_box[1],
                       'right': coordinates_big_box[2],
                       'top': coordinates_big_box[3],
                       'geom': luxgetfeaturedefinition.geometry_column}

                query_others = query_1 + "ST_Intersects( %(geom)s,"\
                    " ST_MakeEnvelope (%(left)s, %(bottom)s, %(right)s,"\
                    " %(top)s, 2169) ) AND  ST_NRings(%(geom)s) > 0"\
                    % {'left': coordinates_small_box[0],
                       'bottom': coordinates_small_box[1],
                       'right': coordinates_small_box[2],
                       'top': coordinates_small_box[3],
                       'geom': luxgetfeaturedefinition.geometry_column}

                query = query_point + " UNION ALL " + query_others +\
                    " LIMIT 20"

                res = engine.execute(query)
                rows = res.fetchall()

                if (luxgetfeaturedefinition.additional_info_function
                    is not None and
                    len(luxgetfeaturedefinition.additional_info_function)
                        > 0):

                    features = eval(luxgetfeaturedefinition.
                                    additional_info_function)

                    if len(features) > 0:
                        results.append(
                            self.to_featureinfo(
                                features,
                                luxgetfeaturedefinition.layer,
                                luxgetfeaturedefinition.template,
                                luxgetfeaturedefinition.remote_template))
                else:
                    features = []
                    for row in rows:
                        geometry = geojson_loads(row['st_asgeojson'])
                        attributes = dict(row)
                        f = self.to_feature(
                            geometry,
                            attributes,
                            luxgetfeaturedefinition.attributes_to_remove,
                            luxgetfeaturedefinition.geometry_column)
                        features.append(f)
                    if len(features) > 0:
                        results.append(
                            self.to_featureinfo(
                                self.remove_features_outside_tolerance(
                                    features, coordinates_small_box),
                                luxgetfeaturedefinition.layer,
                                luxgetfeaturedefinition.template,
                                luxgetfeaturedefinition.remote_template))

            if (luxgetfeaturedefinition is not None and
                luxgetfeaturedefinition.rest_url is not None and
                    len(luxgetfeaturedefinition.rest_url) > 0):
                features = self._get_external_data(
                    luxgetfeaturedefinition.rest_url,
                    big_box, None, None,
                    luxgetfeaturedefinition.attributes_to_remove)
                if len(features) > 0:
                    results.append(
                        self.to_featureinfo(
                            self.remove_features_outside_tolerance(
                                features, coordinates_small_box),
                            luxgetfeaturedefinition.layer,
                            luxgetfeaturedefinition.template,
                            luxgetfeaturedefinition.remote_template))
        return results
Esempio n. 20
0
    def _get_external_data(self, url, bbox=None, featureid=None, cfg=None,
                           attributes_to_remove=None):
        # ArcGIS Server REST API:
        # http://help.arcgis.com/en/arcgisserver/10.0/apis/rest/query.html
        # form example:
        # http://ws.geoportail.lu/ArcGIS/rest/services/wassergis/waassergis_mxd/MapServer/45/query
        #
        # params "params_meaning" (params_type) [predefined_values]:
        #
        # f              "Format" (choice) [html kml pjson]
        # geometry       "Filter Geometry" (string)
        # geometryType   "Geometry Type" (choice)
        #                [esriGeometryEnvelope esriGeometryPoint
        #                 esriGeometryPolyline esriGeometryPolygon
        #                 esriGeometryMultipoint]
        # inSR           "Input Spatial Reference (WKID)" (string)
        # objectIds      "The object IDs of this layer
        #                 / table to be queried" (string)
        # outFields      "Return Fields (Comma Separated or *)" (string)
        # outSR          "Output Spatial Reference (WKID)" (string)
        # returnGeometry "Return Geometry" (bool true/false)
        # spatialRel     "Spatial Relationship" (choice)
        #                [esriSpatialRelIntersects esriSpatialRelContains
        #                 esriSpatialRelCrosses
        #                 esriSpatialRelEnvelopeIntersects
        #                 esriSpatialRelIndexIntersects esriSpatialRelOverlaps
        #                 esriSpatialRelTouches esriSpatialRelWithin]
        # text           "nom" (string)
        # where          "Where" (string)
        #
        # example:
        # http://ws.geoportail.lu/ArcGIS/rest/services/wassergis/waassergis_mxd/MapServer/45/query?text=&geometry=69000%2C124000%2C70000%2C125000&geometryType=esriGeometryEnvelope&inSR=2169&spatialRel=esriSpatialRelIntersects&where=&returnGeometry=true&outSR=&outFields=&f=pjson

        body = {'f': 'pjson',
                'geometry': '',
                'geometryType': '',
                'inSR': '2169',
                'outSR': '',
                'returnGeometry': 'true',
                'spatialRel': '',
                'text': '',
                'where': '',
                'outFields': '*',
                'objectIds': ''}

        if featureid is not None:
            body['objectIds'] = featureid
        elif bbox is not None:
            body['geometry'] = bbox
            body['geometryType'] = 'esriGeometryEnvelope'
            body['spatialRel'] = 'esriSpatialRelIntersects'
        else:
            return []

        # construct url for get request
        separator = "?"
        if url.find('?'):
            separator = "&"
        query = '%s%s%s' % (url, separator, urlencode(body))

        try:
            result = urllib2.urlopen(query, None, 15)
            content = result.read()
        except:
            traceback.print_exc(file=sys.stdout)
            return []

        features = []
        try:
            esricoll = geojson_loads(content)
        except:
            raise
        if 'features' in esricoll:
            for rawfeature in esricoll['features']:
                geometry = ''
                if (rawfeature['geometry'] and
                    'x' in rawfeature['geometry'] and
                        'y' in rawfeature['geometry']):
                    geometry = {'type': 'Point',
                                'coordinates': [rawfeature['geometry']['x'],
                                                rawfeature['geometry']['y']]}
                elif (rawfeature['geometry'] and
                      'x' in rawfeature['geometry'] and
                      'Y' in rawfeature['geometry']):
                    geometry = {'type': 'Point',
                                'coordinates': [rawfeature['geometry']['x'],
                                                rawfeature['geometry']['Y']]}
                elif (rawfeature['geometry'] and
                      'paths' in rawfeature['geometry'] and
                      len(rawfeature['geometry']['paths']) > 0):
                    geometry = {'type': 'MultiLineString',
                                'coordinates': rawfeature['geometry']['paths']}
                elif (rawfeature['geometry'] and
                      'rings' in rawfeature['geometry'] and
                      len(rawfeature['geometry']['rings']) > 0):
                    geometry = {'type': 'Polygon',
                                'coordinates': rawfeature['geometry']['rings']}

                if geometry != '':
                    f = self.to_feature(geometry,
                                        rawfeature['attributes'],
                                        attributes_to_remove)
                    features.append(f)

        return features
    def get_info(self, fid, coordinates_big_box, coordinates_small_box,
                 results, layers, big_box):
        luxgetfeaturedefinitions = self.get_lux_feature_definition(layers)
        for luxgetfeaturedefinition in luxgetfeaturedefinitions:
            if (luxgetfeaturedefinition is not None and
                luxgetfeaturedefinition.engine_gfi is not None and
                luxgetfeaturedefinition.query is not None and
                    len(luxgetfeaturedefinition.query) > 0):
                is_ordered = luxgetfeaturedefinition.columns_order is not None\
                    and len(luxgetfeaturedefinition.columns_order) > 0
                query_1 = luxgetfeaturedefinition.query
                if "WHERE" in query_1.upper():
                    query_1 = query_1 + " AND "
                else:
                    query_1 = query_1 + " WHERE "

                if "SELECT" in query_1.upper():
                    query_1 = query_1.replace(
                        "SELECT",
                        "SELECT ST_AsGeoJSON (%(geom)s), "
                        % {'geom': luxgetfeaturedefinition.geometry_column}, 1)

                else:
                    query_1 = "SELECT *,ST_AsGeoJSON(%(geom)s) FROM "\
                        % {'geom': luxgetfeaturedefinition.geometry_column} +\
                        query_1
                if fid is None:
                    query_point = query_1 + "ST_Intersects( %(geom)s, "\
                        "ST_MakeEnvelope(%(left)s, %(bottom)s, %(right)s,"\
                        "%(top)s, 2169) ) AND ST_NRings(%(geom)s) = 0"\
                        % {'left': coordinates_big_box[0],
                           'bottom': coordinates_big_box[1],
                           'right': coordinates_big_box[2],
                           'top': coordinates_big_box[3],
                           'geom': luxgetfeaturedefinition.geometry_column}

                    query_others = query_1 + "ST_Intersects( %(geom)s,"\
                        " ST_MakeEnvelope (%(left)s, %(bottom)s, %(right)s,"\
                        " %(top)s, 2169) ) AND  ST_NRings(%(geom)s) > 0"\
                        % {'left': coordinates_small_box[0],
                           'bottom': coordinates_small_box[1],
                           'right': coordinates_small_box[2],
                           'top': coordinates_small_box[3],
                           'geom': luxgetfeaturedefinition.geometry_column}
                    query_limit = 20
                    if luxgetfeaturedefinition.query_limit is not None:
                        query_limit = luxgetfeaturedefinition.query_limit
                    query = query_point + " UNION ALL " + query_others
                    if query_limit > 0:
                        query = query + " LIMIT " + str(query_limit)
                else:
                    if luxgetfeaturedefinition.id_column is not None:
                        query = query_1 + luxgetfeaturedefinition.id_column +\
                            " = '" + fid + "'"
                    else:
                        query = query_1 + " id = '" + fid + "'"

                session = self._get_session(luxgetfeaturedefinition.engine_gfi)
                res = session.execute(query)
                rows = res.fetchall()

                if (luxgetfeaturedefinition.additional_info_function
                    is not None and
                    len(luxgetfeaturedefinition.
                        additional_info_function) > 0):

                    features = eval(luxgetfeaturedefinition.
                                    additional_info_function)

                    if len(features) > 0:
                        results.append(
                            self.to_featureinfo(
                                features,
                                luxgetfeaturedefinition.layer,
                                luxgetfeaturedefinition.template,
                                is_ordered,
                                luxgetfeaturedefinition.has_profile,
                                luxgetfeaturedefinition.remote_template))
                else:
                    features = []
                    for row in rows:
                        geometry = geojson_loads(row['st_asgeojson'])
                        attributes = dict(row)
                        if luxgetfeaturedefinition.id_column in row:
                            featureid = row[luxgetfeaturedefinition.id_column]
                        else:
                            if 'id' in row:
                                featureid = row['id']
                            else:
                                featureid = None
                        f = self.to_feature(
                            luxgetfeaturedefinition.layer,
                            featureid,
                            geometry,
                            attributes,
                            luxgetfeaturedefinition.attributes_to_remove,
                            luxgetfeaturedefinition.columns_order,
                            luxgetfeaturedefinition.geometry_column)
                        features.append(f)
                    if len(features) > 0:
                        if fid is None:
                            results.append(
                                self.to_featureinfo(
                                    self.remove_features_outside_tolerance(
                                        features, coordinates_small_box),
                                    luxgetfeaturedefinition.layer,
                                    luxgetfeaturedefinition.template,
                                    is_ordered,
                                    luxgetfeaturedefinition.has_profile,
                                    luxgetfeaturedefinition.remote_template))
                        else:
                            results.append(
                                self.to_featureinfo(
                                    features,
                                    luxgetfeaturedefinition.layer,
                                    luxgetfeaturedefinition.template,
                                    is_ordered,
                                    luxgetfeaturedefinition.has_profile,
                                    luxgetfeaturedefinition.remote_template))
            if (luxgetfeaturedefinition is not None and
                (luxgetfeaturedefinition.rest_url is None or
                    len(luxgetfeaturedefinition.rest_url) == 0) and
                (luxgetfeaturedefinition.query is None or
                    len(luxgetfeaturedefinition.query) == 0)):
                x = self.request.params.get('X', None)
                y = self.request.params.get('Y', None)
                width = self.request.params.get('WIDTH', None)
                height = self.request.params.get('HEIGHT', None)
                bbox = self.request.params.get('BBOX', None)
                if x is None or y is None or width is None or\
                   height is None or bbox is None:
                    return HTTPBadRequest()
                srs = self.request.params.get('srs', 'EPSG:2169')
                internal_wms = DBSession.query(LuxLayerInternalWMS).filter(
                    LuxLayerInternalWMS.id == luxgetfeaturedefinition.layer).\
                    first()
                url = internal_wms.url
                ogc_layers = internal_wms.layers

                features = self._ogc_getfeatureinfo(
                    url, x, y, width, height,
                    ogc_layers, bbox, srs, luxgetfeaturedefinition.layer,
                    luxgetfeaturedefinition.attributes_to_remove,
                    luxgetfeaturedefinition.columns_order)
                if len(features) > 0:
                    if (luxgetfeaturedefinition.additional_info_function
                        is not None and
                        len(luxgetfeaturedefinition.
                            additional_info_function) > 0):
                        features = eval(luxgetfeaturedefinition.
                                        additional_info_function)
                    is_ordered =\
                        luxgetfeaturedefinition.columns_order is not None\
                        and len(luxgetfeaturedefinition.columns_order) > 0
                    results.append(
                        self.to_featureinfo(
                            features,
                            luxgetfeaturedefinition.layer,
                            luxgetfeaturedefinition.template,
                            is_ordered,
                            luxgetfeaturedefinition.has_profile,
                            luxgetfeaturedefinition.remote_template))

            if (luxgetfeaturedefinition is not None and
                luxgetfeaturedefinition.rest_url is not None and
                    len(luxgetfeaturedefinition.rest_url) > 0):
                if fid is None:
                    features = self._get_external_data(
                        luxgetfeaturedefinition.layer,
                        luxgetfeaturedefinition.rest_url,
                        luxgetfeaturedefinition.id_column,
                        big_box, None, None,
                        luxgetfeaturedefinition.attributes_to_remove,
                        luxgetfeaturedefinition.columns_order)

                else:
                    features = self._get_external_data(
                        luxgetfeaturedefinition.layer,
                        luxgetfeaturedefinition.rest_url,
                        luxgetfeaturedefinition.id_column,
                        None, fid, None,
                        luxgetfeaturedefinition.attributes_to_remove,
                        luxgetfeaturedefinition.columns_order)
                if len(features) > 0:
                    if (luxgetfeaturedefinition.additional_info_function
                        is not None and
                        len(luxgetfeaturedefinition.
                            additional_info_function) > 0):
                        features = eval(luxgetfeaturedefinition.
                                        additional_info_function)
                    is_ordered =\
                        luxgetfeaturedefinition.columns_order is not None\
                        and len(luxgetfeaturedefinition.columns_order) > 0
                    if fid is None:
                        results.append(
                            self.to_featureinfo(
                                self.remove_features_outside_tolerance(
                                    features, coordinates_small_box),
                                luxgetfeaturedefinition.layer,
                                luxgetfeaturedefinition.template,
                                is_ordered,
                                luxgetfeaturedefinition.has_profile,
                                luxgetfeaturedefinition.remote_template))
                    else:
                        results.append(
                            self.to_featureinfo(
                                features,
                                luxgetfeaturedefinition.layer,
                                luxgetfeaturedefinition.template,
                                is_ordered,
                                luxgetfeaturedefinition.has_profile,
                                luxgetfeaturedefinition.remote_template))

        if self.request.params.get('tooltip', None) is not None:
            path = 'templates/tooltip/'
            localizer = get_localizer(self.request)
            server = TranslationStringFactory("geoportailv3-server")
            tooltips = TranslationStringFactory("geoportailv3-tooltips")
            client = TranslationStringFactory("geoportailv3-client")

            for r in results:
                l_template = r['template']
                filename = resource_filename('geoportailv3', path + l_template)
                template = l_template if isfile(filename) else 'default.html'
                features = r['features']
                context = {
                    "_s": lambda s: localizer.translate(server(s)),
                    "_t": lambda s: localizer.translate(tooltips(s)),
                    "_c": lambda s: localizer.translate(client(s)),
                    "features": features}
                if r['remote_template'] is not None and\
                   r['remote_template']:
                    data = ""
                    try:
                        url_remote = urllib2.urlopen(
                            l_template + "&render=apiv3", None, 15)
                        data = url_remote.read()
                    except Exception as e:
                        log.exception(e)
                        log.error(l_template)
                        return HTTPBadGateway()
                    remote_template = Template(data)
                    if "${features" in data:
                        r['tooltip'] =\
                            remote_template.render(features=features)
                    else:
                        r['tooltip'] =\
                            remote_template.render(feature=features[0])

                else:
                    r['tooltip'] = render(
                        'geoportailv3:' + path + template, context)

        return results
    def _get_external_data(self, layer_id, url, id_column='objectid',
                           bbox=None, featureid=None, cfg=None,
                           attributes_to_remove=None, columns_order=None,
                           where_clause=None):
        # ArcGIS Server REST API:
        # http://help.arcgis.com/en/arcgisserver/10.0/apis/rest/query.html
        # form example:
        # http://ws.geoportail.lu/ArcGIS/rest/services/wassergis/waassergis_mxd/MapServer/45/query
        #
        # params "params_meaning" (params_type) [predefined_values]:
        #
        # f              "Format" (choice) [html kml pjson]
        # geometry       "Filter Geometry" (string)
        # geometryType   "Geometry Type" (choice)
        #                [esriGeometryEnvelope esriGeometryPoint
        #                 esriGeometryPolyline esriGeometryPolygon
        #                 esriGeometryMultipoint]
        # inSR           "Input Spatial Reference (WKID)" (string)
        # objectIds      "The object IDs of this layer
        #                 / table to be queried" (string)
        # outFields      "Return Fields (Comma Separated or *)" (string)
        # outSR          "Output Spatial Reference (WKID)" (string)
        # returnGeometry "Return Geometry" (bool true/false)
        # spatialRel     "Spatial Relationship" (choice)
        #                [esriSpatialRelIntersects esriSpatialRelContains
        #                 esriSpatialRelCrosses
        #                 esriSpatialRelEnvelopeIntersects
        #                 esriSpatialRelIndexIntersects esriSpatialRelOverlaps
        #                 esriSpatialRelTouches esriSpatialRelWithin]
        # text           "nom" (string)
        # where          "Where" (string)
        #
        # example:
        # http://ws.geoportail.lu/ArcGIS/rest/services/wassergis/waassergis_mxd/MapServer/45/query?text=&geometry=69000%2C124000%2C70000%2C125000&geometryType=esriGeometryEnvelope&inSR=2169&spatialRel=esriSpatialRelIntersects&where=&returnGeometry=true&outSR=&outFields=&f=pjson

        body = {'f': 'json',
                'geometry': '',
                'geometryType': '',
                'inSR': '2169',
                'outSR': '2169',
                'returnGeometry': 'true',
                'spatialRel': '',
                'text': '',
                'where': '',
                'outFields': '*',
                'objectIds': ''}
        if id_column is None:
            id_column = 'objectid'

        if featureid is not None:
            if id_column == 'objectid':
                body['objectIds'] = featureid
            else:
                body['where'] = id_column + ' = \'' + featureid + '\''
        elif bbox is not None:
            body['geometry'] = bbox
            body['geometryType'] = 'esriGeometryEnvelope'
            body['spatialRel'] = 'esriSpatialRelIntersects'
        elif where_clause is not None:
            body['where'] = where_clause
        else:
            return []

        if url.find("@") > -1:
            url = self._get_url_with_token(url)

        # construct url for get request
        separator = '?'
        if url.find(separator) > 0:
            separator = '&'
        query = '%s%s%s' % (url, separator, urlencode(body))
        try:
            result = urllib2.urlopen(query, None, 15)
            content = result.read()
        except Exception as e:
            log.exception(e)
            return []

        features = []
        try:
            esricoll = geojson_loads(content)
        except:
            raise
        if 'fields' in esricoll:
            fields = esricoll['fields']
        else:
            fields = []
        if 'features' in esricoll:
            for rawfeature in esricoll['features']:
                geometry = ''
                if (rawfeature['geometry'] and
                    'x' in rawfeature['geometry'] and
                        'y' in rawfeature['geometry']):
                    geometry = {'type': 'Point',
                                'coordinates': [rawfeature['geometry']['x'],
                                                rawfeature['geometry']['y']]}
                elif (rawfeature['geometry'] and
                      'x' in rawfeature['geometry'] and
                      'Y' in rawfeature['geometry']):
                    geometry = {'type': 'Point',
                                'coordinates': [rawfeature['geometry']['x'],
                                                rawfeature['geometry']['Y']]}
                elif (rawfeature['geometry'] and
                      'paths' in rawfeature['geometry'] and
                      len(rawfeature['geometry']['paths']) > 0):
                    geometry = {'type': 'MultiLineString',
                                'coordinates': rawfeature['geometry']['paths']}
                elif (rawfeature['geometry'] and
                      'rings' in rawfeature['geometry'] and
                      len(rawfeature['geometry']['rings']) > 0):
                        if len(rawfeature['geometry']['rings']) == 1:
                            geometry = {'type': 'Polygon',
                                        'coordinates':
                                            rawfeature['geometry']['rings']}
                        else:
                            coordinates = []
                            curpolygon = []
                            for ring in rawfeature['geometry']['rings']:
                                if not LinearRing(ring).is_ccw:
                                    if len(curpolygon) > 0:
                                        coordinates.append(curpolygon)
                                        curpolygon = []
                                curpolygon.append(ring)

                            if len(curpolygon) > 0:
                                coordinates.append(curpolygon)

                            geometry = {'type': 'MultiPolygon',
                                        'coordinates': coordinates}

                if geometry != '':
                    alias = {}
                    if id_column in rawfeature['attributes']:
                        fid = rawfeature['attributes'][id_column]
                    else:
                        fid = None
                    for attribute in rawfeature['attributes']:
                        for field in fields:
                            if (field['name'] == attribute and
                                field['alias'] is not None and
                                    len(field['alias']) > 0):
                                alias[field['alias']] = field['name']
                                break
                    for key, value in alias.items():
                        rawfeature['attributes'][key] =\
                            rawfeature['attributes'].pop(value)
                    f = self.to_feature(layer_id, fid,
                                        geometry,
                                        rawfeature['attributes'],
                                        attributes_to_remove,
                                        columns_order, 'geom', alias)
                    features.append(f)
        return features
Esempio n. 23
0
    def test_empty_params_list(self, m):
        result = tuple(site_geojson_generator([]))
        feature = geojson_loads(''.join(result).replace('\n', ''))

        self.assertEqual(feature['type'], 'FeatureCollection')
        self.assertEqual(len(feature['features']), 0)
Esempio n. 24
0
    def _get_external_data(self,
                           layer_id,
                           url,
                           id_column='objectid',
                           bbox=None,
                           featureid=None,
                           cfg=None,
                           attributes_to_remove=None,
                           columns_order=None):
        # ArcGIS Server REST API:
        # http://help.arcgis.com/en/arcgisserver/10.0/apis/rest/query.html
        # form example:
        # http://ws.geoportail.lu/ArcGIS/rest/services/wassergis/waassergis_mxd/MapServer/45/query
        #
        # params "params_meaning" (params_type) [predefined_values]:
        #
        # f              "Format" (choice) [html kml pjson]
        # geometry       "Filter Geometry" (string)
        # geometryType   "Geometry Type" (choice)
        #                [esriGeometryEnvelope esriGeometryPoint
        #                 esriGeometryPolyline esriGeometryPolygon
        #                 esriGeometryMultipoint]
        # inSR           "Input Spatial Reference (WKID)" (string)
        # objectIds      "The object IDs of this layer
        #                 / table to be queried" (string)
        # outFields      "Return Fields (Comma Separated or *)" (string)
        # outSR          "Output Spatial Reference (WKID)" (string)
        # returnGeometry "Return Geometry" (bool true/false)
        # spatialRel     "Spatial Relationship" (choice)
        #                [esriSpatialRelIntersects esriSpatialRelContains
        #                 esriSpatialRelCrosses
        #                 esriSpatialRelEnvelopeIntersects
        #                 esriSpatialRelIndexIntersects esriSpatialRelOverlaps
        #                 esriSpatialRelTouches esriSpatialRelWithin]
        # text           "nom" (string)
        # where          "Where" (string)
        #
        # example:
        # http://ws.geoportail.lu/ArcGIS/rest/services/wassergis/waassergis_mxd/MapServer/45/query?text=&geometry=69000%2C124000%2C70000%2C125000&geometryType=esriGeometryEnvelope&inSR=2169&spatialRel=esriSpatialRelIntersects&where=&returnGeometry=true&outSR=&outFields=&f=pjson

        body = {
            'f': 'json',
            'geometry': '',
            'geometryType': '',
            'inSR': '2169',
            'outSR': '2169',
            'returnGeometry': 'true',
            'spatialRel': '',
            'text': '',
            'where': '',
            'outFields': '*',
            'objectIds': ''
        }
        if id_column is None:
            id_column = 'objectid'
        if featureid is not None:
            body['objectIds'] = featureid
        elif bbox is not None:
            body['geometry'] = bbox
            body['geometryType'] = 'esriGeometryEnvelope'
            body['spatialRel'] = 'esriSpatialRelIntersects'
        else:
            return []

        # construct url for get request
        separator = "?"
        if url.find('?'):
            separator = "&"
        query = '%s%s%s' % (url, separator, urlencode(body))

        try:
            result = urllib2.urlopen(query, None, 15)
            content = result.read()
        except:
            traceback.print_exc(file=sys.stdout)
            return []

        features = []
        try:
            esricoll = geojson_loads(content)
        except:
            raise
        fields = esricoll['fields']
        if 'features' in esricoll:
            for rawfeature in esricoll['features']:
                geometry = ''
                if (rawfeature['geometry'] and 'x' in rawfeature['geometry']
                        and 'y' in rawfeature['geometry']):
                    geometry = {
                        'type':
                        'Point',
                        'coordinates': [
                            rawfeature['geometry']['x'],
                            rawfeature['geometry']['y']
                        ]
                    }
                elif (rawfeature['geometry'] and 'x' in rawfeature['geometry']
                      and 'Y' in rawfeature['geometry']):
                    geometry = {
                        'type':
                        'Point',
                        'coordinates': [
                            rawfeature['geometry']['x'],
                            rawfeature['geometry']['Y']
                        ]
                    }
                elif (rawfeature['geometry']
                      and 'paths' in rawfeature['geometry']
                      and len(rawfeature['geometry']['paths']) > 0):
                    geometry = {
                        'type': 'MultiLineString',
                        'coordinates': rawfeature['geometry']['paths']
                    }
                elif (rawfeature['geometry']
                      and 'rings' in rawfeature['geometry']
                      and len(rawfeature['geometry']['rings']) > 0):
                    if len(rawfeature['geometry']['rings']) == 1:
                        geometry = {
                            'type': 'Polygon',
                            'coordinates': rawfeature['geometry']['rings']
                        }
                    else:
                        coordinates = []
                        curpolygon = []
                        for ring in rawfeature['geometry']['rings']:
                            if not LinearRing(ring).is_ccw:
                                if len(curpolygon) > 0:
                                    coordinates.append(curpolygon)
                                    curpolygon = []
                            curpolygon.append(ring)

                        if len(curpolygon) > 0:
                            coordinates.append(curpolygon)

                        geometry = {
                            'type': 'MultiPolygon',
                            'coordinates': coordinates
                        }

                if geometry != '':
                    alias = {}
                    if id_column in rawfeature['attributes']:
                        fid = rawfeature['attributes'][id_column]
                    else:
                        fid = None
                    for attribute in rawfeature['attributes']:
                        for field in fields:
                            if (field['name'] == attribute
                                    and field['alias'] is not None
                                    and len(field['alias']) > 0):
                                alias[field['alias']] = field['name']
                                break
                    for key, value in alias.items():
                        rawfeature['attributes'][key] =\
                            rawfeature['attributes'].pop(value)

                    f = self.to_feature(layer_id, fid, geometry,
                                        rawfeature['attributes'],
                                        attributes_to_remove, columns_order)
                    features.append(f)
        return features
Esempio n. 25
0
    def test_empty_params_list(self, m):
        result = tuple(site_geojson_generator([]))
        feature = geojson_loads(''.join(result).replace('\n', ''))

        self.assertEqual(feature['type'], 'FeatureCollection')
        self.assertEqual(len(feature['features']), 0)
Esempio n. 26
0
    def get_info(self, fid, coordinates_big_box, coordinates_small_box,
                 results, layers, big_box):
        luxgetfeaturedefinitions = self.get_lux_feature_definition(layers)
        for luxgetfeaturedefinition in luxgetfeaturedefinitions:
            if (luxgetfeaturedefinition is not None and
                luxgetfeaturedefinition.engine_gfi is not None and
                luxgetfeaturedefinition.query is not None and
                    len(luxgetfeaturedefinition.query) > 0):
                is_ordered = luxgetfeaturedefinition.columns_order is not None\
                    and len(luxgetfeaturedefinition.columns_order) > 0
                query_1 = luxgetfeaturedefinition.query
                if "WHERE" in query_1.upper():
                    query_1 = query_1 + " AND "
                else:
                    query_1 = query_1 + " WHERE "

                if "SELECT" in query_1.upper():
                    query_1 = query_1.replace(
                        "SELECT",
                        "SELECT ST_AsGeoJSON (%(geom)s), "
                        % {'geom': luxgetfeaturedefinition.geometry_column}, 1)

                else:
                    query_1 = "SELECT *,ST_AsGeoJSON(%(geom)s) FROM "\
                        % {'geom': luxgetfeaturedefinition.geometry_column} +\
                        query_1
                if fid is None:
                    query_point = query_1 + "ST_Intersects( %(geom)s, "\
                        "ST_MakeEnvelope(%(left)s, %(bottom)s, %(right)s,"\
                        "%(top)s, 2169) ) AND ST_NRings(%(geom)s) = 0"\
                        % {'left': coordinates_big_box[0],
                           'bottom': coordinates_big_box[1],
                           'right': coordinates_big_box[2],
                           'top': coordinates_big_box[3],
                           'geom': luxgetfeaturedefinition.geometry_column}

                    query_others = query_1 + "ST_Intersects( %(geom)s,"\
                        " ST_MakeEnvelope (%(left)s, %(bottom)s, %(right)s,"\
                        " %(top)s, 2169) ) AND  ST_NRings(%(geom)s) > 0"\
                        % {'left': coordinates_small_box[0],
                           'bottom': coordinates_small_box[1],
                           'right': coordinates_small_box[2],
                           'top': coordinates_small_box[3],
                           'geom': luxgetfeaturedefinition.geometry_column}

                    query = query_point + " UNION ALL " + query_others +\
                        " LIMIT 20"
                else:
                    if luxgetfeaturedefinition.id_column is not None:
                        query = query_1 + luxgetfeaturedefinition.id_column +\
                            " = '" + fid + "'"
                    else:
                        query = query_1 + " id = '" + fid + "'"

                session = self._get_session(luxgetfeaturedefinition.engine_gfi)
                res = session.execute(query)
                rows = res.fetchall()

                if (luxgetfeaturedefinition.additional_info_function
                    is not None and
                    len(luxgetfeaturedefinition.
                        additional_info_function) > 0):

                    features = eval(luxgetfeaturedefinition.
                                    additional_info_function)

                    if len(features) > 0:
                        results.append(
                            self.to_featureinfo(
                                features,
                                luxgetfeaturedefinition.layer,
                                luxgetfeaturedefinition.template,
                                is_ordered,
                                luxgetfeaturedefinition.has_profile,
                                luxgetfeaturedefinition.remote_template))
                else:
                    features = []
                    for row in rows:
                        geometry = geojson_loads(row['st_asgeojson'])
                        attributes = dict(row)
                        if luxgetfeaturedefinition.id_column in row:
                            featureid = row[luxgetfeaturedefinition.id_column]
                        else:
                            if 'id' in row:
                                featureid = row['id']
                            else:
                                featureid = None
                        f = self.to_feature(
                            luxgetfeaturedefinition.layer,
                            featureid,
                            geometry,
                            attributes,
                            luxgetfeaturedefinition.attributes_to_remove,
                            luxgetfeaturedefinition.columns_order,
                            luxgetfeaturedefinition.geometry_column)
                        features.append(f)
                    if len(features) > 0:
                        if fid is None:
                            results.append(
                                self.to_featureinfo(
                                    self.remove_features_outside_tolerance(
                                        features, coordinates_small_box),
                                    luxgetfeaturedefinition.layer,
                                    luxgetfeaturedefinition.template,
                                    is_ordered,
                                    luxgetfeaturedefinition.has_profile,
                                    luxgetfeaturedefinition.remote_template))
                        else:
                            results.append(
                                self.to_featureinfo(
                                    features,
                                    luxgetfeaturedefinition.layer,
                                    luxgetfeaturedefinition.template,
                                    is_ordered,
                                    luxgetfeaturedefinition.has_profile,
                                    luxgetfeaturedefinition.remote_template))
            if (luxgetfeaturedefinition is not None and
                (luxgetfeaturedefinition.rest_url is None or
                    len(luxgetfeaturedefinition.rest_url) == 0) and
                (luxgetfeaturedefinition.query is None or
                    len(luxgetfeaturedefinition.query) == 0)):
                x = self.request.params.get('X', None)
                y = self.request.params.get('Y', None)
                width = self.request.params.get('WIDTH', None)
                height = self.request.params.get('HEIGHT', None)
                bbox = self.request.params.get('BBOX', None)
                if x is None or y is None or width is None or\
                   height is None or bbox is None:
                    return HTTPBadRequest()
                srs = self.request.params.get('srs', 'EPSG:2169')
                internal_wms = DBSession.query(LuxLayerInternalWMS).filter(
                    LuxLayerInternalWMS.id == luxgetfeaturedefinition.layer).\
                    first()
                url = internal_wms.url
                ogc_layers = internal_wms.layers

                features = self._ogc_getfeatureinfo(
                    url, x, y, width, height,
                    ogc_layers, bbox, srs, luxgetfeaturedefinition.layer,
                    luxgetfeaturedefinition.attributes_to_remove,
                    luxgetfeaturedefinition.columns_order)
                if len(features) > 0:
                    if (luxgetfeaturedefinition.additional_info_function
                        is not None and
                        len(luxgetfeaturedefinition.
                            additional_info_function) > 0):
                        features = eval(luxgetfeaturedefinition.
                                        additional_info_function)
                    is_ordered =\
                        luxgetfeaturedefinition.columns_order is not None\
                        and len(luxgetfeaturedefinition.columns_order) > 0
                    results.append(
                        self.to_featureinfo(
                            features,
                            luxgetfeaturedefinition.layer,
                            luxgetfeaturedefinition.template,
                            is_ordered,
                            luxgetfeaturedefinition.has_profile,
                            luxgetfeaturedefinition.remote_template))

            if (luxgetfeaturedefinition is not None and
                luxgetfeaturedefinition.rest_url is not None and
                    len(luxgetfeaturedefinition.rest_url) > 0):
                if fid is None:
                    features = self._get_external_data(
                        luxgetfeaturedefinition.layer,
                        luxgetfeaturedefinition.rest_url,
                        luxgetfeaturedefinition.id_column,
                        big_box, None, None,
                        luxgetfeaturedefinition.attributes_to_remove,
                        luxgetfeaturedefinition.columns_order)

                else:
                    features = self._get_external_data(
                        luxgetfeaturedefinition.layer,
                        luxgetfeaturedefinition.rest_url,
                        luxgetfeaturedefinition.id_column,
                        None, fid, None,
                        luxgetfeaturedefinition.attributes_to_remove,
                        luxgetfeaturedefinition.columns_order)
                if len(features) > 0:
                    if (luxgetfeaturedefinition.additional_info_function
                        is not None and
                        len(luxgetfeaturedefinition.
                            additional_info_function) > 0):
                        features = eval(luxgetfeaturedefinition.
                                        additional_info_function)
                    is_ordered =\
                        luxgetfeaturedefinition.columns_order is not None\
                        and len(luxgetfeaturedefinition.columns_order) > 0
                    if fid is None:
                        results.append(
                            self.to_featureinfo(
                                self.remove_features_outside_tolerance(
                                    features, coordinates_small_box),
                                luxgetfeaturedefinition.layer,
                                luxgetfeaturedefinition.template,
                                is_ordered,
                                luxgetfeaturedefinition.has_profile,
                                luxgetfeaturedefinition.remote_template))
                    else:
                        results.append(
                            self.to_featureinfo(
                                features,
                                luxgetfeaturedefinition.layer,
                                luxgetfeaturedefinition.template,
                                is_ordered,
                                luxgetfeaturedefinition.has_profile,
                                luxgetfeaturedefinition.remote_template))

        if self.request.params.get('tooltip', None) is not None:
            path = 'templates/tooltip/'
            localizer = get_localizer(self.request)
            server = TranslationStringFactory("geoportailv3-server")
            tooltips = TranslationStringFactory("geoportailv3-tooltips")
            client = TranslationStringFactory("geoportailv3-client")

            for r in results:
                l_template = r['template']
                filename = resource_filename('geoportailv3', path + l_template)
                template = l_template if isfile(filename) else 'default.html'
                features = r['features']
                context = {
                    "_s": lambda s: localizer.translate(server(s)),
                    "_t": lambda s: localizer.translate(tooltips(s)),
                    "_c": lambda s: localizer.translate(client(s)),
                    "features": features}
                if r['remote_template'] is not None and\
                   r['remote_template']:
                    data = ""
                    try:
                        url_remote = urllib2.urlopen(
                            l_template + "&render=apiv3", None, 15)
                        data = url_remote.read()
                    except Exception as e:
                        log.exception(e)
                        log.error(l_template)
                        return HTTPBadGateway()
                    remote_template = Template(data)
                    if "${features" in data:
                        r['tooltip'] =\
                            remote_template.render(features=features)
                    else:
                        r['tooltip'] =\
                            remote_template.render(feature=features[0])

                else:
                    r['tooltip'] = render(
                        'geoportailv3:' + path + template, context)

        return results
Esempio n. 27
0
    def get_feature_info(self):
        fid = self.request.params.get('fid', None)

        if fid is not None:
            layers, fid = fid.split('_', 1)
            if layers is None or fid is None:
                return HTTPBadRequest()
        else:
            layers = self.request.params.get('layers', None)
            if layers is None:
                return HTTPBadRequest()
            big_box = self.request.params.get('box1', None)
            small_box = self.request.params.get('box2', None)
            if big_box is None or small_box is None:
                return HTTPBadRequest()

        luxgetfeaturedefinitions = self.get_lux_feature_definition(layers)
        if fid is None:
            coordinates_big_box = big_box.split(',')
            coordinates_small_box = small_box.split(',')

        results = []
        for luxgetfeaturedefinition in luxgetfeaturedefinitions:
            if (luxgetfeaturedefinition is not None
                    and luxgetfeaturedefinition.engine is not None
                    and luxgetfeaturedefinition.query is not None
                    and len(luxgetfeaturedefinition.query) > 0):
                engine = sqlahelper.get_engine(luxgetfeaturedefinition.engine)
                is_ordered = luxgetfeaturedefinition.columns_order is not None\
                    and len(luxgetfeaturedefinition.columns_order) > 0
                query_1 = luxgetfeaturedefinition.query
                if "WHERE" in query_1.upper():
                    query_1 = query_1 + " AND "
                else:
                    query_1 = query_1 + " WHERE "

                if "SELECT" in query_1.upper():
                    query_1 = query_1.replace(
                        "SELECT", "SELECT ST_AsGeoJSON (%(geom)s), " %
                        {'geom': luxgetfeaturedefinition.geometry_column}, 1)

                else:
                    query_1 = "SELECT *,ST_AsGeoJSON(%(geom)s) FROM "\
                        % {'geom': luxgetfeaturedefinition.geometry_column} +\
                        query_1
                if fid is None:
                    query_point = query_1 + "ST_Intersects( %(geom)s, "\
                        "ST_MakeEnvelope(%(left)s, %(bottom)s, %(right)s,"\
                        "%(top)s, 2169) ) AND ST_NRings(%(geom)s) = 0"\
                        % {'left': coordinates_big_box[0],
                           'bottom': coordinates_big_box[1],
                           'right': coordinates_big_box[2],
                           'top': coordinates_big_box[3],
                           'geom': luxgetfeaturedefinition.geometry_column}

                    query_others = query_1 + "ST_Intersects( %(geom)s,"\
                        " ST_MakeEnvelope (%(left)s, %(bottom)s, %(right)s,"\
                        " %(top)s, 2169) ) AND  ST_NRings(%(geom)s) > 0"\
                        % {'left': coordinates_small_box[0],
                           'bottom': coordinates_small_box[1],
                           'right': coordinates_small_box[2],
                           'top': coordinates_small_box[3],
                           'geom': luxgetfeaturedefinition.geometry_column}

                    query = query_point + " UNION ALL " + query_others +\
                        " LIMIT 20"
                else:
                    if luxgetfeaturedefinition.id_column is not None:
                        query = query_1 + luxgetfeaturedefinition.id_column +\
                            " = '" + fid + "'"
                    else:
                        query = query_1 + " id = '" + fid + "'"
                res = engine.execute(query)
                rows = res.fetchall()

                if (luxgetfeaturedefinition.additional_info_function
                        is not None and
                        len(luxgetfeaturedefinition.additional_info_function) >
                        0):

                    features = eval(
                        luxgetfeaturedefinition.additional_info_function)

                    if len(features) > 0:
                        results.append(
                            self.to_featureinfo(
                                features, luxgetfeaturedefinition.layer,
                                luxgetfeaturedefinition.template, is_ordered,
                                luxgetfeaturedefinition.remote_template))
                else:
                    features = []
                    for row in rows:
                        geometry = geojson_loads(row['st_asgeojson'])
                        attributes = dict(row)
                        if luxgetfeaturedefinition.id_column in row:
                            featureid = row[luxgetfeaturedefinition.id_column]
                        else:
                            if 'id' in row:
                                featureid = row['id']
                            else:
                                featureid = None
                        f = self.to_feature(
                            luxgetfeaturedefinition.layer, featureid, geometry,
                            attributes,
                            luxgetfeaturedefinition.attributes_to_remove,
                            luxgetfeaturedefinition.columns_order,
                            luxgetfeaturedefinition.geometry_column)
                        features.append(f)
                    if len(features) > 0:
                        if fid is None:
                            results.append(
                                self.to_featureinfo(
                                    self.remove_features_outside_tolerance(
                                        features, coordinates_small_box),
                                    luxgetfeaturedefinition.layer,
                                    luxgetfeaturedefinition.template,
                                    is_ordered,
                                    luxgetfeaturedefinition.remote_template))
                        else:
                            results.append(
                                self.to_featureinfo(
                                    features, luxgetfeaturedefinition.layer,
                                    luxgetfeaturedefinition.template,
                                    is_ordered,
                                    luxgetfeaturedefinition.remote_template))
            if (luxgetfeaturedefinition is not None
                    and luxgetfeaturedefinition.rest_url is not None
                    and len(luxgetfeaturedefinition.rest_url) > 0):
                if fid is None:
                    features = self._get_external_data(
                        luxgetfeaturedefinition.layer,
                        luxgetfeaturedefinition.rest_url,
                        luxgetfeaturedefinition.id_column, big_box, None, None,
                        luxgetfeaturedefinition.attributes_to_remove,
                        luxgetfeaturedefinition.columns_order)
                else:
                    features = self._get_external_data(
                        luxgetfeaturedefinition.layer,
                        luxgetfeaturedefinition.rest_url,
                        luxgetfeaturedefinition.id_column, None, fid, None,
                        luxgetfeaturedefinition.attributes_to_remove,
                        luxgetfeaturedefinition.columns_order)
                if len(features) > 0:
                    if (luxgetfeaturedefinition.additional_info_function
                            is not None and len(luxgetfeaturedefinition.
                                                additional_info_function) > 0):
                        features = eval(
                            luxgetfeaturedefinition.additional_info_function)
                    is_ordered =\
                        luxgetfeaturedefinition.columns_order is not None\
                        and len(luxgetfeaturedefinition.columns_order) > 0
                    if fid is None:
                        results.append(
                            self.to_featureinfo(
                                self.remove_features_outside_tolerance(
                                    features, coordinates_small_box),
                                luxgetfeaturedefinition.layer,
                                luxgetfeaturedefinition.template, is_ordered,
                                luxgetfeaturedefinition.remote_template))
                    else:
                        results.append(
                            self.to_featureinfo(
                                features, luxgetfeaturedefinition.layer,
                                luxgetfeaturedefinition.template, is_ordered,
                                luxgetfeaturedefinition.remote_template))
        return results