def create_spatial_tweets(spatial_layer_name, labels_string): spatial.create_layer(spatial_layer_name) tweet_query = "MATCH ({}) RETURN Social;".format(labels_string) records = DB.cypher.execute(tweet_query) for record in records: node = record[0] node_id = node._id properties = node.properties lat = properties['lat'] lon = properties['lon'] tweet_loc = (lat, lon) shape = parse_lat_long(tweet_loc) tweet_id = properties['tweet_id'] try: spatial.create_geometry(geometry_name=tweet_id, wkt_string=shape.wkt, layer_name="Spatial_Tweets", node_id=node_id) print('created {}'.format(tweet_id)) except GeometryExistsError: print 'The geometry is already in the DB'
def find_closest_geometries(self, coords, distance=4): """ Find the "closest" points of interest (poi) accross *all* layers from a given lat-lon location coord. :Params: coords : tuple WGS84 (EPSG 4326) lat, lon pair Latitude is a decimal number between -90.0 and 90.0 Longitude is a decimal number between -180.0 and 180.0 distance : int The max distance in km from `coords` to look for geometries. :Returns: a list of all matched nodes """ resource = self.resources['findClosestGeometries'] shape = parse_lat_long(coords) # get layer nodes query = "MATCH (r { name:'spatial_root' }), (r)-[:LAYER]->(n) RETURN n" results = self.graph.cypher.execute(query) spatial_data = { 'pointX': shape.x, 'pointY': shape.y, # this appears to be handled more like a 'tolerance', as increasing # the value even slightly returns data from hundreds of kms away. # TODO: raise failing spatial PR with Spatial API. 'distanceInKm': distance, } pois = [] for record in results: data = spatial_data.copy() node = record[0] node_properties = node.get_properties() layer_name = node_properties['layer'] data['layer'] = layer_name nodes = self._execute_spatial_request(resource, data) pois.extend(nodes) return pois
def find_within_distance(self, layer_name, coords, distance): """ Find all points of interest (poi) within a given distance from a lat-lon location coord. :Params: layer_name : str The name of the layer/index to remove the geometry from. coords : tuple WGS84 (EPSG 4326) lat, lon pair Latitude is a decimal number between -90.0 and 90.0 Longitude is a decimal number between -180.0 and 180.0 distance : int The radius of the search area in Kilometres (km) :Raises: LayerNotFoundError if the index does not exist. :Returns: a list of all matched nodes """ if not self._layer_exists(layer_name): raise LayerNotFoundError( 'Layer Not Found: "{}"'.format(layer_name) ) resource = self.resources['findGeometriesWithinDistance'] shape = parse_lat_long(coords) spatial_data = { 'pointX': shape.x, 'pointY': shape.y, 'layer': layer_name, 'distanceInKm': distance, } nodes = self._execute_spatial_request(resource, spatial_data) return nodes
def find_within_distance(self, layer_name, coords, distance): """ Find all points of interest (poi) within a given distance from a lat-lon location coord. :Params: layer_name : str The name of the layer/index to remove the geometry from. coords : tuple WGS84 (EPSG 4326) lat, lon pair Latitude is a decimal number between -90.0 and 90.0 Longitude is a decimal number between -180.0 and 180.0 distance : int The radius of the search area in Kilometres (km) :Raises: LayerNotFoundError if the index does not exist. :Returns: a list of all matched nodes """ if not self._layer_exists(layer_name): raise LayerNotFoundError( 'Layer Not Found: "{}"'.format(layer_name)) resource = self.resources['findGeometriesWithinDistance'] shape = parse_lat_long(coords) spatial_data = { 'pointX': shape.x, 'pointY': shape.y, 'layer': layer_name, 'distanceInKm': distance, } nodes = self._execute_spatial_request(resource, spatial_data) return nodes