Esempio n. 1
0
def main():
    """Main function to create the output file"""
    nominatim = Nominatim()
    overpass = Overpass()
    config = configparser.ConfigParser()
    config.read('configuration')
    area_id = nominatim.query('Italia').areaId()

    html_string = ""

    for operator in ast.literal_eval(config.get("MAPPING", "operators")):
        #create folders for needed saving operations later
        if not os.path.exists("./home_to_destination/{}".format(operator)):
            os.mkdir("./home_to_destination/{}".format(operator))
        if not os.path.exists("./routes/{}".format(operator)):
            os.mkdir("./routes/{}".format(operator))
        operator_parameter = '"operator" = "{}"'.format(operator)
        query = overpassQueryBuilder(area=area_id,
                                     elementType='rel',
                                     selector=operator_parameter,
                                     out='body')
        result = overpass.query(query, timeout=600)
        html_string += HTML.main_output_generation(
            config['MAPPING']['address'], result, operator)

    html_file = open("output.html", "w")
    html_file.write(html_string)
    html_file.close()
Esempio n. 2
0
def query_overpass(area_id, query_statement, element_type='node'):
    # Query Overpass based on area
    overpass = Overpass()
    query = overpassQueryBuilder(area=area_id,
                                 elementType=element_type,
                                 selector=query_statement)
    return overpass.query(query)
def get_OSM():
    nominatim = Nominatim()
    overpass = Overpass()
    areaId = nominatim.query('Le Plateau Mont Royal').areaId()
    query = overpassQueryBuilder(area=areaId, elementType=['way'])
    result = overpass.query(query)
    db = db_from_OSM(result)
    return db
Esempio n. 4
0
def obtain_map(city):
    overpass = Overpass()
    result = overpass.query(('''area[name="{}"];
	way(area)[highway=motorway];
	out body geom;

	''').format(city),
                            timeout=500)
    return result
Esempio n. 5
0
def segment(lat, long):
    """
    Main function defining the segment in wich the point is
    :param long: coords
    :param lat: coords
    :return: two way-objects such as our point is in between these two ways, and the road between them, and the main road
    (for later uses)
    """
    long, lat = lat, long
    point = np.array([long, lat])
    bbox, id, name = geo_to_way_info(long, lat)
    query = overpassQueryBuilder(bbox=bbox,
                                 elementType=['way'],
                                 selector=['highway', 'name'],
                                 includeGeometry=True)
    result = Overpass().query(query)

    for x in result.elements():
        if x.id() == id:
            closet_road = x

    IWW_list = []
    for x in result.elements():
        if x.id() != id and x.tags()['name'] != name:
            cc = closest_point_on_way(closet_road.geometry()["coordinates"],
                                      x.geometry()["coordinates"])
            dtm = np.linalg.norm(point - cc)
            IWW_list.append(IWW(x, dtm, cc))

    IWW_list.sort(
        key=lambda x: x.dtm
    )  #sorting the crossing road by distance crossing-point to center of interest
    result1 = IWW_list[
        0]  #first segmentation is done by the closest crossing road

    for iww in IWW_list:  #second one is done by the closest crossing road having the dot product negative (please refer to the presentation)
        if iww.obj != result1.obj:
            if np.dot(result1.cc - point, iww.cc - point) < 0:
                result2 = iww
                break

    new_geometry = []
    bool_add = False
    for point in closet_road.geometry(
    )["coordinates"]:  #we compute the geometry of the segmented road, usefull for next objectives
        point = np.array([point[1], point[0]])
        if np.array_equal(point, result1.cc) or np.array_equal(
                point, result2.cc):
            bool_add = not (bool_add)
        if bool_add:
            new_geometry.append(point)
    closet_road_geom = []
    for x in closet_road.geometry()["coordinates"]:
        closet_road_geom.append(np.array([x[1], x[0]]))

    return result1, result2, new_geometry, closet_road_geom
Esempio n. 6
0
    def getOsmGeoObjects(self, areaId, selector, elementType:OsmType):
        """
        sends overpass-query and return the elements from the json response
        """
        overpass = Overpass()
        # out='geom' also leads to geometry key (list of coordinates for each object)

        query = overpassQueryBuilder(
            area=areaId, elementType=elementType.value, selector=selector, out='geom')
        return overpass.query(query).toJSON()["elements"]
Esempio n. 7
0
def fetchFeatures(areaId, osmkey, osmtype):
    overpass = Overpass()

    query = overpassQueryBuilder(area=areaId,
                                 elementType=['node', 'way', 'relation'],
                                 selector='"' + osmkey + '"="' + osmtype + '"',
                                 includeGeometry=True,
                                 out='center meta')
    logging.debug("OSM query: %s", query)
    return overpass.query(query, timeout=60)
Esempio n. 8
0
def obtain_square_portion(corner1, corner3, corner2, corner4):
    overpass = Overpass()
    query = overpassQueryBuilder(bbox=[corner1, corner2, corner3, corner4],
                                 elementType='way',
                                 selector='"junction"',
                                 out="body geom")
    result = overpass.query(query)
    road_coords = []
    for element in result.elements():
        road_coords.append(element.geometry()['coordinates'])
    return road_coords
Esempio n. 9
0
def query(Key, Value, Location):
    nominatim = Nominatim()
    areaId = nominatim.query(f'{Location}').areaId()
    overpass = Overpass()
    query = overpassQueryBuilder(area=areaId,
                                 elementType=['node', 'way', 'relation'],
                                 selector=f'"{Key}"="{Value}"',
                                 includeGeometry=True)
    print("querying OSM")
    result = overpass.query(query, timeout=250)
    result = result.toJSON()
    return result
Esempio n. 10
0
def get_OSM():
    nominatim = Nominatim()
    overpass = Overpass()
    areaId = nominatim.query('Montreal, Canada').areaId()
    query = overpassQueryBuilder(
        area=areaId, elementType='node',
        selector=['shop']) + overpassQueryBuilder(
            area=areaId, elementType='node', selector='amenity')
    result = overpass.query(query)
    db = db_from_OSM(result)
    db_tags = list_tags(db)
    return db, db_tags
Esempio n. 11
0
def boundbox(Key, Value, box):
    #nominatim = Nominatim()
    print(box)
    overpass = Overpass()
    query = overpassQueryBuilder(bbox=box,
                                 elementType=['node', 'way', 'relation'],
                                 selector=f'"{Key}"="{Value}"',
                                 includeGeometry=True)
    print(query)
    result = overpass.query(query, timeout=250)
    result = result.toJSON()
    return result
