Exemplo 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()
Exemplo 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)
Exemplo n.º 3
0
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
Exemplo 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
Exemplo n.º 5
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)
Exemplo 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"]
Exemplo n.º 7
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
Exemplo n.º 8
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
Exemplo n.º 9
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
Exemplo n.º 10
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
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
Exemplo n.º 12
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
Exemplo n.º 13
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
Exemplo n.º 14
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'
Exemplo n.º 15
0
    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"))
    #print(n.tags())
    #print(n.id())
Exemplo n.º 16
0
class CityMap:
    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()

    def __getattr__(self, feature: str) -> list[tuple[float, float]]:
        """Returns a list of features. Too check the available features, try using the features attribute

        Returns:
            list[tuple[float, float]]: list of relative positions
        """
        if feature in self._normalized_dict:
            return self._normalized_dict[feature]

        return []

    @property
    def features(self) -> list[str]:
        """Returns all loaded and normalized features

        Returns:
            list[str]
        """
        return [f for f in self._normalized_dict]

    @property
    def circular_features(self) -> dict[list[tuple[float, float]]]:
        """Returns all features that can must drawn as circles

        Returns:
            dict[list[tuple[float, float]]]
        """
        return {
            k: v
            for k, v in self._normalized_dict.items()
            if any("node" in x["topology"] and x["name"] == k
                   for x in self._features_list)
        }

    @property
    def polygonal_features(self) -> dict[list[tuple[float, float]]]:
        """Returns all features that can must drawn as polygons

        Returns:
            dict[list[tuple[float, float]]]
        """
        return {
            k: v
            for k, v in self._normalized_dict.items() if any(
                any(y in x["topology"]
                    for y in {"way", "area"}) and x["name"] == k
                for x in self._features_list)
        }

    def loadCity(self) -> None:
        """Loads city area id and bounding box (both in xy and coordinates forms)"""
        city_query = self._nominatim.query(
            self._city,
            timeout=self._timeout,
        )
        # city area id
        self._area_id = city_query.areaId()
        # bounding box
        raw_bbox = city_query.toJSON()[0]["boundingbox"]
        self._bbox = (
            float(raw_bbox[0]),
            float(raw_bbox[2]),
            float(raw_bbox[1]),
            float(raw_bbox[3]),
        )

    def _coordsToXY(self, lat: float, lon: float) -> tuple[float, float]:
        """Calculates points x and y coordinates according to its latitude and longitude

        Args:
            lat (float)
            lon (float)

        Returns:
            tuple[float, float]: x, y coordinates
        """
        x = (lat - self._bbox[0]) / (self._bbox[2] - self._bbox[0])
        y = (lon - self._bbox[1]) / (self._bbox[3] - self._bbox[1])

        return x, y

    def _isPositionValid(self, *_) -> bool:
        """Is the provided position valid?
        Let's just say that the position is valid. The actual check is delegated to the subclasses.

        Returns:
            bool:
        """
        return True

    def _queryOSM(self, **kwargs) -> None:
        """Query OSM and load data into self._elements_dict

        Keyword args:
            name (str): Element type.
            tag (str): Element tag.
            topology (str): Element topology.
        """

        if not self._bbox:
            raise ValueError("Bounding Box not loaded.")

        self._elements_dict[kwargs["name"]] = []

        for t in kwargs["tag"]:
            query = overpassQueryBuilder(
                bbox=self._bbox,
                selector=t,
                elementType=kwargs["topology"],
                includeGeometry=True,
            )

            while True:
                try:
                    results = self._overpass.query(
                        query,
                        timeout=self._timeout,
                    )
                    break
                except Exception as _:
                    # OFC they couldn't raise proper exceptions.
                    # this exceptions is a "generic" exception.
                    logging.error(
                        f"Trying again in {self._try_again} seconds...")
                    sleep(self._try_again)

            if not results.elements():
                continue

            if "node" in kwargs["topology"]:
                self._elements_dict[kwargs["name"]].extend(results.nodes())
            elif any(t in ["way", "area"] for t in kwargs["topology"]):
                self._elements_dict[kwargs["name"]].extend(results.ways())

    def _rotateCoordinates(self,
                           coords: list[tuple[float, float]],
                           angle: float = -90):
        """Rotates each coordinates around its center

        Args:
            coords (list[tuple[float, float]]): List of normalized coordinates
            angle (float, optional): Rotation angle. Defaults to -90.

        Returns:
            [type]: [description]
        """
        c = cos(radians(angle))
        s = sin(radians(angle))

        translated = [tuple(s - 0.5 for s in t) for t in coords]
        rotated = [(x * c - y * s, x * s + y * c) for x, y in translated]
        return [tuple(s + 0.5 for s in t) for t in rotated]

    def _normalizeElements(self, **kwargs) -> None:
        """Creates an entry in self._normalized_dict for a set of positions in self._elements_dict.
        The resulting coordinates are a list of (x, y) tuples in range [0, 1]x[0, 1], relative to
        the bounding box of the coordinates themselves.

        Keyword args:
            name (str): Element type.
            tag (str): Element tag.
            topology (str): Element topology.
        """
        if "node" in kwargs["topology"]:
            coords = [
                self._coordsToXY(e.lat(), e.lon())
                for e in self._elements_dict[kwargs["name"]]
                if self._isPositionValid(e.lat(), e.lon())
            ]

            if not coords:
                return

            # rotate coordinates
            rotated = self._rotateCoordinates(coords)
            # add to dictionary
            self._normalized_dict[kwargs["name"]] = rotated

        elif any(t in ["way", "area"] for t in kwargs["topology"]):
            self._normalized_dict[kwargs["name"]] = []

            for element in self._elements_dict[kwargs["name"]]:
                for shape in element.geometry()["coordinates"]:
                    # sometimes shape are just an element
                    if len(shape) == 1:
                        shape = shape[0]

                    # filter coords and convert to xy
                    # sometimes coords are not lists? I don't understand why
                    coords = [
                        self._coordsToXY(*s[::-1]) for s in shape
                        if isinstance(s, list) and self._isPositionValid(
                            *s[::-1])
                    ]

                    # sometimes the coords list might be empty
                    if len(coords) < 3:
                        continue

                    # rotate coordinates
                    rotated = self._rotateCoordinates(coords)
                    # add to dictionary
                    self._normalized_dict[kwargs["name"]].append(rotated)

    def loadFeatures(self) -> None:
        """Loads and normalize all features."""
        for feature in self._features_list:
            self._queryOSM(**feature)
            self._normalizeElements(**feature)
