def __coord_transform__(self, coord, source_crs): """ Transforms the specified coordinates from the specified CRS to the configured target CRS and creates a point geometry. Args: coord (tuple): The coordinates to transform (x, y). source_crs (intorstr): The source CRS Returns: shapely.geometry.Point or shapely.geometry.Polygon: The transformed coordinates as Point. """ log.debug('----- Transforming Coordinates: -----') log.debug('----- X/Y Coordinates: {0} -----'.format(coord)) epsg = 'epsg:{0}' srid = Config.get('srid') log.debug('----- srid from config (to_srs): {0} -----'.format(srid)) log.debug( '----- srid from source (from_srs): {0} -----'.format(source_crs)) rp = Reprojector() x, y = rp.transform(coord, from_srs=epsg.format(source_crs), to_srs=epsg.format(srid)) log.debug( '----- X/Y coordinates after transformation: ({0}, {1}) -----'. format(x, y)) return Point(x, y)
def read(self, street_name, zip_code, street_number): """ Queries an address using the federal GeoAdmin API location search. Args: street_name (unicode): The name of the street for the desired address. zip_code (int): The postal zipcode for the desired address. street_number (unicode): The house or so called street number of the desired address. """ headers = { 'Referer': 'http://bl.ch' # TODO: Remove this header when referer is not needed anymore! } params = { 'type': self._type, 'origins': self._origins, 'searchText': u'{0} {1} {2}'.format(zip_code, street_name, street_number) } response = requests.get(self._geoadmin_url, params=params, proxies=self._proxies, headers=headers) if response.status_code == requests.codes.ok: rp = Reprojector() srid = Config.get('srid') self.records = list() data = response.json() if 'results' in data: for item in data.get('results'): attrs = item.get('attrs') if isinstance(attrs, dict) and attrs.get('origin') == 'address': x, y = rp.transform( (attrs.get('lon'), attrs.get('lat')), to_srs=srid) self.records.append( AddressRecord(street_name=street_name, zip_code=zip_code, street_number=street_number, geom='POINT({x} {y})'.format(x=x, y=y))) else: response.raise_for_status()
def __coord_transform__(self, coord, source_crs): """ Transforms the specified coordinates from the specified CRS to the configured target CRS and creates a point geometry. Args: coord (tuple): The coordinates to transform (x, y). source_crs (intorstr): The source CRS Returns: shapely.geometry.Point or shapely.geometry.Polygon: The transformed coordinates as Point. """ epsg = 'epsg:{0}' srid = Config.get('srid') rp = Reprojector() x, y = rp.transform(coord, from_srs=epsg.format(source_crs), to_srs=epsg.format(srid)) return Point(x, y)
def read(self, params, street_name, zip_code, street_number): """ Queries an address using the federal GeoAdmin API location search. Args: params (pyramid_oereb.views.webservice.Parameter): The parameters of the extract request. street_name (unicode): The name of the street for the desired address. zip_code (int): The postal zipcode for the desired address. street_number (unicode): The house or so called street number of the desired address. """ headers = {} if self._referer is not None: headers.update({'Referer': self._referer}) request_params = { 'type': self._type, 'origins': self._origins, 'searchText': u'{0} {1} {2}'.format(zip_code, street_name, street_number) } response = requests.get(self._geoadmin_url, params=request_params, proxies=self._proxies, headers=headers) if response.status_code == requests.codes.ok: rp = Reprojector() srid = Config.get('srid') self.records = list() data = response.json() if 'results' in data: for item in data.get('results'): attrs = item.get('attrs') if isinstance(attrs, dict) and attrs.get('origin') == 'address': x, y = rp.transform( (attrs.get('lon'), attrs.get('lat')), to_srs=srid) self.records.append( AddressRecord(street_name=street_name, zip_code=zip_code, street_number=street_number, geom=Point(x, y))) else: response.raise_for_status()
class Geometry(object): TAG_POINT_LV03 = 'Punkt_LV03' TAG_POINT_LV95 = 'Punkt_LV95' TAG_LINE_LV03 = 'Linie_LV03' TAG_LINE_LV95 = 'Linie_LV95' TAG_AREA_LV03 = 'Flaeche_LV03' TAG_AREA_LV95 = 'Flaeche_LV95' TAG_LAW_STATUS = 'Rechtsstatus' TAG_PUBLISHED_FROM = 'publiziertAb' TAG_GEO_METADATA = 'MetadatenGeobasisdaten' TAG_PUBLIC_LAW_RESTRICTION = 'Eigentumsbeschraenkung' TAG_RESPONSIBLE_OFFICE = 'ZustaendigeStelle' TAG_COORD = 'COORD' TAG_ARC = 'ARC' def __init__(self, session, model, geometry_type, srid, arc_max_diff=0.001, arc_precision=3): self._session = session self._model = model self._geometry_type = geometry_type self._to_srs = srid self._arc_max_diff = arc_max_diff self._arc_precision = arc_precision self._log = logging.getLogger('import_federal_topic') self._reprojector = Reprojector() def parse(self, geometry): # pragma: no cover instance = self._model( id=geometry.attrib['TID'], law_status=parse_string(geometry, self.TAG_LAW_STATUS), published_from=parse_string(geometry, self.TAG_PUBLISHED_FROM), geo_metadata=parse_string(geometry, self.TAG_GEO_METADATA), public_law_restriction_id=parse_ref( geometry, self.TAG_PUBLIC_LAW_RESTRICTION), office_id=parse_ref(geometry, self.TAG_RESPONSIBLE_OFFICE), geom=self._parse_geom(geometry)) self._session.add(instance) def _parse_geom(self, geometry): geom_type = self._geometry_type.upper() geom = None # Check for LV95 geometry for element in geometry: tag = get_tag(element) if tag == self.TAG_POINT_LV95: geom = self._parse_point(element, 2056) elif tag == self.TAG_LINE_LV95: geom = self._parse_line(element, 2056) elif tag == self.TAG_AREA_LV95: geom = self._parse_area(element, 2056) # Check for LV03 geometry as fallback if geom is None: for element in geometry: tag = get_tag(element) if tag == self.TAG_POINT_LV03: geom = self._parse_point(element, 21781) elif tag == self.TAG_LINE_LV03: geom = self._parse_line(element, 21781) elif tag == self.TAG_AREA_LV03: geom = self._parse_area(element, 21781) # Wrap in collection if necessary if geom is not None: if geom_type == 'MULTIPOINT': geom = MultiPoint([geom]) elif geom_type == 'MULTILINESTRING': geom = MultiLineString([geom]) elif geom_type == 'MULTIPOLYGON': geom = MultiPolygon([geom]) elif geom_type == 'GEOMETRYCOLLECTION': geom = GeometryCollection([geom]) # Return geometry or None return None if geom is None else from_shape(geom, srid=2056) def _parse_coord(self, coord, srs): p = dict() for c in coord: if get_tag(c) == 'C1': p['x'] = float(c.text) elif get_tag(c) == 'C2': p['y'] = float(c.text) if srs == self._to_srs: return p['x'], p['y'] else: return self._reprojector.transform((p['x'], p['y']), from_srs=srs, to_srs=self._to_srs) def _parse_point(self, point, srs): for coord in point: return Point(self._parse_coord(coord, srs)) return None def _parse_line(self, line, srs): for polyline in line: coords = list() for coord in polyline: tag = get_tag(coord) if tag == self.TAG_COORD: coords.append(self._parse_coord(coord, srs)) elif tag == self.TAG_ARC: coords.extend(self._parse_arc(coord, coords[-1], srs)) else: self._log.warning( 'Found unsupported geometry element: {0}'.format(tag)) return LineString(coords) return None def _parse_area(self, area, srs): for surface in area: boundaries = list() for boundary in surface: boundaries.append(self._parse_line(boundary, srs)) exterior = boundaries[0].coords if len(boundaries) > 1: interiors = [interior.coords for interior in boundaries[1:]] else: interiors = None return Polygon(shell=exterior, holes=interiors) return None def _parse_arc(self, arc, start_point, srs): e = dict() a = dict() for element in arc: tag = get_tag(element) if tag == 'C1': e['x'] = float(element.text) elif tag == 'C2': e['y'] = float(element.text) elif tag == 'A1': a['x'] = float(element.text) elif tag == 'A2': a['y'] = float(element.text) if srs == self._to_srs: arc_point = (a['x'], a['y']) end_point = (e['x'], e['y']) else: arc_point = self._reprojector.transform((a['x'], a['y']), from_srs=srs, to_srs=self._to_srs) end_point = self._reprojector.transform((e['x'], e['y']), from_srs=srs, to_srs=self._to_srs) return stroke_arc(start_point, arc_point, end_point, self._arc_max_diff, self._arc_precision)