<jupyter_start><jupyter_text>Tool to draw polygons on maps and capture verticesThis notebook lets you draw rectangles or polygons on a map and print out the vertices.  This is useful to generate lists of vertices as input to GeoClaw, e.g. to specify AMR regions or fixed grid output domains.Requires<jupyter_code>from IPython.display import display
import ipywidgets
from __future__ import print_function
import ipyleaflet

from ipyleaflet import (
    Map,
    Marker,
    TileLayer, ImageOverlay,
    Polyline, Polygon, Rectangle, Circle, CircleMarker,
    GeoJSON,
    DrawControl
)<jupyter_output><empty_output><jupyter_text>Latitude, Longitude of Map<jupyter_code>#center = [48.355, -124.642] # Neah Bay
center = [41.75, -124.19] # Crescent City

zoom = 11
m = Map(center=center, zoom=zoom)
m<jupyter_output><empty_output><jupyter_text>Map Widget and drawing tool Definitions<jupyter_code>zoom = 13
c = ipywidgets.Box()

topo_background = True   # Use topo as background rather than map?

if topo_background:
    m = Map(width='1000px',height='600px', center=center, zoom=zoom, \
        default_tiles=TileLayer(url=u'http://otile1.mqcdn.com/tiles/1.0.0/sat/{z}/{x}/{y}.jpg'))
else:
    m = Map(width='1000px',height='600px', center=center, zoom=zoom)
    
c.children = [m]

# keep track of rectangles and polygons drawn on map:
def clear_m():
    global rects,polys
    rects = set()
    polys = set()
    
clear_m()
rect_color = '#a52a2a'
poly_color = '#00F'

myDrawControl = DrawControl(
rectangle={'shapeOptions':{'color':rect_color}},
        polygon={'shapeOptions':{'color':poly_color}}) #,polyline=None)

def handle_draw(self, action, geo_json):
    global rects,polys
    polygon=[]
    for coords in geo_json['geometry']['coordinates'][0][:-1][:]:
        polygon.append(tuple(coords))
    polygon = tuple(polygon)
    if geo_json['properties']['style']['color'] == '#00F':  # poly
        if action == 'created':
            polys.add(polygon)
        elif action == 'deleted':
            polys.discard(polygon)
    if geo_json['properties']['style']['color'] == '#a52a2a':  # rect
        if action == 'created':
            rects.add(polygon)
        elif action == 'deleted':
            rects.discard(polygon)
myDrawControl.on_draw(handle_draw)
m.add_control(myDrawControl)<jupyter_output><empty_output><jupyter_text>On the map below, select the polygon tool or the rectangle tool and start clicking points.  You can add several of each if you want.  **Note:**  - If you delete one, you must then click 'save'.   - The edit function doesn't save the edited version (need fixing).<jupyter_code>clear_m()
display(m)<jupyter_output><empty_output><jupyter_text>Now you can print the coordinates of the vertices:Note that 5 digits gives about 1 meter precision.<jupyter_code>for r in polys: 
    print("\nPolygon vertices:")
    for c in r: print('%10.5f, %10.5f' % c)
        
for r in rects: 
    print("\nRectangle vertices:")
    for c in r: print('%10.5f, %10.5f' % c)<jupyter_output><empty_output><jupyter_text>Re-execute the cell above the map to clear and specify a new set of polygons.  Other ways to print the verticesDepending on what you want to do with the output, you might want to print in a different format.  Here are some examples... Rectangle as a tuple x1,y1,x2,y2 of corners:This format is used in specifying a GeoClaw AMR "region".<jupyter_code>for r in rects:
    print("\nCoordinates of lower left and upper right corner of rectangle:")
    x1 = r[0][0]
    x2 = r[2][0]
    y1 = r[0][1]
    y2 = r[2][1]
    print("x1, y1, x2, y2 = %10.5f, %10.5f, %10.5f, %10.5f" % (x1,y1,x2,y2))<jupyter_output><empty_output><jupyter_text>As tuples x and y:This format is used in specifying a GeoClaw fgmax rectangle or quadrilateral.<jupyter_code>for r in rects:
    print("\nCoordinates of lower left and upper right corner of rectangle:")
    x1 = r[0][0]
    x2 = r[2][0]
    y1 = r[0][1]
    y2 = r[2][1]
    print("x = %10.5f, %10.5f" % (x1,x2))
    print("y = %10.5f, %10.5f" % (y1,y2))
    

for r in polys:
    print("\nCoordinates of distinct vertices of polygon:")
    sx = 'x = '
    sy = 'y = '
    for j in range(len(r)-1):
        sx = sx + ' %10.5f,' % r[j][0]
        sy = sy + ' %10.5f,' % r[j][1]
    print(sx)
    print(sy)<jupyter_output><empty_output><jupyter_text>To create kml files:You can create a set of kml files for each rectangle or polygon with the code below.  `poly2kml` is recently added to `geoclaw.kmltools`.<jupyter_code>from clawpack.geoclaw import kmltools
reload(kmltools)
for i,r in enumerate(rects):
    x1 = r[0][0]
    x2 = r[2][0]
    y1 = r[0][1]
    y2 = r[2][1]
    name = "rect%i" % i
    kmltools.box2kml((x1,x2,y1,y2), name=name, verbose=True)
for i,r in enumerate(polys):
    x = [xy[0] for xy in r]
    y = [xy[1] for xy in r]
    kmltools.poly2kml((x,y), name="poly%i" % i, verbose=True)<jupyter_output><empty_output>
{'fraction_non_alphanumeric': 0.1335511537676128, 'fraction_numerical': 0.033081478456197674, 'mean_word_length': 3.8447082096933727, 'pattern_counts': {'":': 0, '<': 33, '<?xml version=': 0, '>': 33, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'chain_length': '8', 'dir': 'jupyter-structured-clean-dedup', 'id': '374876', 'license': 'BSD-2-Clause', 'n_tokens_mistral': 1858, 'n_tokens_neox': 1770, 'n_words': 501, 'path': 'misc/ipyleaflet_polygon_selector.ipynb', 'repo_name': 'rjleveque/binder_experiments', 'size': '4897'}