Path: blob/main/tests/test_vector_layers.py
1593 views
""" "1Test Vector Layers2------------------34"""56import json78from folium import Map9from folium.utilities import get_bounds, normalize10from folium.vector_layers import (11Circle,12CircleMarker,13Polygon,14PolyLine,15Rectangle,16path_options,17)181920def test_circle():21m = Map()22radius = 1000023popup = f"I am {radius} meters"24location = [-27.551667, -48.478889]2526circle = Circle(27location=location,28radius=radius,29color="black",30weight=2,31fill_opacity=0.6,32opacity=1,33fill=True,34popup=popup,35)36circle.add_to(m)3738expected_options = {39"bubblingMouseEvents": True,40"color": "black",41"dashArray": None,42"dashOffset": None,43"fill": True,44"fillColor": "black",45"fillOpacity": 0.6,46"fillRule": "evenodd",47"lineCap": "round",48"lineJoin": "round",49"opacity": 1,50"radius": radius,51"stroke": True,52"weight": 2,53}5455m._repr_html_()56expected_rendered = f"""57var {circle.get_name()} = L.circle(58{location},59{{60"bubblingMouseEvents": true,61"color": "black",62"dashArray": null,63"dashOffset": null,64"fill": true,65"fillColor": "black",66"fillOpacity": 0.6,67"fillRule": "evenodd",68"lineCap": "round",69"lineJoin": "round",70"opacity": 1,71"radius": {radius},72"stroke": true,73"weight": 274}}75)76.addTo({m.get_name()});77""" # noqa7879rendered = circle._template.module.script(circle)80assert normalize(rendered) == normalize(expected_rendered)81assert circle.get_bounds() == [location, location]82assert json.dumps(circle.to_dict()) == circle.to_json()83assert circle.location == [-27.551667, -48.478889]84assert circle.options == expected_options858687def test_circle_marker():88m = Map()89radius = 5090popup = f"I am {radius} pixels"91location = [-27.55, -48.8]9293circle_marker = CircleMarker(94location=location,95radius=radius,96color="black",97weight=2,98fill_opacity=0.6,99opacity=1,100fill=True,101popup=popup,102)103circle_marker.add_to(m)104105options = {106"bubblingMouseEvents": True,107"color": "black",108"dashArray": None,109"dashOffset": None,110"fill": True,111"fillColor": "black",112"fillOpacity": 0.6,113"fillRule": "evenodd",114"lineCap": "round",115"lineJoin": "round",116"opacity": 1,117"radius": radius,118"stroke": True,119"weight": 2,120}121122m._repr_html_()123expected_bounds = [location, location]124expected_rendered = f"""125var {circle_marker.get_name()} = L.circleMarker(126{location},127{{128"bubblingMouseEvents": true,129"color": "black",130"dashArray": null,131"dashOffset": null,132"fill": true,133"fillColor": "black",134"fillOpacity": 0.6,135"fillRule": "evenodd",136"lineCap": "round",137"lineJoin": "round",138"opacity": 1,139"radius": {radius},140"stroke": true,141"weight": 2142}}143)144.addTo({m.get_name()});145""" # noqa146147rendered = circle_marker._template.module.script(circle_marker)148assert normalize(rendered) == normalize(expected_rendered)149assert circle_marker.get_bounds() == expected_bounds150assert json.dumps(circle_marker.to_dict()) == circle_marker.to_json()151assert circle_marker.location == location152assert circle_marker.options == options153154155def test_rectangle():156m = Map()157158location = [[45.6, -122.8], [45.61, -122.7]]159rectangle = Rectangle(160bounds=location,161popup="I am a rectangle",162color="black",163weight=2,164fill_opacity=0.6,165opacity=1,166fill=True,167)168rectangle.add_to(m)169170expected_options = {171"bubblingMouseEvents": True,172"color": "black",173"dashArray": None,174"dashOffset": None,175"fill": True,176"fillColor": "black",177"fillOpacity": 0.6,178"fillRule": "evenodd",179"lineCap": "round",180"lineJoin": "round",181"noClip": False,182"opacity": 1,183"smoothFactor": 1.0,184"stroke": True,185"weight": 2,186}187188m._repr_html_()189expected_rendered = f"""190var {rectangle.get_name()} = L.rectangle(191{location},192{{193"bubblingMouseEvents": true,194"color": "black",195"dashArray": null,196"dashOffset": null,197"fill": true,198"fillColor": "black",199"fillOpacity": 0.6,200"fillRule": "evenodd",201"lineCap": "round",202"lineJoin": "round",203"noClip": false,204"opacity": 1,205"smoothFactor": 1.0,206"stroke": true,207"weight": 2208}}209)210.addTo({m.get_name()});211"""212213rendered = rectangle._template.module.script(rectangle)214assert normalize(rendered) == normalize(expected_rendered)215assert rectangle.get_bounds() == location216assert json.dumps(rectangle.to_dict()) == rectangle.to_json()217assert rectangle.options == expected_options218219220def test_polygon_marker():221m = Map()222locations = [223[35.6636, 139.7634],224[35.6629, 139.7664],225[35.6663, 139.7706],226[35.6725, 139.7632],227[35.6728, 139.7627],228[35.6720, 139.7606],229[35.6682, 139.7588],230[35.6663, 139.7627],231]232polygon = Polygon(locations=locations, popup="I am a polygon")233polygon.add_to(m)234235expected_options = {236"bubblingMouseEvents": True,237"color": "#3388ff",238"dashArray": None,239"dashOffset": None,240"fill": False,241"fillColor": "#3388ff",242"fillOpacity": 0.2,243"fillRule": "evenodd",244"lineCap": "round",245"lineJoin": "round",246"noClip": False,247"opacity": 1.0,248"smoothFactor": 1.0,249"stroke": True,250"weight": 3,251}252253m._repr_html_()254expected_rendered = f"""255var {polygon.get_name()} = L.polygon(256{locations},257{{258"bubblingMouseEvents": true,259"color": "#3388ff",260"dashArray": null,261"dashOffset": null,262"fill": false,263"fillColor": "#3388ff",264"fillOpacity": 0.2,265"fillRule": "evenodd",266"lineCap": "round",267"lineJoin": "round",268"noClip": false,269"opacity": 1.0,270"smoothFactor": 1.0,271"stroke": true,272"weight": 3273}}274)275.addTo({m.get_name()});276"""277278rendered = polygon._template.module.script(polygon)279assert normalize(rendered) == normalize(expected_rendered)280assert polygon.get_bounds() == get_bounds(locations)281assert json.dumps(polygon.to_dict()) == polygon.to_json()282assert polygon.options == expected_options283284285def test_polyline():286m = Map()287locations = [[40.0, -80.0], [45.0, -80.0]]288polyline = PolyLine(locations=locations, popup="I am PolyLine")289polyline.add_to(m)290291expected_options = {292"smoothFactor": 1.0,293"noClip": False,294"bubblingMouseEvents": True,295"color": "#3388ff",296"dashArray": None,297"dashOffset": None,298"fill": False,299"fillColor": "#3388ff",300"fillOpacity": 0.2,301"fillRule": "evenodd",302"lineCap": "round",303"lineJoin": "round",304"opacity": 1.0,305"stroke": True,306"weight": 3,307}308309m._repr_html_()310expected_rendered = f"""311var {polyline.get_name()} = L.polyline(312{locations},313{{314"bubblingMouseEvents": true,315"color": "#3388ff",316"dashArray": null,317"dashOffset": null,318"fill": false,319"fillColor": "#3388ff",320"fillOpacity": 0.2,321"fillRule": "evenodd",322"lineCap": "round",323"lineJoin": "round",324"noClip": false,325"opacity": 1.0,326"smoothFactor": 1.0,327"stroke": true,328"weight": 3329}}330)331.addTo({m.get_name()});332"""333334rendered = polyline._template.module.script(polyline)335assert normalize(rendered) == normalize(expected_rendered)336assert polyline.get_bounds() == get_bounds(locations)337assert json.dumps(polyline.to_dict()) == polyline.to_json()338assert polyline.options == expected_options339340341def test_mulyipolyline():342m = Map()343344locations = [345[[45.51, -122.68], [37.77, -122.43], [34.04, -118.2]],346[[40.78, -73.91], [41.83, -87.62], [32.76, -96.72]],347]348349multipolyline = PolyLine(locations=locations, popup="MultiPolyLine")350multipolyline.add_to(m)351352expected_options = {353"smoothFactor": 1.0,354"noClip": False,355"bubblingMouseEvents": True,356"color": "#3388ff",357"dashArray": None,358"dashOffset": None,359"fill": False,360"fillColor": "#3388ff",361"fillOpacity": 0.2,362"fillRule": "evenodd",363"lineCap": "round",364"lineJoin": "round",365"opacity": 1.0,366"stroke": True,367"weight": 3,368}369370m._repr_html_()371expected_rendered = f"""372var {multipolyline.get_name()} = L.polyline(373{locations},374{{375"bubblingMouseEvents": true,376"color": "#3388ff",377"dashArray": null,378"dashOffset": null,379"fill": false,380"fillColor": "#3388ff",381"fillOpacity": 0.2,382"fillRule": "evenodd",383"lineCap": "round",384"lineJoin": "round",385"noClip": false,386"opacity": 1.0,387"smoothFactor": 1.0,388"stroke": true,389"weight": 3390}}391)392.addTo({m.get_name()});393"""394395rendered = multipolyline._template.module.script(multipolyline)396assert normalize(rendered) == normalize(expected_rendered)397assert multipolyline.get_bounds() == get_bounds(locations)398assert json.dumps(multipolyline.to_dict()) == multipolyline.to_json()399assert multipolyline.options == expected_options400401402def test_path_options_lower_camel_case():403options = path_options(fill_color="red", fillOpacity=0.3)404assert options["fillColor"] == "red"405assert options["fillOpacity"] == 0.3406407408