Ejemplo n.º 1
0
def SelectMap():
    """Create a Map object using ipyleaflet module.
    
    Returns: Map,DrawControl (ipyleaflet objects)
    """
    m = Map(basemap=basemaps.Esri.WorldStreetMap,
            center=(40.853294, 14.305573),
            zoom=4,
            dragging=True,
            scroll_wheel_zoom=True,
            world_copy_jump=False)
    draw_control = DrawControl()
    draw_control.rectangle = {
        "shapeOptions": {
            "fillColor": "#fca45d",
            "color": "#fca45d",
            "fillOpacity": 0.3
        }
    }
    # only rectangle selection is provided
    draw_control.circle = {}
    draw_control.polygon = {}
    draw_control.polyline = {}
    draw_control.circlemarker = {}
    return m, draw_control
Ejemplo n.º 2
0
    def __init__(self, bbox, zoom=8, resolution=10):
        center = (bbox.min_y + bbox.max_y) / 2, (bbox.min_x + bbox.max_x) / 2
        self.map = Map(center=center, zoom=zoom, scroll_wheel_zoom=True)

        self.resolution = resolution

        control = DrawControl()

        control.rectangle = {
            "shapeOptions": {
                "fillColor": "#fabd14",
                "color": "#fa6814",
                "fillOpacity": 0.2
            }
        }

        #Disable the rest of draw options
        control.polyline = {}
        control.circle = {}
        control.circlemarker = {}
        control.polygon = {}
        control.edit = False
        control.remove = False

        control.on_draw(self._handle_draw)

        self.map.add_control(control)

        self.bbox = None
        self.size = None
        self.rectangle = None
        self.add_rectangle(bbox.min_x, bbox.min_y, bbox.max_x, bbox.max_y)
Ejemplo n.º 3
0
from ipyleaflet import DrawControl

# create map
draw_control_map = ipyleaflet.Map(zoom=1)

# create control
draw_control = DrawControl()

# add control to map
draw_control_map.add_control(draw_control)

# add extra options to control
draw_control.circle = {
    "shapeOptions": {
        "fillColor": "blue",
        "color": 'blue',
        "fillOpacity": 0.5
    }
}

# display map
draw_control_map
# In[21]

import ipyleaflet
from ipyleaflet import MeasureControl

# create map
measure_control_map = ipyleaflet.Map(zoom=1)

# create control
Ejemplo n.º 4
0
def mk_map_region_selector(height='600px', **kwargs):
    from ipyleaflet import Map, WidgetControl, FullScreenControl, DrawControl
    from ipywidgets.widgets import Layout, Button, HTML
    from types import SimpleNamespace

    state = SimpleNamespace(selection=None, bounds=None, done=False)

    btn_done = Button(description='done', layout=Layout(width='5em'))
    btn_done.style.button_color = 'green'
    btn_done.disabled = True

    html_info = HTML(
        layout=Layout(flex='1 0 20em', width='20em', height='3em'))

    def update_info(txt):
        html_info.value = '<pre style="color:grey">' + txt + '</pre>'

    m = Map(**kwargs) if len(kwargs) else Map(zoom=2)
    m.scroll_wheel_zoom = True
    m.layout.height = height

    widgets = [
        WidgetControl(widget=btn_done, position='topright'),
        WidgetControl(widget=html_info, position='bottomleft'),
    ]
    for w in widgets:
        m.add_control(w)

    draw = DrawControl()

    draw.circle = {}
    draw.polyline = {}
    draw.circlemarker = {}

    shape_opts = {
        "fillColor": "#fca45d",
        "color": "#000000",
        "fillOpacity": 0.1
    }
    draw.rectangle = {"shapeOptions": shape_opts}
    poly_opts = {"shapeOptions": dict(**shape_opts)}
    poly_opts["shapeOptions"]["original"] = dict(**shape_opts)
    poly_opts["shapeOptions"]["editing"] = dict(**shape_opts)

    draw.polygon = poly_opts
    draw.edit = True
    draw.remove = True
    m.add_control(draw)
    m.add_control(FullScreenControl())

    def on_done(btn):
        state.done = True
        btn_done.disabled = True
        m.remove_control(draw)
        for w in widgets:
            m.remove_control(w)

    def bounds_handler(event):
        (lat1, lon1), (lat2, lon2) = event['new']
        txt = 'lat: [{:.{n}f}, {:.{n}f}]\nlon: [{:.{n}f}, {:.{n}f}]'.format(
            lat1, lat2, lon1, lon2, n=4)
        update_info(txt)
        state.bounds = dict(lat=(lat1, lat2), lon=(lon1, lon2))

    def on_draw(event):
        v = event['new']
        action = event['name']
        if action == 'last_draw':
            state.selection = v['geometry']
        elif action == 'last_action' and v == 'deleted':
            state.selection = None

        btn_done.disabled = state.selection is None

    draw.observe(on_draw)
    m.observe(bounds_handler, ('bounds', ))
    btn_done.on_click(on_done)

    return m, state