Exemplo n.º 17
0
from OSMPythonTools.api import Api

api = Api()
way = api.query('way/5887599')
print('building:        %s' % way.tag('building'))
print('historic         %s' % way.tag('historic'))
print('architect:       %s' % way.tag('architect'))
print('website:         %s' % way.tag('website'))

print('=' * 6, 'Example 2', '=' * 6)

from OSMPythonTools.overpass import Overpass

overpass = Overpass()
result = overpass.query('way["name"="Stephansdom"]; out body;')
stephansdom = result.elements()[0]
print('name:en:         %s' % stephansdom.tag('name:en'))
print('address:         %s %s, %s %s' %
      (stephansdom.tag('addr:street'), stephansdom.tag('addr:housenumber'),
       stephansdom.tag('addr:postcode'), stephansdom.tag('addr:city')))
print('building:        %s' % stephansdom.tag('building'))
print('denomination:    %s' % stephansdom.tag('denomination'))

print('=' * 6, 'Example 3', '=' * 6)

from OSMPythonTools.nominatim import Nominatim
nominatim = Nominatim()
areaId = nominatim.query('Vienna').areaId()

from OSMPythonTools.overpass import overpassQueryBuilder
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']])
    '''
Exemplo n.º 19
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)
"""
Exemplo n.º 20
0
def calculate_distance_view(request):
    # initial values
    nominatim = Nominatim()

    global castle_ide
    #areaId = nominatim.query('poland').areaId()

    overpass = Overpass()
    api = Api()
    name = None
    review = None
    state = None

    country = nominatim.query('germany')

    locator = Photon()  # (user_agent='martin_jr8')
    areaId = country.areaId()
    names_query = country.toJSON()
    country_details = names_query[0]
    country_coor_lat = country_details["lat"]
    country_coor_lon = country_details["lon"]

    # castles location query
    castle_query = overpassQueryBuilder(area=areaId,
                                        elementType='node',
                                        selector='castle_type',
                                        out='body')
    castle_objects = overpass.query(castle_query)
    castle_list = castle_objects.elements()

    # castle_infos = []
    # for castle in castle_list:
    #     castle_infos.append(castle.tags())

    # initail folium map
    m = folium.Map(location=[country_coor_lat, country_coor_lon], zoom_start=6)

    for castle_loc in castle_list:
        info_dict = castle_loc.tags()
        castle_id = castle_loc.id()
        castle_name = info_dict.get('name')
        if castle_name == None:
            continue
        else:
            folium.Marker(
                [castle_loc.lat(), castle_loc.lon()],
                tooltip=castle_name,
                popup=folium.Popup(popup_box(info_dict.get('name'), castle_id),
                                   max_width=500),
                icon=folium.Icon(color='purple',
                                 icon='fort-awesome',
                                 prefix='fa')).add_to(m)

    if request.user.is_authenticated:
        user = Profile.objects.get(user=request.user)
        loc = user.location
        location = locator.geocode(loc)
        l_lat = location.latitude
        l_lon = location.longitude
        loc_point = (l_lat, l_lon)

        folium.Marker([l_lat, l_lon],
                      tooltip='Your Location',
                      popup=location,
                      icon=folium.Icon(color='blue', icon='home',
                                       prefix='fa')).add_to(m)

    if request.method == 'GET' and request.is_ajax():
        castle_ide = request.GET.get('castle_id')
        castle_data = api.query(f'node/{castle_ide}')
        castle_details = castle_data.tags()

        # if request.user.is_authenticated and user.location != None:
        #     castle_lat = castle_data.lat()
        #     castle_lon = castle_data.lon()
        #     castle_point = (castle_lat, castle_lon)
        #     distance = round(geodesic(loc_point, castle_point).km, 2)
        #     castle_details["distance"] = distance

        # line = folium.PolyLine(locations=[loc_point, castle_point], weight=2, color='blue')
        # m.add_child(line)
        # m = m._repr_html_()

        return HttpResponse(json.dumps(castle_details),
                            content_type="application/json")

    if request.user.is_authenticated and request.method == 'POST' and request.is_ajax(
    ):
        castleName = request.POST.get('castleName')
        review = request.POST.get('review')
        state = request.POST.get('state')
        user = request.user
        ide = castle_ide
        form = LocationsModelForm()
        form.save(user, castleName, review, state, ide)

        folium.Marker([52.352, 6.22],
                      tooltip='Your Location',
                      popup=location,
                      icon=folium.Icon(color='green', icon='home',
                                       prefix='fa')).add_to(m)
        messages.info(request, 'success. Review saved')

        # m = m._repr_html_()

        # return redirect('/')

        # return HttpResponseRedirect('measurements/main.html')

        # return render(request, 'measurements/main.html', {'map': m})

        # return render(request, 'measurements/main.html')

        # return HttpResponse(request)

    m = m._repr_html_()

    context = {
        # 'name': name,
        # 'review': review,
        # 'state': state,
        # 'map': m,
        # 'castles': country_coor_lat,
        # 'ops' : border_objects
        # 'name': requested_id
    }

    return render(request, 'measurements/main.html', {'map': m})
Exemplo n.º 21
0
import overpy
from OSMPythonTools.api import Api
from OSMPythonTools.overpass import Overpass

api = Api()
op = Overpass()

way = api.query('way/290083775')

res = op.query('way(id:29008377);')
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")
# loop over postcodes and get list of supermarkets

plzIdx = 0
for plz in plzList:
    plzIdx += 1
    if plzIdx % 100 == 0:
        print(plzIdx, '/', len(plzList))
    areaId = nominatim.query(plz).areaId()
    if areaId is None:
        continue
    query = overpassQueryBuilder(area=areaId,
                                 elementType='node',
                                 selector='"shop"="supermarket"',
                                 out='body')  # includeGeometry=True,
    result = overpass.query(query, timeout=60)

    if result.countElements() <= 0:
        continue
    for item in result.elements():
        if item is None:
            continue
        if item.tag('addr:street') == None or item.tag(
                'addr:postcode') == None or item.tag('name') == None:
            continue
        #add row to output
        writer.writerow({
            'postcode':
            str(item.tag('addr:postcode')),
            'name':
            item.tag('name'),