def geom_area_diffs(**kwargs): """ Calculates the spatial difference between the first and the last version of the same area. Function requires at least two areas. """ list_current = current_data(**kwargs) for current in list_current: if current.geom_type == 'multipolygons': features = session.query(Feature).filter( Feature.osm_id == current.osm_id).order_by( Feature.osm_version).all() if len(features) < 2: result = str(0.0) else: area1 = session.query(func.ST_Area(func.ST_GeogFromText( func.ST_AsText(GeomMultiPolygon.the_geom)))).filter( GeomMultiPolygon.feature_id == features[0].id).first()[0] area2 = session.query(func.ST_Area(func.ST_GeogFromText( func.ST_AsText(GeomMultiPolygon.the_geom)))).filter( GeomMultiPolygon.feature_id == features[1].id).first()[0] result = str(area2 - area1) save_result(kwargs['job_op_id'], current.osm_id, result)
def geom_sum_dist(**kwargs): """ Calculates the cumulative distance accross points. Function requires at least three points. """ list_current = current_data(**kwargs) for current in list_current: if current.geom_type == 'points': features = session.query(Feature).filter( Feature.osm_id == current.osm_id).order_by( Feature.osm_version).all() result = 0.0 if len(features) >= 3: for i, feature in enumerate(features): if i != 0: geog1 = session.query(func.ST_GeogFromText( func.ST_AsText(GeomPoint.the_geom))).filter( GeomPoint.feature_id == features[i - 1].id).first()[0] geog2 = session.query(func.ST_GeogFromText( func.ST_AsText(GeomPoint.the_geom))).filter( GeomPoint.feature_id == feature.id).first()[0] result += session.query(func.ST_Distance( geog1, geog2, True)).first()[0] save_result(kwargs['job_op_id'], current.osm_id, result)
def geom_from_original(**kwargs): """ Calculates the distance from the first point. Function requires at least two points. """ list_current = current_data(**kwargs) for current in list_current: if current.geom_type == 'points': features = session.query(Feature).filter( Feature.osm_id == current.osm_id).order_by( Feature.osm_version).all() if len(features) < 2: result = '0' else: geog1 = session.query(func.ST_GeogFromText(func.ST_AsText( GeomPoint.the_geom))).filter( GeomPoint.feature_id == features[0].id).first()[0] geog2 = session.query(func.ST_GeogFromText(func.ST_AsText( GeomPoint.the_geom))).filter( GeomPoint.feature_id == features[-1].id).first()[0] result = session.query( func.ST_Distance(geog1, geog2, True)).first()[0] save_result(kwargs['job_op_id'], current.osm_id, str(result))
def count_inserts(**kwargs): """ Counts the number of tag insertions. """ # get current data list_current = current_data(**kwargs) for i, current in enumerate(list_current): # select all version of the feature features = session.query(Feature).filter( Feature.osm_id == current.osm_id).order_by( Feature.osm_version).all() # initialize counter total = 0 if len(features) >= 2: for i, feature in enumerate(features): if i != 0: previous = features[i - 1] if feature.all_tags != previous.all_tags: # go through all tags for key in feature.all_tags: if not previous.all_tags.has_key(key): total += 1 # save results to the DB save_result(kwargs['job_op_id'], current.osm_id, total)
def count_geom_changes(**kwargs): """ Counts the number of geometric changes. """ # get current data list_current = current_data(**kwargs) for i, current in enumerate(list_current): if current.osm_version >= kwargs['osm_version']: # select features based on the osm_id features = session.query(Feature).filter( Feature.osm_id == current.osm_id).order_by( Feature.osm_version).all() # initialize counter total = 0 if len(features) >= 2: for i, feature in enumerate(features): if i != 0: previous_geom = find_geom(features[i - 1]) current_geom = find_geom(feature) if not previous_geom.almost_equals(current_geom): total += 1 # save results to the DB save_result(kwargs['job_op_id'], current.osm_id, total)
def count_updates(**kwargs): """ Counts the number of modified tags or the number of modifications to a certain tag of interest. """ # get current data list_current = current_data(**kwargs) # find TOI try: tag = kwargs['tag_of_interest'] except KeyError: tag = None for i, current in enumerate(list_current): # select features based on the osm_id features = session.query(Feature).filter( Feature.osm_id == current.osm_id).order_by( Feature.osm_version).all() # initialize counter total = 0 if len(features) >= 2: for i, feature in enumerate(features): if i != 0: previous = features[i - 1] # if no tag was specified if not tag: for key in feature.all_tags: # go through all tags if previous.all_tags.has_key(key) and \ feature.all_tags.has_key(key) and \ previous.all_tags[key] != feature.all_tags[key]: total += 1 # if a tag was specified else: # check the specified tag if previous.all_tags.has_key(tag) and \ feature.all_tags.has_key(tag) and \ previous.all_tags[tag] != feature.all_tags[tag]: total += 1 # save results to the DB save_result(kwargs['job_op_id'], current.osm_id, total)
def currency_coefficient(**kwargs): """ Assigns a currency coefficient to each feature. The larger the coefficient, the greater the need for updating the object. """ # get current data features = current_data(**kwargs) # find the temporal boundary of the specified group of OSM features ts_boundary = time_boundary(**kwargs) for feature in features: # the result is the timestamp difference in days result = (ts_boundary - convert_timestamp(feature.osm_timestamp)).days # save results to the DB save_result(kwargs['job_op_id'], feature.osm_id, result)
def count_deletions(**kwargs): """ Counts the number of deleted tags. """ # get current data list_current = current_data(**kwargs) # check if any specific tag was specified try: tag = kwargs['tag_of_interest'] except KeyError: tag = None for i, current in enumerate(list_current): # select features based on the osm_id features = session.query(Feature).filter( Feature.osm_id == current.osm_id).order_by( Feature.osm_version).all() # initialize counter total = 0 if len(features) >= 2: for i, feature in enumerate(features): if i != 0: previous = features[i - 1] # if no tag was specified if tag == None: # go through all tags for key in previous.all_tags: if previous.all_tags.has_key(key) == True and \ feature.all_tags.has_key(key) == False: total += 1 # if a tag was specified else: # check the specified tag if previous.all_tags.has_key(tag) == True and \ feature.all_tags.has_key(tag) == False: total += 1 # save results to the DB save_result(kwargs['job_op_id'], current.osm_id, total)
def currency_revision_needed(**kwargs): """ Marks features in need of revision. """ # get current data features = current_data(**kwargs) # find the temporal boundary of the specified group of OSM features ts_boundary = time_boundary(**kwargs) for feature in features: # revision is not needed yet if convert_timestamp(feature.osm_timestamp) >= ts_boundary: result = 0 # revision is needed else: result = 1 # save results to the DB save_result(kwargs['job_op_id'], feature.osm_id, result)
def geom_length_diffs(**kwargs): """ Calculates the spatial difference between the first and the last version of the same line. Function requires at least two lines. """ list_current = current_data(**kwargs) for current in list_current: if (current.geom_type == 'lines' or current.geom_type == 'multilinestrings'): features = session.query(Feature).filter( Feature.osm_id == current.osm_id).order_by( Feature.osm_version).all() if len(features) < 2: result = str(0.0) else: if current.geom_type == 'lines': length1 = session.query(func.ST_Length(func.ST_GeogFromText( func.ST_AsText(GeomLine.the_geom)))).filter( GeomLine.feature_id == features[0].id).first()[0] length2 = session.query(func.ST_Length(func.ST_GeogFromText( func.ST_AsText(GeomLine.the_geom)))).filter( GeomLine.feature_id == features[1].id).first()[0] else: length1 = session.query(func.ST_Length(func.ST_GeogFromText( func.ST_AsText(GeomMultiLine.the_geom)))).filter( GeomMultiLine.feature_id == features[0].id).first()[0] length2 = session.query(func.ST_Length(func.ST_GeogFromText( func.ST_AsText(GeomMultiLine.the_geom)))).filter( GeomMultiLine.feature_id == features[1].id).first()[0] result = str(length2 - length1) save_result(kwargs['job_op_id'], current.osm_id, result)