Ejemplo n.º 5
0
def mk_map_region_selector(map=None, height="600px", **kwargs):
    from ipyleaflet import Map, WidgetControl, FullScreenControl, DrawControl
    from ipywidgets.widgets import Layout, Button, HTML
    from types import SimpleNamespace

    state = SimpleNamespace(selection=None, bounds=None, done=False)

    btn_done = Button(description="done", layout=Layout(width="5em"))
    btn_done.style.button_color = "green"
    btn_done.disabled = True

    html_info = HTML(layout=Layout(flex="1 0 20em", width="20em", height="3em"))

    def update_info(txt):
        html_info.value = '<pre style="color:grey">' + txt + "</pre>"

    def render_bounds(bounds):
        (lat1, lon1), (lat2, lon2) = bounds
        txt = "lat: [{:.{n}f}, {:.{n}f}]\nlon: [{:.{n}f}, {:.{n}f}]".format(
            lat1, lat2, lon1, lon2, n=4
        )
        update_info(txt)

    if map is None:
        m = Map(**kwargs) if len(kwargs) else Map(zoom=2)
        m.scroll_wheel_zoom = True
        m.layout.height = height
    else:
        m = map
        render_bounds(m.bounds)

    widgets = [
        WidgetControl(widget=btn_done, position="topright"),
        WidgetControl(widget=html_info, position="bottomleft"),
    ]
    for w in widgets:
        m.add_control(w)

    draw = DrawControl()

    draw.circle = {}
    draw.polyline = {}
    draw.circlemarker = {}

    shape_opts = {"fillColor": "#fca45d", "color": "#000000", "fillOpacity": 0.1}
    draw.rectangle = {"shapeOptions": shape_opts, "metric": ["km", "m"]}
    poly_opts = {"shapeOptions": dict(**shape_opts)}
    poly_opts["shapeOptions"]["original"] = dict(**shape_opts)
    poly_opts["shapeOptions"]["editing"] = dict(**shape_opts)

    draw.polygon = poly_opts
    draw.edit = True
    draw.remove = True
    m.add_control(draw)
    m.add_control(FullScreenControl())

    def on_done(btn):
        state.done = True
        btn_done.disabled = True
        m.remove_control(draw)
        for w in widgets:
            m.remove_control(w)

    def bounds_handler(event):
        bounds = event["new"]
        render_bounds(bounds)
        (lat1, lon1), (lat2, lon2) = bounds
        state.bounds = dict(lat=(lat1, lat2), lon=(lon1, lon2))

    def on_draw(event):
        v = event["new"]
        action = event["name"]
        if action == "last_draw":
            state.selection = v["geometry"]
        elif action == "last_action" and v == "deleted":
            state.selection = None

        btn_done.disabled = state.selection is None

    draw.observe(on_draw)
    m.observe(bounds_handler, ("bounds",))
    btn_done.on_click(on_done)

    return m, state