def segment(long, lat):
    """
    Main function defining the segment in wich the point is
    :param long: coords
    :param lat: coords
    :return: two way-objects such as our point is in between these two ways
    """
    point = np.array([long, lat])
    bbox, id, name = geo_to_way_info(long, lat)
    query = overpassQueryBuilder(bbox=bbox,
                                 elementType=['way'],
                                 selector=['highway', 'name'],
                                 includeGeometry=True)
    result = Overpass().query(query)

    for x in result.elements():
        if x.id() == id:
            closet_road = x

    IWW_list = []
    for x in result.elements():
        if x.id() != id and x.tags()['name'] != name:
            cc = closest_point_on_way(closet_road.geometry()["coordinates"],
                                      x.geometry()["coordinates"])
            dtm = np.linalg.norm(point - cc)
            IWW_list.append(IWW(x, dtm, cc))

    IWW_list.sort(key=lambda x: x.dtm)
    result1 = IWW_list[0]

    for iww in IWW_list:
        if iww.obj != result1.obj:
            if np.dot(result1.cc - point, iww.cc - point) < 0:
                result2 = iww
                break

    new_geometry = []
    bool_add = False
    for point in closet_road.geometry()["coordinates"]:
        point = np.array([point[1], point[0]])
        if np.array_equal(point, result1.cc) or np.array_equal(
                point, result2.cc):
            bool_add = not (bool_add)
        if bool_add:
            new_geometry.append(point)
    closet_road_geom = []
    for x in closet_road.geometry()["coordinates"]:
        closet_road_geom.append(np.array([x[1], x[0]]))

    return result1, result2, new_geometry, closet_road_geom
Esempio n. 13
0
def test_data():
    dimensions = OrderedDict([
        ('year', dictRangeYears(2015, 2016.5, 1)),
        (
            'city',
            OrderedDict({
                # 'manhattan': 'Manhattan, New York',
                # 'vienna': 'Vienna, Austria',
                'enschede': 'Enschede, Netherlands',
                'vienna': 'Vienna, Austria',
            })),
        ('typeOfShop',
         OrderedDict({
             'supermarket': 'supermarket',
             'coffee': 'coffee',
         })),
    ])

    nominatim = Nominatim()
    overpass = Overpass()

    def fetch(year, city, typeOfShop):
        areaId = nominatim.query(city).areaId()
        query = overpassQueryBuilder(area=areaId,
                                     elementType='way',
                                     selector='"shop"="' + typeOfShop + '"',
                                     out='count')
        x = overpass.query(query, date=year, timeout=30).countElements()
        assert x >= 0
        return x

    data = Data(fetch, dimensions)
def download_osm(area_name):
    """ Given an area name, download corresponding OSM elements """

    # Get area id
    nomanatim = Nominatim()
    area_id = nomanatim.query(area_name).areaId()

    # Form and ask query
    overpass = Overpass()
    query = overpassQueryBuilder(area=area_id, elementType=['way', 'node'], out='body')
    osm_data = overpass.query(query, timeout=600)

    # Display results
    utils.display_results(osm_data)

    # Keep elements (ways and nodes) in JSON format
    elements = osm_data.toJSON()['elements']
    return elements
Esempio n. 15
0
    def __init__(self, city: str) -> None:
        """Create city map

        Args:
            city (str): City name
            radius (float, optional): City radius, in meters. Defaults to 3000.
        """
        self._city = city

        self._elements_dict = {}
        self._normalized_dict = {}
        self._features_list = []

        # request timing
        self._timeout = 300
        self._try_again = 30
        # initialize instances
        self._nominatim = Nominatim()
        self._overpass = Overpass()
Esempio n. 16
0
def GetOSMWaysData(box):
    overpass = Overpass()
    query = overpassQueryBuilder(
        bbox=[box.latMin, box.lonMin, box.latMax, box.lonMax],
        elementType='way',
        out='body')
    try:
        ways = overpass.query(query)
    except:
        Log.logging.error("In OSMPythonToolsHandler.py, GetOSMWaysData",
                          exc_info=True)
        return False
    if ways._json['elements'] == []:
        box.lonMin -= 0.5
        box.latMin -= 0.5
        box.lonMax += 0.5
        box.latMax += 0.5
        GetOSMWaysData(box)
    else:
        StoreWaysData(ways._json['elements'])
    return True
Esempio n. 17
0
def assertForQueryResult(minElements=100, overpassKwargs={}, **kwargs):
    print(kwargs)
    overpass = Overpass()
    y = overpass.query(overpassQueryBuilder(**kwargs), **overpassKwargs)
    assert y.isValid()
    assert len(y.elements()) > minElements
    assert len(y.elements()) == y.countElements()
    assert len(y.nodes()) >= 0
    assert len(y.nodes()) == y.countNodes()
    assert len(y.ways()) >= 0
    assert len(y.ways()) == y.countWays()
    assert len(y.relations()) >= 0
    assert len(y.relations()) == y.countRelations()
    assert len(y.areas()) >= 0
    assert len(y.areas()) == y.countAreas()
    assert y.countNodes() + y.countWays() + y.countRelations() + y.countAreas(
    ) == y.countElements()
    assert y.toJSON()
    assert y.version() > 0
    assert y.generator()
    assert y.timestamp_osm_base()
    assert y.copyright()
    return y
def query_construction(search_area):
        nominatim_query=Nominatim().query(search_area)
        query_01=overpassQueryBuilder(area=nominatim_query.areaId(),elementType=['node','way','relation'], selector=["military"],includeGeometry=True)
        query_02=overpassQueryBuilder(area=nominatim_query.areaId(),elementType=['node','way','relation'], selector='"landuse"="military"',includeGeometry=True)
        result_01=Overpass().query(query_01)
        result_02=Overpass().query(query_02)
        print("Pronađeno ukupno "+str(result_01.countElements())+" 'military' objekta")
        print("Pronađeno ukupno "+str(result_02.countElements())+" 'landuse=military' objekta")
        results=[result_01,result_02]
        return results
Esempio n. 19
0
def fetch_osm(bbox, osm_value="motorway", osm_key="highway"):
    """
    Fetches OSM road data from the OverpassAPI.
    :param bbox: array-like of four coordinates: miny, minx, maxy, maxx.
    :param osm_value: str specifies the OSM value to be retrieved.
    :param osm_key: str specifies the OSM key to be retrieved.
    :return: gpd.GeoDataFrame
    """
    element_type = ["way", "relation"]
    bbox_osm = buffer_bbox(bbox)
    quot = '"'
    select = quot + osm_key + quot + "=" + quot + osm_value + quot
    select_link = select.replace(osm_value,
                                 osm_value + "_link")  # also get road links
    select_junction = select.replace(osm_value, osm_value + "_junction")
    geoms = []
    for selector in [select, select_link, select_junction]:
        query = overpassQueryBuilder(bbox=bbox_osm,
                                     elementType=element_type,
                                     selector=selector,
                                     out="body",
                                     includeGeometry=True)
        try:
            elements = Overpass().query(query, timeout=120).elements()
        except Exception:  # type?
            elements = []
            Warning("Could not download OSM data")
        # create multiline of all elements
        if len(elements) > 0:
            for i in range(len(elements)):
                elem = elements[i]
                try:
                    geoms.append(elem.geometry())
                except Exception:
                    continue
        else:
            Warning("Could not retrieve " + select)
    if len(geoms) > 0:
        lines = gpd.GeoDataFrame(crs="EPSG:4326", geometry=geoms)
        n = len(geoms)
        lines["osm_value"] = [osm_value] * n  # add road type
        return lines
