def generate_relative_configurations(reference, gaz, context, projector): if context == 'rural': pois = projector(gaz(reference, category='poi', filter_rural_score='MEDIUM')) elif context == 'urban': pois = projector(gaz(reference, category='poi', filter_urban_score='MEDIUM')) roads = projector(gaz(reference, category='way', filter_urban_score='LOW')) proj_reference = projector(reference) configurations = [] for model_name in MODELS[context]['one_poi']: model = models.load(model_name) for feature in pois: value = model(*(numpy.array(proj_reference) - numpy.array(feature['geo_lonlat']))) if value >= 0.4: configurations.append({'type': 'preposition', 'model': model_name, 'value': value, 'feature': feature}) if context == 'rural': for model_name in MODELS[context]['two_poi']: model = models.load(model_name) for idx in range(1, len(pois)): start = pois[idx] for idx2 in range(0, idx): end = pois[idx2] if start['dc_title'] != end['dc_title'] and start['geo_type'].main_type == end['geo_type'].main_type and start['geo_type'].main_type != 'POI': d = start['geo_lonlat'].distance(end['geo_lonlat']) if 5000 > d > 0: params = list((numpy.array(end['geo_lonlat']) - numpy.array(start['geo_lonlat']))) + list((numpy.array(proj_reference) - numpy.array(start['geo_lonlat']))) value = model(*(params)) if value >= 0.6: configurations.append({'type': 'preposition', 'model': model_name, 'value': value, 'feature': [start, end]}) elif context == 'urban': for model_name in MODELS[context]['two_poi']: model = models.load(model_name) for road in roads: if road['geo_lonlat'].distance(proj_reference) <= 10: if model_name == 'between.urban': intersections = [] for poi in pois: d = road['geo_lonlat'].distance(poi['geo_lonlat']) if d <= 10: intersections.append(poi) for road2 in roads: if road['dc_title'] != road2['dc_title'] and road['geo_lonlat'].intersects(road2['geo_lonlat']): intersection = road['geo_lonlat'].intersection(road2['geo_lonlat']) if isinstance(intersection, geometry.Point): poi = deepcopy(road2) poi['geo_lonlat'] = intersection intersections.append(poi) for pairs in permutations(intersections, 2): dist_1 = road['geo_lonlat'].project(pairs[0]['geo_lonlat']) dist_2 = road['geo_lonlat'].project(proj_reference) dist_3 = road['geo_lonlat'].project(pairs[1]['geo_lonlat']) if dist_1 < dist_2 < dist_3: value = model(dist_2 - dist_1) / (dist_3 - dist_1) if value >= 0.6: configurations.append({'type': 'preposition', 'model': model_name, 'value': value, 'feature': [pairs[0], pairs[1]]}) return configurations
def rural_between_test(): mdl = load('between.rural') tools.assert_is_not_none(mdl) tools.assert_greater_equal(0.01, mdl(100, 100, 0, 0)) tools.assert_greater_equal(0.01, mdl(100, 100, 100, 100)) tools.assert_greater_equal(mdl(100, 0, 53, 0), 0.8) tools.assert_greater_equal(mdl(100, 0, 75, 0), 0.8)
def urban_between_test(): mdl = load('between.urban') tools.assert_is_not_none(mdl) tools.eq_(0, mdl(-1)) tools.eq_(0, mdl(1.1)) tools.assert_greater_equal(mdl(0.4), 0.98) tools.assert_greater_equal(mdl(0.5), 0.98) tools.assert_greater_equal(mdl(0.6), 0.98)
def rural_near_test(): """Test loading the "near" rural model""" mdl = load('near.rural') tools.assert_is_not_none(mdl) tools.assert_greater_equal(mdl(0, 0), mdl(1000, 1000)) tools.assert_greater_equal(mdl(0, 0), mdl(-1000, 1000)) tools.assert_greater_equal(mdl(0, 0), mdl(1000, -1000)) tools.assert_greater_equal(mdl(0, 0), mdl(-1000, -1000))
def rural_west_test(): mdl = load('west.rural') tools.assert_is_not_none(mdl) tools.assert_greater_equal(mdl(0, 0), mdl(1000, 1000)) tools.assert_greater_equal(mdl(0, 0), mdl(-1000, 1000)) tools.assert_greater_equal(mdl(0, 0), mdl(1000, -1000)) tools.assert_greater_equal(mdl(0, 0), mdl(-1000, -1000)) tools.assert_equal(0, mdl(1000, 0)) tools.assert_equal(0, mdl(1000, 1000)) tools.assert_equal(0, mdl(1000, -1000))
def generate_road_configurations(reference, gaz, context, projector): if context == 'rural': roads = projector(gaz(reference, category='way', filter_rural_score='LOW')) elif context == 'urban': roads = projector(gaz(reference, category='way', filter_urban_score='LOW')) proj_reference = projector(reference) configurations = [] for road in roads: d = road['geo_lonlat'].distance(proj_reference) if d <= 10: configurations.append({'type': 'preposition', 'model': 'on', 'value': 1, 'feature': road}) if context == 'urban': model = models.load('at_corner.urban') for road2 in roads: if road['dc_title'] != road2['dc_title'] and road['geo_lonlat'].intersects(road2['geo_lonlat']): intersection = road['geo_lonlat'].intersection(road2['geo_lonlat']) if isinstance(intersection, geometry.Point): params = list(numpy.array(proj_reference) - numpy.array(intersection)) value = model(*params) if value >= 0.6: configurations.append({'type': 'preposition', 'model': 'at_corner.urban', 'value': value, 'feature': [road, road2]}) return configurations
def main(): proj = Proj(init='epsg:32630') for feature in GAZETTEER: feature['geo_lonlat'] = numpy.array(proj(*feature['geo_lonlat'])) #point = numpy.array(proj(*(-2.5901763343811035, 53.38953524087438))) #point = numpy.array(proj(*(-2.6008307933807373, 53.388645857875055, ))) #point = numpy.array(proj(*(-2.5901763343811035, 53.38953524087438))) point = numpy.array(proj(*(-2.6004, 53.3887))) # Between point configurations = [] """ for model_name in RURAL_ONE_POINT_MODELS: model = models.load(model_name) for feature in GAZETTEER: if model_name in ['at.urban', 'near.urban', 'next_to.urban', 'east.rural', 'north.rural', 'west.rural', 'south.rural', 'near.rural'] and feature['dc_type'] == POI: value = model(*(point - feature['geo_lonlat'])) elif model_name == 'at_corner.urban' and feature['dc_type'] == JUNCTION: value = model(*(point - feature['geo_lonlat'])) else: value = None if value and value >= 0.6: print model_name, value configurations.append(((model_name, value), feature)) """ for model_name in RURAL_TWO_POINT_MODELS: model = models.load(model_name) for feature1 in GAZETTEER: for feature2 in GAZETTEER: if feature1['dc_type'] != JUNCTION and feature2[ 'dc_type'] != JUNCTION and feature1[ 'dc_title'] != feature2['dc_title']: baseline = feature2['geo_lonlat'] - feature1['geo_lonlat'] pos = point - feature1['geo_lonlat'] print(baseline, pos) configurations.append( (('between.rural', 1), feature1, feature2)) print(language.generate_caption(generator.urban_caption(configurations)))