def test_transform(self): spot = session.query(Spot).get(1) # compare the coordinates using a tolerance, because they may vary on different systems assert_almost_equal(session.scalar(functions.x(spot.spot_location.transform(2249))), -3890517.6109559298) assert_almost_equal(session.scalar(functions.y(spot.spot_location.transform(2249))), 3627658.6746507999) ok_( session.query(Spot) .filter( Spot.spot_location.transform(2249) == WKTSpatialElement("POINT(-3890517.61095593 3627658.6746508)", 2249) ) .first() is not None ) eq_( session.scalar( functions.wkt( functions.transform( WKTSpatialElement( "POLYGON((743238 2967416,743238 2967450,743265 2967450,743265.625 2967416,743238 2967416))", 2249, ), 4326, ) ) ), u"POLYGON((-71.1776848522251 42.3902896512902,-71.1776843766326 42.3903829478009,-71.1775844305465 42.3903826677917,-71.1775825927231 42.3902893647987,-71.1776848522251 42.3902896512902))", )
def test_transform(self): spot = session.query(Spot).get(1) eq_(session.scalar(functions.wkt(spot.spot_location.transform(2249))), u"POINT(-3890517.610956 3627658.674651)") ok_( session.query(Spot) .filter( sqlite_functions.mbr_contains( functions.buffer(Spot.spot_location.transform(2249), 10), WKTSpatialElement("POINT(-3890517.610956 3627658.674651)", 2249), ) ) .first() is not None ) eq_( session.scalar( functions.wkt( functions.transform( WKTSpatialElement( "POLYGON((743238 2967416,743238 2967450,743265 2967450,743265.625 2967416,743238 2967416))", 2249, ), 4326, ) ) ), u"POLYGON((-71.177685 42.39029, -71.177684 42.390383, -71.177584 42.390383, -71.177583 42.390289, -71.177685 42.39029))", )
def _handle_parameters(self): response = self.request.response # Make sure the _LOCATION_ cookie is correctly set: The old version GUI # version used to store the map center and the zoom level which is not # understood by new GUI (which stores the map extent as 4 coordinates) if '_LOCATION_' in self.request.cookies: c = urllib.unquote(self.request.cookies['_LOCATION_']) if len(c.split('|')) == 3: response.delete_cookie('_LOCATION_') # Check if language (_LOCALE_) is set if self.request is not None: if '_LOCALE_' in self.request.params: response.set_cookie('_LOCALE_', self.request.params.get( '_LOCALE_'), timedelta(days=90)) elif '_LOCALE_' in self.request.cookies: pass # Check if profile (_PROFILE_) is set if self.request is not None: if '_PROFILE_' in self.request.params: # Set the profile cookie profile_code = self.request.params.get('_PROFILE_') response.set_cookie( '_PROFILE_', profile_code, timedelta(days=90)) # Update _LOCATION_ from cookies to profile geometry bbox # retrieved from database profile_db = DBSession.query(Profile).\ filter(Profile.code == profile_code).\ first() if profile_db is not None: # Calculate and transform bounding box bbox = DBSession.scalar(geofunctions.envelope( geofunctions.transform( profile_db.geometry, '900913')).wkt) geojson = utils.from_wkt(bbox) coords = geojson['coordinates'][0] p1 = coords[0] p2 = coords[2] l = '%s,%s' % (','.join([str(x) for x in p1]), ','.join([str(x) for x in p2])) response.set_cookie( '_LOCATION_', urllib.quote(l), timedelta(days=90)) elif '_PROFILE_' in self.request.cookies: # Profile already set, leave it pass else: # If no profile is set, set the default profile response.set_cookie('_PROFILE_', get_default_profile( self.request), timedelta(days=90))
def test_transform(self): spot = session.query(Spot).get(1) # note that we have to cast to 'ST_POINT', because 'functions.x' only works for Points in Oracle assert_almost_equal(float(session.scalar(functions.x(func.ST_POINT(spot.spot_location.transform(2249))))), -3890517.61088792) assert_almost_equal(float(session.scalar(functions.y(func.ST_POINT(spot.spot_location.transform(2249))))), 3627658.6749871401) ok_(session.query(Spot).filter(functions.wkt(Spot.spot_location.transform(2249)) == 'POINT (-3890517.61088792 3627658.67498714)').first() is not None) eq_(session.scalar(functions.wkt(functions.transform(WKTSpatialElement('POLYGON((743238 2967416,743238 2967450,743265 2967450,743265.625 2967416,743238 2967416))', 2249), 4326))), u'POLYGON ((-71.1776848522252 42.3902896503503, -71.1776843766327 42.390382946861, -71.1775844305466 42.3903826668518, -71.1775825927231 42.3902893638588, -71.1776848522252 42.3902896503503))')
def test_transform(self): spot = session.query(Spot).get(1) # compare the coordinates using a tolerance, because they may vary on different systems assert_almost_equal(session.scalar(functions.x(spot.spot_location.transform(2249))), -3890517.6109559298) assert_almost_equal(session.scalar(functions.y(spot.spot_location.transform(2249))), 3627658.6746507999) ok_(session.query(Spot).filter(Spot.spot_location.transform(2249).wkt == 'POINT(-3890517.61095593 3627658.6746508)').first() is not None) eq_(session.scalar(functions.wkt(functions.transform(WKTSpatialElement('POLYGON((743238 2967416,743238 2967450,743265 2967450,743265.625 2967416,743238 2967416))', 2249), 4326))), 'POLYGON((-71.1776848522251 42.3902896512902,-71.1776843766326 42.3903829478009,-71.1775844305465 42.3903826677917,-71.1775825927231 42.3902893647987,-71.1776848522251 42.3902896512902))')
def create_geom_filter(request, mapped_class, geom_attr, within_distance_additional_params={}): """Create MapFish geometry filter based on the request params. Either a box or within or geometry filter, depending on the request params. Additional named arguments are passed to the spatial filter. Arguments: request the request. mapped_class the SQLAlchemy mapped class. geom_attr the key of the geometry property as defined in the SQLAlchemy mapper. If you use ``declarative_base`` this is the name of the geometry attribute as defined in the mapped class. within_distance_additional_params additional_params to pass to the ``within_distance`` function. """ tolerance = float(request.params.get('tolerance', 0.0)) epsg = None if 'epsg' in request.params: epsg = int(request.params['epsg']) box = request.params.get('bbox') geometry = None if box is not None: box = map(float, box.split(',')) geometry = Polygon( ((box[0], box[1]), (box[0], box[3]), (box[2], box[3]), (box[2], box[1]), (box[0], box[1]))) elif 'lon' and 'lat' in request.params: geometry = Point(float(request.params['lon']), float(request.params['lat'])) elif 'geometry' in request.params: geometry = loads(request.params['geometry'], object_hook=GeoJSON.to_instance) geometry = asShape(geometry) if geometry is None: return None column_epsg = _get_col_epsg(mapped_class, geom_attr) geom_attr = getattr(mapped_class, geom_attr) epsg = column_epsg if epsg is None else epsg if epsg != column_epsg: geom_attr = functions.transform(geom_attr, epsg) wkb_geometry = WKBSpatialElement(buffer(geometry.wkb), epsg) return functions._within_distance(geom_attr, wkb_geometry, tolerance, within_distance_additional_params)
def create_geom_filter(request, mapped_class, geom_attr, within_distance_additional_params={}): """Create MapFish geometry filter based on the request params. Either a box or within or geometry filter, depending on the request params. Additional named arguments are passed to the spatial filter. Arguments: request the request. mapped_class the SQLAlchemy mapped class. geom_attr the key of the geometry property as defined in the SQLAlchemy mapper. If you use ``declarative_base`` this is the name of the geometry attribute as defined in the mapped class. within_distance_additional_params additional_params to pass to the ``within_distance`` function. """ tolerance = float(request.params.get('tolerance', 0.0)) epsg = None if 'epsg' in request.params: epsg = int(request.params['epsg']) box = request.params.get('bbox') geometry = None if box is not None: box = map(float, box.split(',')) geometry = Polygon(((box[0], box[1]), (box[0], box[3]), (box[2], box[3]), (box[2], box[1]), (box[0], box[1]))) elif 'lon' and 'lat' in request.params: geometry = Point(float(request.params['lon']), float(request.params['lat'])) elif 'geometry' in request.params: geometry = loads(request.params['geometry'], object_hook=GeoJSON.to_instance) geometry = asShape(geometry) if geometry is None: return None column_epsg = _get_col_epsg(mapped_class, geom_attr) geom_attr = getattr(mapped_class, geom_attr) epsg = column_epsg if epsg is None else epsg if epsg != column_epsg: geom_attr = functions.transform(geom_attr, epsg) wkb_geometry = WKBSpatialElement(buffer(geometry.wkb), epsg) return functions._within_distance(geom_attr, wkb_geometry, tolerance, within_distance_additional_params)
def create_geom_filter(request, mapped_class, **kwargs): """Create MapFish geometry filter based on the request params. Either a box or within or geometry filter, depending on the request params. Additional named arguments are passed to the spatial filter.""" tolerance = 0 if 'tolerance' in request.params: tolerance = float(request.params['tolerance']) epsg = None if 'epsg' in request.params: epsg = int(request.params['epsg']) box = None if 'bbox' in request.params: box = request.params['bbox'] geometry = None if box is not None: box = map(float, box.split(',')) geometry = Polygon( ((box[0], box[1]), (box[0], box[3]), (box[2], box[3]), (box[2], box[1]), (box[0], box[1]))) elif 'lon' and 'lat' in request.params: geometry = Point(float(request.params['lon']), float(request.params['lat'])) elif 'geometry' in request.params: factory = lambda ob: GeoJSON.to_instance(ob) geometry = loads(request.params['geometry'], object_hook=factory) geometry = asShape(geometry) if geometry is None: return None geom_column = mapped_class.geometry_column() epsg = geom_column.type.srid if epsg is None else epsg if epsg != geom_column.type.srid: geom_column = functions.transform(geom_column, epsg) wkb_geometry = WKBSpatialElement(buffer(geometry.wkb), epsg) if 'additional_params' in kwargs: return functions._within_distance(geom_column, wkb_geometry, tolerance, kwargs['additional_params']) else: return functions._within_distance(geom_column, wkb_geometry, tolerance)
def create_geom_filter(request, mapped_class, **kwargs): """Create MapFish geometry filter based on the request params. Either a box or within or geometry filter, depending on the request params. Additional named arguments are passed to the spatial filter.""" tolerance = 0 if 'tolerance' in request.params: tolerance = float(request.params['tolerance']) epsg = None if 'epsg' in request.params: epsg = int(request.params['epsg']) box = None if 'bbox' in request.params: box = request.params['bbox'] geometry = None if box is not None: box = map(float, box.split(',')) geometry = Polygon(((box[0], box[1]), (box[0], box[3]), (box[2], box[3]), (box[2], box[1]), (box[0], box[1]))) elif 'lon' and 'lat' in request.params: geometry = Point(float(request.params['lon']), float(request.params['lat'])) elif 'geometry' in request.params: factory = lambda ob: GeoJSON.to_instance(ob) geometry = loads(request.params['geometry'], object_hook=factory) geometry = asShape(geometry) if geometry is None: return None geom_column = mapped_class.geometry_column() epsg = geom_column.type.srid if epsg is None else epsg if epsg != geom_column.type.srid: geom_column = functions.transform(geom_column, epsg) wkb_geometry = WKBSpatialElement(buffer(geometry.wkb), epsg) if 'additional_params' in kwargs: return functions._within_distance(geom_column, wkb_geometry, tolerance, kwargs['additional_params']) else: return functions._within_distance(geom_column, wkb_geometry, tolerance)
def test_transform(self): spot = session.query(Spot).get(1) eq_(session.scalar(functions.wkt(spot.spot_location.transform(2249))), 'POINT(-3890517.610956 3627658.674651)') ok_( session.query(Spot).filter( sqlite_functions.mbr_contains( functions.buffer(Spot.spot_location.transform(2249), 10), WKTSpatialElement('POINT(-3890517.610956 3627658.674651)', 2249))).first() is not None) eq_( session.scalar( functions.wkt( functions.transform( WKTSpatialElement( 'POLYGON((743238 2967416,743238 2967450,743265 2967450,743265.625 2967416,743238 2967416))', 2249), 4326))), 'POLYGON((-71.177685 42.39029, -71.177684 42.390383, -71.177584 42.390383, -71.177583 42.390289, -71.177685 42.39029))' )
def __get_wkt_for_field(self, options): """Returns the WKT string for the value of a field by making a database query. If necessary the geometry is reprojected. """ if self.raw_value is not None and isinstance(self.raw_value, PersistentSpatialElement): geom_srid = self.field.type.srid map_srid = options.get('map_srid', geom_srid) if geom_srid != map_srid: # if the map uses a different CRS we have to ask the database # to reproject the geometry query = functions.wkt(functions.transform(self.raw_value, map_srid)) else: query = self.raw_value.wkt session = self.field.parent.session return session.scalar(query) else: return '';
def __get_wkt_for_field(self, options): """Returns the WKT string for the value of a field by making a database query. If necessary the geometry is reprojected. """ if self.raw_value is not None and isinstance(self.raw_value, PersistentSpatialElement): geom_srid = self.field.type.srid map_srid = options.get('map_srid', geom_srid) if geom_srid != map_srid: # if the map uses a different CRS we have to ask the database # to reproject the geometry query = functions.wkt( functions.transform(self.raw_value, map_srid)) else: query = self.raw_value.wkt session = self.field.parent.session return session.scalar(query) else: return ''