Esempio n. 20
0
    def __init__(self,
                 areaName: str,
                 elementsToUse: OsmObjectType = OsmObjectType.NODE):
        assert (self.osmSelector)

        areaId = Nominatim().query(areaName).areaId()
        query = overpassQueryBuilder(area=areaId,
                                     elementType=elementsToUse.value,
                                     selector=self.osmSelector,
                                     out='geom')
        osmObjects = Overpass().query(query).toJSON()["elements"]

        self.dataSource = osmObjectsToGeoJSON(osmObjects,
                                              polygonize=True)["features"]

        shapeGeoms = []
        for loc in self.dataSource:
            shapeGeom = shape(loc["geometry"])
            shapeGeom.properties = loc["properties"]
            shapeGeoms.append(shapeGeom)

        self.shapeIndex = STRtree(shapeGeoms)
Esempio n. 21
0
def get_osm(bbox,
            osm_value="motorway",
            osm_key="highway",
            ref_arr=None):
    element_type = ["way", "relation"]
    bbox_osm = buffer_bbox(bbox, ref_arr)
    quot = '"'
    select = quot + osm_key + quot + "=" + quot + osm_value + quot
    select_link = select.replace(osm_value, osm_value + "_link")  # also get road links
    select_junction = select.replace(osm_value, osm_value + "_junction")
    geoms = []
    for selector in [select, select_link, select_junction]:
        query = overpassQueryBuilder(bbox=bbox_osm,
                                     elementType=element_type,
                                     selector=selector,
                                     out="body",
                                     includeGeometry=True)
        try:
            elements = Overpass().query(query, timeout=120).elements()
        except Exception:
            elements = []
            Warning("Could not download OSM data")
        # create multiline of all elements
        if len(elements) > 0:
            for i in range(len(elements)):
                elem = elements[i]
                try:
                    geoms.append(elem.geometry())
                except Exception:
                    continue
        Warning("Could not retrieve " + select)
    if len(geoms) > 0:
        lines = gpd.GeoDataFrame(crs="EPSG:4326", geometry=geoms)
        n = len(geoms)
        lines["osm_value"] = [osm_value] * n  # add road type
        return lines
    else:
        return []
from OSMPythonTools.nominatim import Nominatim
from OSMPythonTools.overpass import overpassQueryBuilder, Overpass
from shapely.geometry import Point, Polygon, MultiPolygon

import pandas as pd, geopandas as gpd

if __name__ == "__main__":
    nominatim = Nominatim()
    areaId = nominatim.query('Bogota, Colombia').areaId()
    overpass = Overpass()
    query = overpassQueryBuilder(area=areaId,
                                 elementType=['way'],
                                 selector='"amenity"="hospital"',
                                 includeGeometry=True)
    result = overpass.query(query)
    result = [{
        'tags': x.tags(),
        'geometry': x.geometry()
    } for x in result.elements()]
    #to pandas for cleaning tags
    df_hosps = pd.io.json.json_normalize(result)
    #to geopandas
    gdf_hosps = gpd.GeoDataFrame(df_hosps)
    #
    #gdf_hosps['geometry']=gdf_hosps['geometry.coordinates'].apply(lambda x: Polygon(x[0]))
    gdf_hosps['geometry.point'] = gdf_hosps['geometry.coordinates'].apply(
        lambda x: x[0][0]).apply(Point)
    gdf_hosps = gdf_hosps.set_geometry(col='geometry.point')
    #gdf_hosps.plot().get_figure().savefig('hospitals_points.png')
    gdf_hosps.drop(columns=['geometry.coordinates']).to_file(
        driver='GeoJSON', filename="bogota_hospitales.geojson")
Esempio n. 23
0
from OSMPythonTools.api import Api
from OSMPythonTools.overpass import Overpass
from pprint import pprint
overpass = Overpass()

around = 100  #around in meters
loc = [41.3735485, 2.1215157]

result = overpass.query('node["amenity"](around:' + str(around) + ',' +
                        str(loc[0]) + ',' + str(loc[1]) + '); out;')
print(result.toJSON())
for n in result.toJSON()["elements"]:
    #print(n.tags())
    elem = {"id": n["id"], "type": n["tags"]["amenity"]}
    if "name" in n["tags"]:
        elem["name"] = n["tags"]["name"]
    else:
        elem["name"] = None
    elem["loc"] = [n["lat"], n["lon"]]

    #print(," - ",n["tags"]["name"],n["lat"])
    print(elem)

#mas info sobre el objeto result https://github.com/mocnik-science/osm-python-tools/blob/master/docs/overpass.md
"""
api = Api()
overpass_query = 'node(57.7,11.9,57.8,12.0)[amenity=bar];out;'
loc = api.query(overpass_query )
print(loc)
"""
Esempio n. 24
0
from xml.etree import ElementTree
try:
    import overpy
    overpy_api = overpy.Overpass()
except:
    print("\nRUN $HOME/anaconda2/bin/pip install overpy\n")
try:
    import overpass
    overpass_api = overpass.API()
except:
    print("\nRUN $HOME/anaconda2/bin/pip install overpass\n")
try:
    from OSMPythonTools.api import Api
    OSMPythonTools_api = Api()
    from OSMPythonTools.overpass import Overpass
    OSMPythonTools_overpass = Overpass()
    from OSMPythonTools.nominatim import Nominatim
    OSMPythonTools_nominatim = Nominatim()
except:
    print("\nCOPY ./OSMPythonTools\n")

mypydir = os.path.abspath(
    os.path.dirname(inspect.getfile(inspect.currentframe())))
addpath = mypydir + "/mytools"
if addpath not in sys.path: sys.path.append(addpath)
if mypydir not in sys.path: sys.path.append(mypydir)
from readconf import get_conf, get_conf_int, get_conf_float
iprint = 2
configfile = "conf.txt"

Esempio n. 25
0
        out='body')
    result = overpass.query(query)
    output = {}
    for x in result.elements():
        nodeID = x.id()
        stopName = x.tag('name')
        coord = [x.lat(), x.lon()]
        output[nodeID] = [stopName, coord]

    return output


if __name__ == "__main__":

    nominatim = Nominatim()
    overpass = Overpass()

    bbox = [37.835727, -122.368679, 37.90669, -122.234196]

    #bbox = generate_bbox_around_point([38.069908,-121.908297],1)
    print(bbox)
    x = get_supermarkets(overpass, bbox)

    for node, info in x.items():
        print(node)
        print(info[0])
        print(info[1][0])
        print(info[1][1])

    dict_of_nearby_bus_stops = get_bus_stops(overpass, bbox)
    num_of_bus_stops_in_area = len(dict_of_nearby_bus_stops)
import geopandas

import matplotlib.pyplot as plt
import pandas as pd

from helper.geoJsonConverter import osmObjectsToGeoJSON

