<jupyter_start><jupyter_text>[![image](https://colab.research.google.com/assets/colab-badge.svg)](https://githubtocolab.com/giswqs/leafmap/blob/master/examples/notebooks/10_add_vector.ipynb)[![image](https://mybinder.org/badge_logo.svg)](https://gishub.org/leafmap-binder)**Adding local vector data (e.g., shp, geojson, kml) to the map**Uncomment the following line to install [leafmap](https://leafmap.org) if needed.<jupyter_code># !pip install leafmap
import leafmap
m = leafmap.Map()
m<jupyter_output><empty_output><jupyter_text>This demo is based on the ipyleaflet plotting backend. The folium plotting backend does not have the interactive GUI for loading local vector data. ![](https://i.imgur.com/hnaTPZa.gif) Add a GeoJSON to the map.<jupyter_code>m = leafmap.Map(center=[0, 0], zoom=2)

in_geojson = 'https://raw.githubusercontent.com/giswqs/leafmap/master/examples/data/cable_geo.geojson'
m.add_geojson(in_geojson, layer_name="Cable lines")

m<jupyter_output><empty_output><jupyter_text>Add a GeoJSON with random filled color to the map.<jupyter_code>m = leafmap.Map(center=[0, 0], zoom=2)
url = "https://raw.githubusercontent.com/giswqs/leafmap/master/examples/data/countries.geojson"
m.add_geojson(
    url, layer_name="Countries", fill_colors=['red', 'yellow', 'green', 'orange']
)
m<jupyter_output><empty_output><jupyter_text>Use the `style_callback`function for assigning a random color to each polygon.<jupyter_code>import random

m = leafmap.Map(center=[0, 0], zoom=2)

url = "https://raw.githubusercontent.com/giswqs/leafmap/master/examples/data/countries.geojson"


def random_color(feature):
    return {
        'color': 'black',
        'fillColor': random.choice(['red', 'yellow', 'green', 'orange']),
    }


m.add_geojson(url, layer_name="Countries", style_callback=random_color)
m<jupyter_output><empty_output><jupyter_text>Use custom `style` and `hover_style` functions.<jupyter_code>m = leafmap.Map(center=[0, 0], zoom=2)

url = "https://raw.githubusercontent.com/giswqs/leafmap/master/examples/data/countries.geojson"

style = {
    "stroke": True,
    "color": "#0000ff",
    "weight": 2,
    "opacity": 1,
    "fill": True,
    "fillColor": "#0000ff",
    "fillOpacity": 0.1,
}

hover_style = {"fillOpacity": 0.7}

m.add_geojson(url, layer_name="Countries", style=style, hover_style=hover_style)
m<jupyter_output><empty_output><jupyter_text>Add a shapefile to the map.<jupyter_code>m = leafmap.Map(center=[0, 0], zoom=2)

in_shp = '../data/countries.shp'
m.add_shp(in_shp, layer_name="Countries")

m<jupyter_output><empty_output><jupyter_text>Add a KML to the map.<jupyter_code>m = leafmap.Map()

in_kml = '../data/us_states.kml'
m.add_kml(in_kml, layer_name="US States KML")

m<jupyter_output><empty_output><jupyter_text>The `add_vector` function supports any vector data format supported by GeoPandas.<jupyter_code>m = leafmap.Map(center=[0, 0], zoom=2)
url = "https://raw.githubusercontent.com/giswqs/leafmap/master/examples/data/countries.geojson"
m.add_vector(
    url, layer_name="Countries", fill_colors=['red', 'yellow', 'green', 'orange']
)
m
m = leafmap.Map()
in_shp = '../data/countries.shp'
in_geojson = '../data/us_states.json'
in_kml = '../data/us_states.kml'
m.add_shp(in_shp, layer_name="Shapefile")
m.add_geojson(in_geojson, layer_name="GeoJSON")
m.add_kml(in_kml, layer_name="KML")
m<jupyter_output><empty_output>
{'fraction_non_alphanumeric': 0.15997621171573, 'fraction_numerical': 0.010110020814748736, 'mean_word_length': 6.127118644067797, 'pattern_counts': {'":': 8, '<': 33, '<?xml version=': 0, '>': 33, 'https://': 11, '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': '296895', 'license': 'MIT', 'n_tokens_mistral': 1332, 'n_tokens_neox': 1278, 'n_words': 216, 'path': 'examples/notebooks/10_add_vector.ipynb', 'repo_name': 'glennlaughlin/leafmap', 'size': '3363'}