def test_if_tables_got_populated_correctly(self): """Test the data inserted in tables by util_populate_table""" # pick a set of rows in t_co2 as points # try to find centers==points in t_areas print(self.samples) samples = tuple([s[0] for s in self.samples]) print(samples) # try to find aoi that contains points q2 = ('SELECT t_areas.aoi, t_areas.data ' 'FROM t_co2, t_areas WHERE ' '((t_co2.id IN %s) AND ' 'ST_Contains(t_areas.aoi, t_co2.geometry));') r = dbProxy._connected(q2, **{'values': (samples, ), 'multi': True}) #print(r) # check if aoi.data contains centers from src.spatial import spatial outcome = [] for result in r: coords = [ tuple(cc['geometry']['coordinates']) for cc in result[1]['features'] ] aoi = result[0] for c in coords: geom = spatial.shape_geometry(c[0], c[1]) q3 = ('SELECT ST_Contains(%s, %s::geometry);') contains = dbProxy._connected( q3, **{ 'values': ( aoi, geom, ), 'multi': False }) self.assertTrue(contains[0])
def increasing_area(p, results='start', step=0): # check if square contains point # if not recursively increase the size if results != 'start' and results or step == 25: return results[0] if results else None print(p, step) query = select([Areas.center ]).where(func.ST_Contains(Areas.aoi, p)) #print(str(query.compile())) results = areasOps.exec_func_query(query, multi=True) lookup = from_wkt(p).__geo_interface__['coordinates'] stepping = lookup for r in range(100): s = step - 4 if mapping.get(str(s), None): if step % 2 == 0: stepping = (lookup[mapping.get('2')[0]] + mapping.get('2')[1], lookup[1]) else: stepping = (lookup[0], lookup[mapping.get('3')[0]] + mapping.get('3')[1]) step += 1 new_point = spatial.shape_geometry(stepping[0], stepping[1]) return increasing_area(new_point, results, step)
def test_should_test_shape_geometry(self): """Test shape_geometry() """ geom = spatial.shape_geometry(self.luke.longitude, self.luke.latitude) #print(geom) self.assertEqual( geom, geom # #todo: use a SELECT casting )
def test_should_test_shape_geometry(self): """Test shape_geometry() """ geom = spatial.shape_geometry( self.luke.longitude, self.luke.latitude ) #print(geom) self.assertEqual( geom, geom # #todo: use a SELECT casting )
def test_shape_aoi(self): print('TEST2<<<<') geom = spatial.shape_geometry(self.long, self.lat) shape, center = spatial.shape_aoi(geom) print(shape) try: conversion = self.conn.execute('SELECT %s::geometry;', (shape, )).first() assert conversion except Exception as e: print('TEST FAILED') raise e # SELECT 'SRID=3857;POLYGON(((-179.748110962 -22.8178), (-178.348110962 -22.8178), # (-179.048 -22.1178405762), (-179.048 -23.5178405762), (-179.748110962 -22.8178), ))'::geometry; print('TEST PASSED\n')
def setUp(self): """Create a random square.""" geojson = { "geometry": { "type": "Polygon", "coordinates": rand_polygon() } } # simulate a single view/area self.polygon_avg = spatial.from_list_to_ewkt(geojson['geometry']['coordinates']) # simulate a view with 3 areas p = (-14, -68) # rand_coordinates() self.polygon_big, _ = spatial.shape_aoi(p, size=8) # spatial.from_list_to_ewkt(rand_polygon(size=12)) # simulate a random point self.point = spatial.shape_geometry(p[0], p[1])
def test_insert_area_and_center(self): """Test insertion of row in t_areas""" geom = spatial.shape_geometry(self.long, self.lat) shape, center = spatial.shape_aoi(geom) try: self.conn.execute( 'INSERT INTO t_areas(center, aoi) VALUES (\'{center}\', \'{polygon}\');' .format(center=center, polygon=shape)) select = self.conn.execute( 'SELECT COUNT(id) FROM t_areas WHERE center = \'{center}\';'. format(center=center)).fetchall() self.assertEqual(select[0][0], 1) except Exception as e: print('TEST FAILED') raise e print('TEST PASSED\n')
def test_shape_aoi(self): print('TEST2<<<<') geom = spatial.shape_geometry(self.long, self.lat) shape, center = spatial.shape_aoi(geom) print(shape) try: conversion = self.conn.execute( 'SELECT %s::geometry;', (shape, ) ).first() assert conversion except Exception as e: print('TEST FAILED') raise e # SELECT 'SRID=3857;POLYGON(((-179.748110962 -22.8178), (-178.348110962 -22.8178), # (-179.048 -22.1178405762), (-179.048 -23.5178405762), (-179.748110962 -22.8178), ))'::geometry; print('TEST PASSED\n')
def center(self): """ Return the point itself or the center of the polygon. :return: """ if isinstance(self.geo_object, Point): # if it's a point, return the point closest = self.what_are_the_closest_centers_to_(self.geometry)[0] elif isinstance(self.geo_object, Polygon): # if it's a polygon, calculate and return the center of the polygon coords = self.geo_object.__geo_interface__['coordinates'][0] side = abs(coords[0][0]-coords[0][1]) return spatial.shape_geometry(coords[0][0] + side/2, coords[0][1] - side/2) else: raise ValueError('cls.center() method: geometry can be only {}'.format( self.elements ))
def center(self): """ Return the point itself or the center of the polygon. :return: """ if isinstance(self.geo_object, Point): # if it's a point, return the point closest = self.what_are_the_closest_centers_to_(self.geometry)[0] elif isinstance(self.geo_object, Polygon): # if it's a polygon, calculate and return the center of the polygon coords = self.geo_object.__geo_interface__['coordinates'][0] side = abs(coords[0][0] - coords[0][1]) return spatial.shape_geometry(coords[0][0] + side / 2, coords[0][1] - side / 2) else: raise ValueError( 'cls.center() method: geometry can be only {}'.format( self.elements))
def store_xco2(self): """ Main function to the database when storing Xco2 data. When a client requests a center `(x, y)`, the lookup table can find the square containing that point and respond with the JSON. (If a point is not in any square, the algorithm looks on the areas of the closest (in a 200 Km radius) centers, or finally generates a new area with this point as a center). INSERT procedure is as follow: - insert point > belong the point to any known area? - if Y > INSERT point - if N > create area with center=point using shape_aoi() At database initialization, this method is called to populate both the Xco2 and the Areas tables following the pop-above algorithm. Store a Xco2 datum and store or update its related Area of Interest. :return tuple: (pkey_xco2, pkey_area, ) """ from src.spatial import spatial from src.dbproxy import dbProxy geometry = spatial.shape_geometry(self.longitude, self.latitude) ins = Xco2.__table__.insert().values( xco2=self.xco2, timestamp=self.timestamp, geometry=geometry ) try: result = dbProxy.alchemy.execute(ins) aoi = Areas.store_area(geometry, self.xco2) except (IntegrityError, Exception) as e: # #todo: # integrity error happens when a datum is updated or two data are # are rounded to the same coordinates (Postgis maximum tolerance is -xxx.yyy) # compare dates, if record is more recent store it and archive the older one; # in the latter case, just pass # #### raise e # at the moment pass print(str(e)) return None, None return result.inserted_primary_key[0], aoi.pks
def test_if_tables_got_populated_correctly(self): """Test the data inserted in tables by util_populate_table""" # pick a set of rows in t_co2 as points # try to find centers==points in t_areas print(self.samples) samples = tuple([s[0] for s in self.samples]) print(samples) # try to find aoi that contains points q2 = ( 'SELECT t_areas.aoi, t_areas.data ' 'FROM t_co2, t_areas WHERE ' '((t_co2.id IN %s) AND ' 'ST_Contains(t_areas.aoi, t_co2.geometry));' ) r = dbProxy._connected( q2, **{ 'values': (samples, ), 'multi': True } ) #print(r) # check if aoi.data contains centers from src.spatial import spatial outcome = [] for result in r: coords = [tuple(cc['geometry']['coordinates']) for cc in result[1]['features']] aoi = result[0] for c in coords: geom = spatial.shape_geometry(c[0], c[1]) q3 = ( 'SELECT ST_Contains(%s, %s::geometry);' ) contains = dbProxy._connected( q3, **{ 'values': (aoi, geom, ), 'multi': False } ) self.assertTrue(contains[0])
def increasing_area(p, results='start', step=0): # check if square contains point # if not recursively increase the size if results != 'start' and results or step == 25: return results[0] if results else None print(p, step) query = select([Areas.center]).where(func.ST_Contains(Areas.aoi, p)) #print(str(query.compile())) results = areasOps.exec_func_query(query, multi=True) lookup = from_wkt(p).__geo_interface__['coordinates'] stepping = lookup for r in range(100): s = step - 4 if mapping.get(str(s), None): if step % 2 == 0: stepping = (lookup[mapping.get('2')[0]] + mapping.get('2')[1], lookup[1]) else: stepping = (lookup[0], lookup[mapping.get('3')[0]] + mapping.get('3')[1]) step += 1 new_point = spatial.shape_geometry(stepping[0], stepping[1]) return increasing_area(new_point, results, step)
def test_insert_area_and_center(self): """Test insertion of row in t_areas""" geom = spatial.shape_geometry(self.long, self.lat) shape, center = spatial.shape_aoi(geom) try: self.conn.execute( 'INSERT INTO t_areas(center, aoi) VALUES (\'{center}\', \'{polygon}\');'.format( center=center, polygon=shape ) ) select = self.conn.execute( 'SELECT COUNT(id) FROM t_areas WHERE center = \'{center}\';'.format( center=center ) ).fetchall() self.assertEqual( select[0][0], 1 ) except Exception as e: print('TEST FAILED') raise e print('TEST PASSED\n')