areaName = "pieschen"
# area to query
pieschen = Nominatim().query('Pieschen, Dresden, Germany')

query = overpassQueryBuilder(area=pieschen.areaId(),
                             elementType="way",
                             selector=["'building'"],
                             out='geom')
buildings = osmObjectsToGeoJSON(Overpass().query(query).toJSON()["elements"])

buildingsGdf = geopandas.GeoDataFrame.from_features(buildings["features"])
#buildingsGdf.plot("building", legend = True)

buildingsGdf["area"] = buildingsGdf.geometry.area

dataFrame = pd.DataFrame(buildingsGdf)
#dataFrame.hist(column="area", by="building")
avgAreaPerBuildingType = dataFrame.groupby(["building"]).agg(
    avg_area=("area", "mean"), count=("area", "count")).reset_index()
avgAreaPerBuildingType = avgAreaPerBuildingType.sort_values("count")
avgAreaPerBuildingType.plot.bar(x="building", y="count")
avgAreaPerBuildingType.plot.bar(x="building",
                                y="avg_area",
                                title="Ascending Sorted by group size")
Esempio n. 27
0
from OSMPythonTools.api import Api
from OSMPythonTools.overpass import Overpass
from mediawiki import MediaWiki
api = Api()
op = Overpass()
result = op.query('''
area(3600421007)->.searchArea;
rel["leisure"="park"](area.searchArea); 
out body;''')
raw_parks = result._json['elements']
print(raw_parks[0])
for park in raw_parks:
    if 'tags' in park:
        # if 'wikipedia'
Esempio n. 28
0
def find_points_of_interest(geotag, location):
    """
    Find points of interest, like transit, near a result.
    :param geotag: The geotag field of a Craigslist result.
    :param location: The where field of a Craigslist result.  Is a string containing a description of where
    the listing was posted.
    :return: A dictionary containing annotations.
    """
    area_found = False
    area = ""
    min_dist = None

    num_supermarkets_in_area = 0
    closest_supermarket = "N/A"
    near_supermarket = False
    closest_supermarket_dist = 'N/A'
    
    num_convenience_stores_in_area = 0
    closest_convenience_store = "N/A"
    near_convenience_store = False
    closest_convenience_store_dist = 'N/A'

    num_pharmacys_in_area = 0
    closest_pharmacy = "N/A"
    near_pharmacy = False
    closest_pharmacy_dist = 'N/A'
    
    num_BART_in_area = 0
    closest_BART = "N/A"
    near_BART = False
    closest_BART_dist = 'N/A'
    
    num_of_bus_stops_in_area = 0
    near_Transbay_stop = False
    nearby_routes = []
    
    # Look to see if the listing is in any of the neighborhood boxes we defined.
    for a, coords in settings.BOXES.items():
        if in_box(geotag, coords):
            area = a
            area_found = True

    #Create a bbox around the listing
    distance = 1 #km
    bbox = osm.generate_bbox_around_point(geotag, distance)
    overpass = Overpass()
    dict_of_nearby_supermarkets = osm.get_supermarkets(overpass,bbox)
    num_supermarkets_in_area = len(dict_of_nearby_supermarkets) 
    for node, info in dict_of_nearby_supermarkets.items():
        name = info[0]
        lat = info[1][0]
        long = info[1][1]
        dist = coord_distance(lat, long, geotag[0], geotag[1])
        if (min_dist is None or dist < min_dist):
            min_dist = dist
            closest_supermarket = name
            near_supermarket = True
            closest_supermarket_dist = dist
        

    dict_of_nearby_convenience_stores = osm.get_convenience_store(overpass,bbox)
    num_convenience_stores_in_area = len(dict_of_nearby_convenience_stores) 
    for node, info in dict_of_nearby_convenience_stores.items():
        name = info[0]
        lat = info[1][0]
        long = info[1][1]
        dist = coord_distance(lat, long, geotag[0], geotag[1])
        if (min_dist is None or dist < min_dist):
            min_dist = dist
            closest_convenience_store = name
            near_convenience_store = True
            closest_convenience_store_dist = dist    
    
    dict_of_nearby_pharmacys = osm.get_pharmacy(overpass,bbox)
    num_pharmacys_in_area = len(dict_of_nearby_pharmacys) 
    for node, info in dict_of_nearby_pharmacys.items():
        name = info[0]
        lat = info[1][0]
        long = info[1][1]
        dist = coord_distance(lat, long, geotag[0], geotag[1])
        if (min_dist is None or dist < min_dist):
            min_dist = dist
            closest_pharmacy = name
            near_pharmacy = True
            closest_pharmacy_dist = dist    
        
    
    
    dict_of_nearby_BART = osm.get_BART(overpass,bbox)
    num_BART_in_area = len(dict_of_nearby_BART) 
    for node, info in dict_of_nearby_BART.items():
        name = info[0]
        lat = info[1][0]
        long = info[1][1]
        dist = coord_distance(lat, long, geotag[0], geotag[1])
        if (min_dist is None or dist < min_dist):
            min_dist = dist
            closest_BART = name
            near_BART = True
            closest_BART_dist = dist   
    
    #Is it close to bus stops?
    Transbay_route_list = ['B','C','CB','E','F','G','H','J','L',
                           'LA','NL','NX','NX1','NX2','NX3','NX4',
                           'O','P','S','SB','V','W','Z']
            
    dict_of_nearby_bus_stops = osm.get_bus_stops(overpass,bbox)
    num_of_bus_stops_in_area = len(dict_of_nearby_bus_stops)
    for node, info in dict_of_nearby_bus_stops.items():
        name = info[0]
        routes = info[1]
        lat = info[2][0]
        long = info[2][1]
        if routes != None:
            list_of_routes_at_stop = routes.split(';')
            for route in list_of_routes_at_stop:
                if route in Transbay_route_list:
                    near_Transbay_stop = True
                #add to list of nearby routes if not there already
                if route not in nearby_routes:
                    nearby_routes.append(route)
        
        

    

    # If the listing isn't in any of the boxes we defined, check to see if the string description of the neighborhood
    # matches anything in our list of neighborhoods.
    if len(area) == 0:
        for hood in settings.NEIGHBORHOODS:
            if hood in location.lower():
                area = hood

    return {
        "area_found": area_found,
        "area": area,
        "num_BART_in_area": num_BART_in_area,
        "near_bart": near_BART,
        "bart_dist": closest_BART_dist,
        "bart": closest_BART,
        "num_supermarkets_in_area":num_supermarkets_in_area,
        'closest_supermarket':closest_supermarket,
        "near_supermarket":near_supermarket,
        'closest_supermarket_dist':closest_supermarket_dist,
        "num_convenience_stores_in_area":num_convenience_stores_in_area,
        'closest_convenience_store':closest_convenience_store,
        "near_convenience_store":near_convenience_store,
        'closest_convenience_store_dist':closest_convenience_store_dist,
        "num_pharmacys_in_area":num_pharmacys_in_area,
        'closest_pharmacy':closest_pharmacy,
        "near_pharmacy":near_pharmacy,
        'closest_pharmacy_dist':closest_pharmacy_dist,
        'nearby_bus_routes':nearby_routes,
        'near_Transbay_stop':near_Transbay_stop,
        'num_of_bus_stops_in_area':num_of_bus_stops_in_area
        
    }