Ejemplo n.º 6
0
def draw_map(routes):
    tiles = create_dummy()
    m = Map(layers=(basemap_to_tiles(tiles, "2017-04-08"), ),
            zoom=12,
            layout=Layout(width='100%', height='600px'))

    tiles30 = create_dummy()
    tiles30[
        "url"] = 'https://wmts.geo.admin.ch/1.0.0/ch.swisstopo.hangneigung-ueber_30/default/current/3857/{z}/{x}/{y}.png'
    tiles["opacity"] = .0
    layer = basemap_to_tiles(tiles30, "adad")

    layer.opacity = 0.2
    m.add_layer(layer)

    tiles30 = create_dummy()
    tiles30[
        "url"] = 'https://wmts.geo.admin.ch/1.0.0/ch.swisstopo-karto.skitouren/default/current/3857/{z}/{x}/{y}.png'
    tiles["opacity"] = .0
    layer = basemap_to_tiles(tiles30, "adad")

    layer.opacity = 0.9
    m.add_layer(layer)

    # layer = ipyl.GeoJSON(data=geojson, hover_style={'fillColor': 'red'})

    # def hover_handler(event=None, id=None, properties=None):
    #    label.value = properties['geounit']

    # layer.on_hover(hover_handler)
    draw_control = DrawControl()
    draw_control.polyline = {
        "shapeOptions": {
            "color": "#6bc2e5",
            "weight": 8,
            "opacity": 1.0
        }
    }
    draw_control.polygon = {
        "shapeOptions": {
            "fillColor": "#6be5c3",
            "color": "#6be5c3",
            "fillOpacity": 1.0
        },
        "drawError": {
            "color": "#dd253b",
            "message": "Oups!"
        },
        "allowIntersection": False
    }
    draw_control.circle = {
        "shapeOptions": {
            "fillColor": "#efed69",
            "color": "#efed69",
            "fillOpacity": 1.0
        }
    }
    draw_control.rectangle = {
        "shapeOptions": {
            "fillColor": "#fca45d",
            "color": "#fca45d",
            "fillOpacity": 1.0
        }
    }

    def on_draw(x, geo_json, action):
        route = routes[0]
        cs_ch = []
        for ps in geo_json["geometry"]["coordinates"]:
            p = Point(ps[0], ps[1])
            p = transform(wgs2ch, p)
            cs_ch.append([p.x, p.y])

        geo_json["geometry"]["coordinates"] = cs_ch

        route.geo_path = geo_json

    draw_control.on_draw(on_draw)

    m.add_control(draw_control)

    for route in routes:

        if route.geo_path is not None:
            geo_json = copy.deepcopy(route.geo_path)
            cs_ch = []
            for ps in geo_json["geometry"]["coordinates"]:
                p = Point(ps[0], ps[1])
                p = transform(ch2wgs, p)
                cs_ch.append([p.x, p.y])

            geo_json["geometry"]["coordinates"] = cs_ch

            geo_json = GeoJSON(data=geo_json)
            m.add_layer(geo_json)

        points = []
        labels = []
        points += [transform(ch2wgs, w.point) for w in route.get_waypoints()]
        labels += [route.get_name() for p in route.get_waypoints()]
        for p, label in zip(points, labels):
            marker = CircleMarker()
            marker.location = (p.y, p.x)
            marker.color = "blue"
            marker.title = label

            m.add_layer(marker)

        points = []
        labels = []
        points += [
            transform(ch2wgs, stop.point) for stop in route.get_pt_stops()
        ]
        labels += [route.get_name() for p in route.get_pt_stops()]
        for p, label in zip(points, labels):
            marker = CircleMarker()
            marker.location = (p.y, p.x)
            marker.color = "red"
            marker.title = label

            m.add_layer(marker)
    p = points[0]
    m.center = (p.y, p.x)
    return m