Esempio n. 29
0
        #    raz += temp[i][0] * temp[i-1]
    sum += temp[n-1][0]*temp[0][1] - temp[0][0]*temp[n-1][1]
    return fabs(sum/2)
"""
from OSMPythonTools.nominatim import Nominatim
nominatim = Nominatim()
nyc = nominatim.query('Perm')
print(nyc.toJSON())"""

#query = overpassQueryBuilder(bbox=[58.0065792, 56.2248863, 58.0103644, 56.2305674], elementType=['way','node'], selector='building', out='body',includeGeometry=True)

#query = overpassQueryBuilder(area=nominatim.query('Perm').areaId(), elementType=['way','node'], selector=['building','"name"="Ленинский район"'], out='body',includeGeometry=True)
#query = overpassQueryBuilder(area=nominatim.query('Perm, Свердловский район').areaId(), elementType=['way','node'], selector=['building'], out='body',includeGeometry=True)
query = overpassQueryBuilder(area=nominatim.query('Perm, Свердловский район').areaId(), elementType=['way','node'], selector=['building'], out='body',includeGeometry=True)

overpass = Overpass()

huh = overpass.query(query)
#print(huh.ways())
print(huh.countElements())
print(huh.countWays())
living_houses_w_levels = ['yes','apartments','dormitory','residential']
living_houses_wo_levels = ['house','bungalow','detached']
unliving_houses = ['roof', 'university', 'kindergarten', 'school', 'retail', 'commercial', 'office', 'church',
 'hotel', 'garages', 'construction', 'train_station', 'cathedral', 'supermarket', 'service', 'offices',
 'mosque', 'hospital', 'college', 'garage', 'warehouse', 'industrial', 'kiosk']
check_build=[]
people = 0
max = 0
for n in huh.ways():
    #print(n.tag("building"))
def maparea(area_name, lat=None, lon=None):
    clean_name = str(re.sub(r'[^a-zA-Z\d]', '', area_name))
    nominatim = Nominatim()
    if area_name.isdigit():
        areaId = int(area_name)
    else:
        areaId = nominatim.query(area_name).areaId()

    overpass = Overpass()

    query = overpassQueryBuilder(area=areaId,
                                 elementType='way',
                                 selector='"landuse"="residential"',
                                 out='geom')
    residential_areas = overpass.query(query).ways() or []

    query = overpassQueryBuilder(area=areaId,
                                 elementType='way',
                                 selector='"highway"="footway"',
                                 out='geom')
    roads_footway = overpass.query(query).ways() or []

    query = overpassQueryBuilder(
        area=areaId,
        elementType='way',
        selector=['"highway"="service"', '"service"!="parking_aisle"'],
        out='geom')
    roads_cycleway = overpass.query(query).ways() or []

    query = overpassQueryBuilder(area=areaId,
                                 elementType='way',
                                 selector='"highway"="residential"',
                                 out='geom')
    roads_residential = overpass.query(query).ways() or []

    query = overpassQueryBuilder(area=areaId,
                                 elementType='way',
                                 selector='"highway"="tertiary"',
                                 out='geom')
    roads_tertiary = overpass.query(query).ways() or []

    query = overpassQueryBuilder(area=areaId,
                                 elementType='way',
                                 selector='"highway"="secondary"',
                                 out='geom')
    roads_secondary = overpass.query(query).ways() or []

    query = overpassQueryBuilder(area=areaId,
                                 elementType='way',
                                 selector='"highway"="primary"',
                                 out='geom')
    roads_primary = overpass.query(query).ways() or []

    query = overpassQueryBuilder(area=areaId,
                                 elementType='way',
                                 selector='"highway"="motorway"',
                                 out='geom')
    roads_motorway = overpass.query(query).ways() or []

    query = overpassQueryBuilder(area=areaId,
                                 elementType='way',
                                 selector='"highway"="trunk"',
                                 out='geom')
    roads_trunk = overpass.query(query).ways() or []

    query = overpassQueryBuilder(area=areaId,
                                 elementType='way',
                                 selector='"highway"="trunk_link"',
                                 out='geom')
    roads_trunk_link = overpass.query(query).ways() or []

    query = overpassQueryBuilder(area=areaId,
                                 elementType='way',
                                 selector='"highway"="unclassified"',
                                 out='geom')
    roads_unclassified = overpass.query(query).ways() or []

    #print (result.countElements())
    #ax = plt.axes(projection=ccrs.PlateCarree(globe=ccrs.Globe(datum='WGS84',
    #                                              ellipse='WGS84')))
    #print(bbx)

    request = cimgt.OSM()

    terrain = cimgt.MapboxTiles(
        'pk.eyJ1IjoiZWVzYWhlIiwiYSI6ImNqdGFjMTI1ODA1ZGc0NHRmN28wcG5ybnEifQ.ZBSMcdbWkjfjRgDVQLjfnw',
        'terrain-rgb')

    plt.style.use('dark_background')

    fig, ax = plt.subplots(num=1,
                           figsize=(10, 10),
                           subplot_kw=dict(projection=request.crs,
                                           facecolor='#000000'))

    #print("trying to save debug terrain img")
    #plt.savefig("terrain.png")
    #print("saved")

    #ax.set_extent([26.6561, 22.6589, 59.611, 60.8409])
    #ax.add_image(request, 10, zorder=0)

    roads = roads_residential + roads_tertiary + roads_secondary + roads_primary + roads_unclassified + roads_trunk + roads_trunk_link

    def roads_in_bbox(bbox):
        ret = []
        for road in roads:
            if road._json['bounds']['minlat'] < bbox['maxlat'] and road._json[
                    'bounds']['maxlat'] > bbox['minlat'] and road._json[
                        'bounds']['minlon'] < bbox['maxlon'] and road._json[
                            'bounds']['maxlon'] > bbox['minlon']:
                ret.append(road)
        return ret

    # find areas completely enclosed inside a bounding box (not partially)
    def residential_areas_enclosed_in_bbox(bbox):
        ret = []
        for area in residential_areas:
            if area._json['bounds']['maxlat'] < bbox['maxlat'] and area._json[
                    'bounds']['minlat'] > bbox['minlat'] and area._json[
                        'bounds']['maxlon'] < bbox['maxlon'] and area._json[
                            'bounds']['minlon'] > bbox['minlon']:
                ret.append(area)
        return ret

    cll = []

    bbx = None
    '''

  processed = 0
  roads = roads_residential + roads_tertiary + roads_secondary + roads_primary + roads_unclassified + roads_trunk + roads_trunk_link
  print("processing total " + str(len(roads)) + " roads")
  for area in (roads_residential + roads_tertiary + roads_secondary + roads_primary + roads_unclassified + roads_trunk + roads_trunk_link):
    if(processed % 500 == 0): 
      print("processing number " + str(processed))

    geometry = area._json['geometry']
    lons = []
    lats = []
    for point in geometry:
        lons.append(point['lon'])
        lats.append(point['lat'])
        #mycoords.append( (point['lat'], ) )

    xs = np.array(lons)
    ys = np.array(lats)
    xynps=ax.projection.transform_points(ccrs.Geodetic(), xs, ys)
    #print(xynps)
    #break
    ax.plot(xynps[:,0], xynps[:,1], "r", zorder=11, color='green', marker='o',linewidth=0.2, markersize=0, antialiased=False)
    processed+=1

  '''

    print("Found " + str(len(residential_areas)) + " residential areas in " +
          area_name)

    # What are we exactly doing here??
    '''
  for area in residential_areas:
    #print(vars(area))
    geometry = area._json['geometry']
    bbx = area._json['bounds']
    lons = []
    lats = []
    for point in geometry:
        lons.append(point['lon'])
        lats.append(point['lat'])
        #mycoords.append( (point['lat'], ) )

    xs = np.array(lons)
    ys = np.array(lats)
    xynps=ax.projection.transform_points(ccrs.Geodetic(), xs, ys)
    #print(xynps)
    #break
    plt.figure(1)
    ax.fill(xynps[:,0], xynps[:,1], "b", zorder=10, antialiased=False)

    cll.append( (lons,lats) )
  #print(len(cll))

  #ax.axis('off')
  '''

    counter = 0
    for area in residential_areas:
        #if(lat is not None):
        #      bbx['minlon'] = lon
        #      bbx['maxlon'] = lon
        #      bbx['minlat'] = lat
        #      bbx['maxlat'] = lat
        #      width = 0
        #      height = 0
        #else:
        bbx = area._json['bounds']

        width = bbx['maxlon'] - bbx['minlon']
        height = bbx['maxlat'] - bbx['minlat']
        if (width < 0.0008 or height < 0.0008):
            print("area too small, skipping")
            continue
        #print(width)
        #print(height)

        zoom = 0.7

        conv_factor = (2.0 * math.pi) / 360.0
        lat = bbx['minlat']
        lat = lat * conv_factor

        m1 = 111132.92
        # latitude calculation term 1
        m2 = -559.82
        # latitude calculation term 2
        m3 = 1.175
        # latitude calculation term 3
        m4 = -0.0023
        # latitude calculation term 4
        p1 = 111412.84
        # longitude calculation term 1
        p2 = -93.5
        # longitude calculation term 2
        p3 = 0.118
        # longitude calculation term 3

        # Calculate the length of a degree of latitude and longitude in meters
        latlen = m1 + (m2 * math.cos(2 * lat)) + (m3 * math.cos(4 * lat)) + \
                (m4 * math.cos(6 * lat))
        longlen = (p1 * math.cos(lat)) + (p2 * math.cos(3 * lat)) + \
                    (p3 * math.cos(5 * lat))

        #print("lat len " + str(latlen))
        #print("lon len " + str(longlen))

        targetWidth = 2500
        targetHeight = 2500

        currentWidth = longlen * width
        currentHeight = latlen * height

        offset_w_meters = (targetWidth - currentWidth) / 2
        offset_h_meters = (targetHeight - currentHeight) / 2

        offset_w_angles = offset_w_meters / longlen
        offset_h_angles = offset_h_meters / latlen

        #print("currentWidth " + str(currentWidth))
        #print("currentHeight " + str(currentHeight))
        #print("offsetAngleW " + str(offset_w_angles))
        #print("offsetAngleH " + str(offset_h_angles))

        offsetW = offset_w_angles
        offsetH = offset_h_angles

        #my_cos=math.cos(rad)
        #print("my cos " + str(my_cos))
        #test = 0.1 * abs(math.cos(abs(bbx['minlat'])))
        #print("test " + str(test))

        #print("trying to make it " + str(zoom*test) + " degrees wide")

        #print("testOffsetW" + str(testOffsetW))

        #offsetW = ((zoom*0.051122172576223) - width) / 2
        #print("realoffsetW" + str(offsetW))

        #offsetH = ((zoom*0.038) - height) / 2
        #print("offsetH" + str(offsetH))

        if offsetW < 0 or offsetH < 0:
            print("area too big, skipping")
            continue

        test_savename = outdir + "/" + clean_name + "_" + str(counter) + ".png"

        # continue if we already created this file
        if os.path.isfile(test_savename):
            counter += 1
            continue

        #print(bbx)

        new_bbx = bbx.copy()
        try:
            new_bbx['minlon'] = bbx['minlon'] - offsetW
            new_bbx['maxlon'] = bbx['maxlon'] + offsetW
            new_bbx['minlat'] = bbx['minlat'] - offsetH
            new_bbx['maxlat'] = bbx['maxlat'] + offsetH
        except:
            print("FAILED, BBX: " + str(bbx))
            pprint(area._json)

        # get population density

        ring = [[new_bbx['minlon'], new_bbx['minlat']],
                [new_bbx['minlon'], new_bbx['maxlat']],
                [new_bbx['maxlon'], new_bbx['maxlat']],
                [new_bbx['maxlon'], new_bbx['minlat']],
                [new_bbx['minlon'], new_bbx['minlat']]]

        ring_string = json.dumps(ring)

        if (lon is None):
            r = requests.post("http://sedac.ciesin.columbia.edu/arcgis/rest/services/sedac/geoprocessing/GPServer/pes-v1/execute", \
              data={'Input_Feature_Set': '{ "geometryType": "esriGeometryPolygon","fields": [{"name": "id","type": "esriFieldTypeInteger"}],"spatialReference": {"wkid": 4326},"features": [{"geometry": {"rings": [  \
        '                + ring_string + ' \
        ],"spatialReference": {"wkid": 4326}},"attributes": {"id": 1}}]}'                                                                               , 'f': 'json'})
            json_result = json.loads(r.text)
            attributes = json_result['results'][0]['value']['features'][0][
                'attributes']
            pop = attributes['POPULATION05']
            area = attributes['LANDAREA']
            density = pop / area

            if (density < 1000):
                print("density too small")
                continue

        #print(new_bbx)
        #exit()

        #print("bbx height " + str(new_bbx['maxlat'] - new_bbx['minlat']))

        #red_fill = ax.fill(xynps[:,0], xynps[:,1], "r", zorder=10, antialiased=False)

        plt.figure("terrain")

        fig_terrain, ax_terrain = plt.subplots(figsize=(10, 10),
                                               subplot_kw=dict(
                                                   projection=request.crs,
                                                   facecolor='#000000'))

        try:
            ax_terrain.set_extent([
                new_bbx['minlon'], new_bbx['maxlon'], new_bbx['minlat'],
                new_bbx['maxlat']
            ])
        except Exception:
            print(traceback.format_exc())
            print(sys.exc_info()[0])
            continue

        ax_terrain.add_image(terrain, 13)
        savename = "osm/" + str(re.sub(r'[^a-zA-Z\d]', '',
                                       area_name)) + str(counter) + "_t.png"

        plt.savefig(savename,
                    dpi=_dpi,
                    transparent=True,
                    bbox_inches='tight',
                    pad_inches=0,
                    frameon=None)
        terrain_raster_261 = cv2.imread(savename)
        terrain_raster_256_b = terrain_raster_261[3:259, 3:259, 0]
        terrain_raster_256_g = terrain_raster_261[3:259, 3:259, 1]
        terrain_raster_256_r = terrain_raster_261[3:259, 3:259, 2]

        plt.figure(2)
        fig2, ax2 = plt.subplots(figsize=(10, 10),
                                 subplot_kw=dict(projection=request.crs,
                                                 facecolor='#000000'))

        xynps = ax.projection.transform_points(
            ccrs.Geodetic(), np.asarray([new_bbx['minlon'],
                                         new_bbx['maxlon']]),
            np.asarray([new_bbx['minlat'], new_bbx['maxlat']]))
        #max_lonlat=ax.projection.transform_points(ccrs.Geodetic(), new_bbx['maxlon'], new_bbx['maxlat'])

        #ax2.set_extent([new_bbx['minlon'], new_bbx['maxlon'], new_bbx['minlat'], new_bbx['maxlat']])
        ax2.set_extent([
            new_bbx['minlon'], new_bbx['maxlon'], new_bbx['minlat'],
            new_bbx['maxlat']
        ],
                       crs=ccrs.Geodetic())

        roads_current = roads_in_bbox(new_bbx)

        all_areas = residential_areas_enclosed_in_bbox(new_bbx)
        #print(str(len(all_areas)) + " areas in bbox")
        red_fills = []
        for current_residential_area in (all_areas):
            bbx = current_residential_area._json['bounds']
            width = bbx['maxlon'] - bbx['minlon']
            height = bbx['maxlat'] - bbx['minlat']
            if (width < 0.001 or height < 0.001):
                #print("area too small, skipping")
                continue

            geometry = current_residential_area._json['geometry']
            lons = []
            lats = []
            for point in geometry:
                lons.append(point['lon'])
                lats.append(point['lat'])
                #mycoords.append( (point['lat'], ) )

            xs = np.array(lons)
            ys = np.array(lats)
            xynps = ax.projection.transform_points(ccrs.Geodetic(), xs, ys)

            #print("area " + str(current_residential_area))
            red_fill2 = ax2.fill(xynps[:, 0],
                                 xynps[:, 1],
                                 "k",
                                 zorder=11,
                                 antialiased=False)
            red_fills.append(red_fill2)

        road_refs = []
        savename = "osm/" + str(re.sub(r'[^a-zA-Z\d]', '',
                                       area_name)) + str(counter) + "_2.png"
        #print("trying to save file called " +savename)
        plt.savefig(savename,
                    dpi=_dpi,
                    facecolor='0.0',
                    edgecolor='0.0',
                    transparent=True,
                    bbox_inches='tight',
                    pad_inches=0,
                    frameon=None)

        #exit()

        mask_261 = cv2.imread(savename, 0)
        mask_256 = mask_261[3:259, 3:259]
        #print("shape:" + str(mask.shape))
        #exit()

        mask_256 = cv2.threshold(mask_256, 127, 255,
                                 cv2.THRESH_BINARY_INV)[1]  # ensure binary
        #kernel = np.ones((3,3), np.uint8)
        kernel = cv2.getStructuringElement(cv2.MORPH_CROSS, (3, 3))
        mask_dilated_256 = cv2.dilate(mask_256, kernel, iterations=2)

        savename = "osm/" + str(re.sub(r'[^a-zA-Z\d]', '',
                                       area_name)) + str(counter) + "_3.png"
        cv2.imwrite(savename, mask_256)

        #for road_ref in (road_refs):
        #  l = road_ref.pop(0)
        #  wl = weakref.ref(l)
        #  l.remove()
        #  del l

        plt.figure(1)
        ax.set_extent([
            new_bbx['minlon'], new_bbx['maxlon'], new_bbx['minlat'],
            new_bbx['maxlat']
        ])

        #print("fetch roads")
        #print("got " + str(len(roads))+ " roads ")
        #exit()

        #query = overpassQueryBuilder(bbox=[new_bbx['minlat'], new_bbx['minlon'], new_bbx['maxlat'], new_bbx['maxlon']], elementType='way', selector=['"highway"="residential"', '"highway"="tertiary"', '"highway"="secondary"', '"highway"="primary"', '"highway"="unclassified"', '"highway"="trunk"', '"highway"="trunk_link"'], out='geom')
        #roads_current = overpass.query(query).ways()
        #print("got num roads:")

        if len(roads_current) > 0:
            #print(len(roads_current))
            processed = 0
            for area in (roads_current):
                _type = area._json['tags']['highway']
                #print(_type)
                if _type not in ['primary', 'secondary', 'highway', 'trunk']:
                    continue

                geometry = area._json['geometry']
                lons = []
                lats = []
                for point in geometry:
                    lons.append(point['lon'])
                    lats.append(point['lat'])
                    #mycoords.append( (point['lat'], ) )

                xs = np.array(lons)
                ys = np.array(lats)
                xynps = ax.projection.transform_points(ccrs.Geodetic(), xs, ys)
                #print(xynps)
                #break
                plt.figure(1)
                road_ref = ax.plot(xynps[:, 0],
                                   xynps[:, 1],
                                   zorder=11,
                                   color='#000000',
                                   marker='o',
                                   linewidth=0.05,
                                   markersize=0,
                                   antialiased=False)
                road_refs.append(road_ref)
                processed += 1

        savename = "osm/" + str(re.sub(r'[^a-zA-Z\d]', '',
                                       area_name)) + str(counter) + "_0.png"
        plt.savefig(savename,
                    dpi=_dpi,
                    facecolor='0.0',
                    edgecolor='1.0',
                    transparent=True,
                    bbox_inches='tight',
                    pad_inches=0,
                    frameon=None)

        roads_primary_secondary_raster_261 = cv2.imread(savename, 0)
        roads_primary_secondary_raster_256 = roads_primary_secondary_raster_261[
            3:259, 3:259]
        roads_primary_secondary_raster_256 = cv2.threshold(
            roads_primary_secondary_raster_256, 127, 255,
            cv2.THRESH_BINARY_INV)[1]

        if len(roads_current) > 0:
            #print(len(roads_current))
            processed = 0
            for area in (roads_current):
                #if(processed % 500 == 0):
                #print("processing number " + str(processed))

                geometry = area._json['geometry']
                lons = []
                lats = []
                for point in geometry:
                    lons.append(point['lon'])
                    lats.append(point['lat'])
                    #mycoords.append( (point['lat'], ) )

                xs = np.array(lons)
                ys = np.array(lats)
                xynps = ax.projection.transform_points(ccrs.Geodetic(), xs, ys)
                #print(xynps)
                #break
                plt.figure(1)
                road_ref = ax.plot(xynps[:, 0],
                                   xynps[:, 1],
                                   zorder=11,
                                   color='#000000',
                                   marker='o',
                                   linewidth=0.05,
                                   markersize=0,
                                   antialiased=False)
                road_refs.append(road_ref)
                processed += 1

        savename = "osm/" + str(re.sub(r'[^a-zA-Z\d]', '',
                                       area_name)) + str(counter) + "_1.png"
        plt.savefig(savename,
                    dpi=_dpi,
                    facecolor='0.0',
                    edgecolor='1.0',
                    transparent=True,
                    bbox_inches='tight',
                    pad_inches=0,
                    frameon=None)

        # FINISHED ALL MATPLOTLIB EXPORTS AT THIS POINT

        roads_raster_261 = cv2.imread(savename, 0)
        roads_raster_256 = roads_raster_261[3:259, 3:259]
        #b,g,r = cv2.split(masked_roads)''
        roads_raster_256 = cv2.threshold(
            roads_raster_256, 127, 255,
            cv2.THRESH_BINARY_INV)[1]  # ensure binary

        # This one will only display the roads that are behind residential areas for the SECOND image

        masked_roads_256 = cv2.bitwise_and(roads_raster_256,
                                           roads_raster_256,
                                           mask=mask_dilated_256)

        #masked_roads = cv2.threshold(mask, 127, 255, cv2.THRESH_BINARY_INV)[1]  # ensure binary

        #savename = outdir + str(re.sub(r'[^a-zA-Z\d]', '', area_name)) + str(counter) + "_4.png"
        #cv2.imwrite(savename, masked_roads)

        # This one will only show the roads behind residential areas

        roads_raster_256 = cv2.bitwise_and(roads_raster_256,
                                           roads_raster_256,
                                           mask=(255 - mask_256))
        roads_raster_256 = cv2.bitwise_or(roads_raster_256,
                                          roads_primary_secondary_raster_256)

        __width = mask_256.shape[1]
        __height = mask_256.shape[0]
        empty = np.zeros((__height, __width), dtype=np.uint8)

        #print(str(roads_raster.shape))
        #print(str(mask.shape))
        #print(str(empty.shape))

        out = cv2.merge((roads_raster_256, empty, mask_256))

        height = out.shape[0]
        width = out.shape[1]
        #print("width")
        #print(width)
        if width > 256:
            out = out[0:height, 0:256]
            masked_roads = masked_roads[0:height, 0:256]
            width = 256
        if height > 256:
            out = out[0:256, 0:width]
            masked_roads = masked_roads[0:256, 0:width]
            height = 256
        if width < 256 or height < 256:
            width_diff = 256 - width
            height_diff = 256 - height

            out = cv2.copyMakeBorder(out,
                                     height_diff,
                                     0,
                                     width_diff,
                                     0,
                                     cv2.BORDER_CONSTANT,
                                     value=[0, 0, 0])
            masked_roads = cv2.copyMakeBorder(masked_roads,
                                              height_diff,
                                              0,
                                              width_diff,
                                              0,
                                              cv2.BORDER_CONSTANT,
                                              value=[0, 0, 0])
            height = out.shape[0]
            width = out.shape[1]

        #savename = outdir + str(re.sub(r'[^a-zA-Z\d]', '', area_name)) + str(counter) + "_5.png"
        #cv2.imwrite(savename, out)

        combined_r = np.zeros((256, 512), dtype=np.uint8)
        combined_r[0:256, 0:256] = out[:, :, 0]
        combined_r[0:256, 256:512] = masked_roads_256

        combined_g = np.zeros((256, 512), dtype=np.uint8)
        #combined_g[0:256, 0:256] = terrain_combined_256
        #combined_g[0:256, 256:512] = masked_roads

        combined_b = np.zeros((256, 512), dtype=np.uint8)
        combined_b[0:256, 0:256] = out[:, :, 2]
        #combined_b[0:256, 256:512] = masked_roads

        #b,g,r = cv2.split(terrain_raster)

        terrain_combined_256 = -10000 + (
            (terrain_raster_256_r * 256 * 256 + terrain_raster_256_g * 256 +
             terrain_raster_256_b) * 0.1)
        #water = cv2.inRange(terrain_combined_256, 0, 24467)

        smallest = terrain_combined_256.min()  #terrain_combined_256.min()
        biggest = smallest + 100  # terrain_combined_256.max()
        #biggest = terrain_combined_256.max()

        _range = biggest - smallest
        #print ("biggest " + str(biggest))

        #print ("range " + str(_range))

        terrain_combined_256 = terrain_combined_256 - smallest
        print("smallest " + str(terrain_combined_256.min()))
        print("biggest " + str(terrain_combined_256.max()))

        terrain_combined_256 = (terrain_combined_256 / _range) * 255
        #ret,terrain_combined_256 = cv2.threshold(terrain_combined_256,2,255,cv2.THRESH_BINARY)

        #terrain_combined_256 = cv2.bitwise_not(terrain_combined_256,terrain_combined_256, mask = water)

        combined_g[0:256, 0:256] = terrain_combined_256
        combined_g[0:256, 0:256] = np.clip(terrain_combined_256,
                                           a_min=0,
                                           a_max=255)

        #terrain_debug_savename = outdir + "/" + clean_name + "_" + str(counter) + "_" + str(int(density)) + "_tdbg.png"
        #cv2.imwrite(terrain_debug_savename,terrain_new)

        savename = outdir + "/" + clean_name + "_" + str(counter) + "_" + str(
            int(density)) + ".png"
        out = cv2.merge((combined_r, combined_g, combined_b))
        print("writing out file " + savename)
        cv2.imwrite(savename, out)

        for cur_red_fill in (red_fills):
            l = cur_red_fill.pop(0)
            wl = weakref.ref(l)
            l.remove()
            del l

        #red_fill2 = ax.fill(xynps[:,0], xynps[:,1], "r", zorder=15, antialiased=False)

        #savename = "osm/" + str(re.sub(r'[^a-zA-Z\d]', '', area_name)) + str(counter) + "_2.png"
        #plt.savefig(savename, dpi=128, facecolor='0.0', edgecolor='0.0', transparent=False, bbox_inches='tight', pad_inches=0, frameon=None)

        counter += 1
        #l = red_fill.pop(0)
        #wl = weakref.ref(l)
        #l.remove()
        #del l

        #counter +=1
        #l = red_fill2.pop(0)
        #wl = weakref.ref(l)
        #l.remove()
        #del l

        for road_ref in (road_refs):
            l = road_ref.pop(0)
            wl = weakref.ref(l)
            l.remove()
            del l

        #ax.fill(xynps[:,0], xynps[:,1], "b", zorder=10, antialiased=False)

    #ax.set_extent([bbx['minlat'], bbx['maxlat'], bbx['minlon'], bbx['maxlon']])
    '''