Skip to content

ipyleafletmap module

Map (Map)

Source code in rasterarea/ipyleafletmap.py
class Map(ipyleaflet.Map):

    def __init__(self, center=[20, 0], zoom=2, **kwargs) -> None:

        if "scroll_wheel_zoom" not in kwargs:
            kwargs["scroll_wheel_zoom"] = True

        super().__init__(center=center, zoom=zoom, **kwargs)

        if "height" not in kwargs:
            self.layout.height = "500px"
        else:
            self.layout.height = kwargs["height"]

        if "fullscreen_control" not in kwargs:
            kwargs["fullscreen_control"] = True
        if kwargs["fullscreen_control"]:
            self.add_fullscreen_control()

        if "layers_control" not in kwargs:
            kwargs["layers_control"] = True
        if kwargs["layers_control"]:
            self.add_layers_control()

    def add_search_control(self, position="topleft", **kwargs):
        """Adds a search control to the map.

        Args:
            kwargs: Keyword arguments to pass to the search control.
        """
        if "url" not in kwargs:
            kwargs["url"] = 'https://nominatim.openstreetmap.org/search?format=json&q={s}'


        search_control = ipyleaflet.SearchControl(position=position, **kwargs)
        self.add_control(search_control)

    def add_draw_control(self, **kwargs):
        """Adds a draw control to the map.

        Args:
            kwargs: Keyword arguments to pass to the draw control.
        """
        draw_control = ipyleaflet.DrawControl(**kwargs)

        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
            }
        }

        self.add_control(draw_control)

    def add_layers_control(self, position="topright"):
        """Adds a layers control to the map.

        Args:
            kwargs: Keyword arguments to pass to the layers control.
        """
        layers_control = ipyleaflet.LayersControl(position=position)
        self.add_control(layers_control)

    def add_fullscreen_control(self, position="topleft"):
        """Adds a fullscreen control to the map.

        Args:
            kwargs: Keyword arguments to pass to the fullscreen control.
        """
        fullscreen_control = ipyleaflet.FullScreenControl(position=position)
        self.add_control(fullscreen_control)

    def add_tile_layer(self, url, name, attribution="", **kwargs):
        """Adds a tile layer to the map.

        Args:
            url (str): The URL template of the tile layer.
            attribution (str): The attribution of the tile layer.
            name (str, optional): The name of the tile layer. Defaults to "OpenStreetMap".
            kwargs: Keyword arguments to pass to the tile layer.
        """
        tile_layer = ipyleaflet.TileLayer(url=url, attribution=attribution, name=name, **kwargs)
        self.add_layer(tile_layer)

    def add_basemap(self, basemap, **kwargs):

        import xyzservices.providers as xyz

        if basemap.lower() == "roadmap":
            url = 'http://mt0.google.com/vt/lyrs=m&hl=en&x={x}&y={y}&z={z}'
            self.add_tile_layer(url, name=basemap, **kwargs)
        elif basemap.lower() == "satellite":
            url = 'http://mt0.google.com/vt/lyrs=y&hl=en&x={x}&y={y}&z={z}'
            self.add_tile_layer(url, name=basemap, **kwargs)
        else:
            try:
                basemap = eval(f"xyz.{basemap}")
                url = basemap.build_url()
                attribution = basemap.attribution
                self.add_tile_layer(url, name=basemap.name, attribution=attribution, **kwargs)
            except:
                raise ValueError(f"Basemap '{basemap}' not found.")

    def add_geojson(self, data, name='GeoJSON', **kwargs):
        """Adds a GeoJSON layer to the map.

        Args:
            data (dict): The GeoJSON data.
        """
        if isinstance(data, str):
            import json
            with open(data, "r") as f:
                data = json.load(f)
        geojson = ipyleaflet.GeoJSON(data=data,name=name, **kwargs)
        self.add_layer(geojson)


    def add_shp(self, data, name='Shapefile', **kwargs):
        """Adds a Shapefile layer to the map.

        Args:
            data (str): The path to the Shapefile.
        """
        import geopandas as gpd
        gdf = gpd.read_file(data)
        geojson = gdf.__geo_interface__
        self.add_geojson(geojson, name=name, **kwargs)

    def add_vector(self, data, name='Vector', **kwargs):
        """Adds a vector layer to the map.

        Args:
            data (str): The path to the vector file.
        """
        import geopandas as gpd
        gdf = gpd.read_file(data)
        geojson = gdf.__geo_interface__
        self.add_geojson(geojson, name=name, **kwargs)

    def add_raster(self, url, name='Raster', fit_bounds=True, **kwargs):
        """Adds a raster layer to the map.

        Args:
            url (str): The URL of the raster layer.
            name (str, optional): The name of the raster layer. Defaults to 'Raster'.
            fit_bounds (bool, optional): Whether to fit the map bounds to the raster layer. Defaults to True.
        """
        import httpx

        titiler_endpoint = "https://titiler.xyz"

        r = httpx.get(
            f"{titiler_endpoint}/cog/info",
            params = {
                "url": url,
            }
        ).json()

        bounds = r["bounds"]

        r = httpx.get(
            f"{titiler_endpoint}/cog/tilejson.json",
            params = {
                "url": url,
            }
        ).json()

        tile = r["tiles"][0]

        self.add_tile_layer(url=tile, name=name, **kwargs)

        if fit_bounds:
            bbox = [[bounds[1], bounds[0]], [bounds[3], bounds[2]]]
            self.fit_bounds(bbox)

    def add_image(self, url, width, height, position = 'bottomright',**kwargs):
        """Add an image to the map.

        Args:
            url (str): The URL of the image.
            width (int): The width of the image.
            height (int): The height of the image.
            position (str, optional): The position of the image. Defaults to 'bottomright'.
        """
        widget = widgets.HTML(value = f'<img src="{url}" width="{width}" height="{height}">')
        control = WidgetControl(widget=widget, position=position)
        self.add(control)


    def csv_to_shp(in_csv,out_shp, x="longitude", y="latitude"):
        """
        This function takes a csv file and converts it to a shapefile.
        """

        # Read in the csv file
        df = pd.read_csv(in_csv)

        # Create a geometry column
        geometry = [Point(xy) for xy in zip(df[x], df[y])]

        # Create a geodataframe
        gdf = gpd.GeoDataFrame(df, geometry=geometry)

        # Save the geodataframe as a shapefile
        gdf.to_file(out_shp,driver='ESRI Shapefile')

        return gdf


    def csv_to_geojson(in_csv, out_geojson, x="longitude", y="latitude"):
        """
        This function takes a csv file and converts it to a geojson file.
        """

        # Read in the csv file
        df = pd.read_csv(in_csv)

        # Create a geometry column
        geometry = [Point(xy) for xy in zip(df[x], df[y])]

        # Create a geodataframe
        gdf = gpd.GeoDataFrame(df, geometry=geometry)

        # Save the geodataframe as a geojson file
        gdf.to_file(out_geojson, driver="GeoJSON")

        return gdf


    def add_marker_from_csv(self, in_csv, x="longitude", y="latitude", label=None, layer_name="Marker cluster"):
        """
        This function takes a csv file and adds a marker cluster to the map.
        """

        # Read in the csv file
        df = pd.read_csv(in_csv)

        # Create a geometry column
        geometry = [Point(xy) for xy in zip(df[x], df[y])]

        # Create a geodataframe
        gdf = gpd.GeoDataFrame(df, geometry=geometry)

        # Create a marker cluster
        marker_cluster = ipyleaflet.MarkerCluster(
            markers=[ipyleaflet.Marker(location=[point.y, point.x]) for point in gdf.geometry]
        )

        # Add the marker cluster to the map
        self.add_layer(marker_cluster)

        return marker_cluster


    def add_toolbar(self, position='topright', **kwargs):
        """Adds a toolbar to the map.

        Args:
            toolbar (str, optional): The toolbar to add. Defaults to 'draw'.
            position (str, optional): The position of the toolbar. Defaults to 'topright'.
        """
        widget_width = "250px"
        padding = "0px 0px 0px 5px"  # upper, right, bottom, left

        toolbar_button = widgets.ToggleButton(
            value=False,
            tooltip="Toolbar",
            icon="wrench",
            layout=widgets.Layout(width="28px", height="28px", padding=padding),
        )

        close_button = widgets.ToggleButton(
            value=False,
            tooltip="Close the tool",
            icon="times",
            button_style="primary",
            layout=widgets.Layout(height="28px", width="28px", padding=padding),
        )
        toolbar = widgets.HBox([toolbar_button])

        def toolbar_click(change):
            if change["new"]:
                toolbar.children = [toolbar_button, close_button]
            else:
                toolbar.children = [toolbar_button]

        toolbar_button.observe(toolbar_click, "value")

        def close_click(change):
            if change["new"]:
                toolbar_button.close()
                close_button.close()
                toolbar.close()

        close_button.observe(close_click, "value")
        rows = 2
        cols = 2
        grid = widgets.GridspecLayout(rows, cols, grid_gap="0px", layout=widgets.Layout(width="65px"))
        icons = ["folder-open", "map", "info", "question"]

        for i in range(rows):
            for j in range(cols):
                grid[i, j] = widgets.Button(description="", button_style="primary", icon=icons[i*rows+j], 
                                            layout=widgets.Layout(width="28px", padding="0px"))
        toolbar = widgets.VBox([toolbar_button])
        def toolbar_click(change):
            if change["new"]:
                toolbar.children = [widgets.HBox([close_button, toolbar_button]), grid]
            else:
                toolbar.children = [toolbar_button]

        toolbar_button.observe(toolbar_click, "value")

        toolbar_ctrl = WidgetControl(widget=toolbar, position=position)

        output = widgets.Output()
        output_ctrl = WidgetControl(widget=output, position="bottomright")
        def tool_click(b):    
            with output:
                output.clear_output()
                print(f"You clicked the {b.icon} button")

        for i in range(rows):
            for j in range(cols):
                tool = grid[i, j]
                tool.on_click(tool_click)

        with output:
            output.clear_output()
            print("Click on a button to see the output")

        basemap = widgets.Dropdown(
            options=["roadmap", "satellite"],
            value=None,
            description="Basemap:",
            style={"description_width": "initial"},
            layout=widgets.Layout(width="200px"),
        )
        basemap_ctrl = WidgetControl(widget=basemap, position="topright")

        csv_input = widgets.Text(
            value='',
            placeholder='Type the path to the CSV file',
            description='CSV File:',
            disabled=False
        )

        csv_input_ctrl = WidgetControl(widget=csv_input, position="topright")

        def tool_click(b):    
            with output:
                output.clear_output()
                print(f"You clicked the {b.icon} button")

                if b.icon == "map":
                    self.add_control(basemap_ctrl)
                elif b.icon == "folder-open":
                    self.add_control(csv_input_ctrl)

        for i in range(rows):
            for j in range(cols):
                tool = grid[i, j]
                tool.on_click(tool_click)

        def change_basemap(change):
            if change["new"]:
                self.add_basemap(basemap.value)


        def change_csv_input(change):
            if change["new"]:
                csv_file = csv_input.value
                self.add_marker_from_csv(in_csv=csv_file, x="longitude", y="latitude")

        csv_input.observe(change_csv_input, names="value")

        basemap.observe(change_basemap, names='value')

        self.add_control(toolbar_ctrl)

add_draw_control(self, **kwargs)

Adds a draw control to the map.

Parameters:

Name Type Description Default
kwargs

Keyword arguments to pass to the draw control.

{}
Source code in rasterarea/ipyleafletmap.py
def add_draw_control(self, **kwargs):
    """Adds a draw control to the map.

    Args:
        kwargs: Keyword arguments to pass to the draw control.
    """
    draw_control = ipyleaflet.DrawControl(**kwargs)

    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
        }
    }

    self.add_control(draw_control)

add_fullscreen_control(self, position='topleft')

Adds a fullscreen control to the map.

Parameters:

Name Type Description Default
kwargs

Keyword arguments to pass to the fullscreen control.

required
Source code in rasterarea/ipyleafletmap.py
def add_fullscreen_control(self, position="topleft"):
    """Adds a fullscreen control to the map.

    Args:
        kwargs: Keyword arguments to pass to the fullscreen control.
    """
    fullscreen_control = ipyleaflet.FullScreenControl(position=position)
    self.add_control(fullscreen_control)

add_geojson(self, data, name='GeoJSON', **kwargs)

Adds a GeoJSON layer to the map.

Parameters:

Name Type Description Default
data dict

The GeoJSON data.

required
Source code in rasterarea/ipyleafletmap.py
def add_geojson(self, data, name='GeoJSON', **kwargs):
    """Adds a GeoJSON layer to the map.

    Args:
        data (dict): The GeoJSON data.
    """
    if isinstance(data, str):
        import json
        with open(data, "r") as f:
            data = json.load(f)
    geojson = ipyleaflet.GeoJSON(data=data,name=name, **kwargs)
    self.add_layer(geojson)

add_image(self, url, width, height, position='bottomright', **kwargs)

Add an image to the map.

Parameters:

Name Type Description Default
url str

The URL of the image.

required
width int

The width of the image.

required
height int

The height of the image.

required
position str

The position of the image. Defaults to 'bottomright'.

'bottomright'
Source code in rasterarea/ipyleafletmap.py
def add_image(self, url, width, height, position = 'bottomright',**kwargs):
    """Add an image to the map.

    Args:
        url (str): The URL of the image.
        width (int): The width of the image.
        height (int): The height of the image.
        position (str, optional): The position of the image. Defaults to 'bottomright'.
    """
    widget = widgets.HTML(value = f'<img src="{url}" width="{width}" height="{height}">')
    control = WidgetControl(widget=widget, position=position)
    self.add(control)

add_layers_control(self, position='topright')

Adds a layers control to the map.

Parameters:

Name Type Description Default
kwargs

Keyword arguments to pass to the layers control.

required
Source code in rasterarea/ipyleafletmap.py
def add_layers_control(self, position="topright"):
    """Adds a layers control to the map.

    Args:
        kwargs: Keyword arguments to pass to the layers control.
    """
    layers_control = ipyleaflet.LayersControl(position=position)
    self.add_control(layers_control)

add_marker_from_csv(self, in_csv, x='longitude', y='latitude', label=None, layer_name='Marker cluster')

This function takes a csv file and adds a marker cluster to the map.

Source code in rasterarea/ipyleafletmap.py
def add_marker_from_csv(self, in_csv, x="longitude", y="latitude", label=None, layer_name="Marker cluster"):
    """
    This function takes a csv file and adds a marker cluster to the map.
    """

    # Read in the csv file
    df = pd.read_csv(in_csv)

    # Create a geometry column
    geometry = [Point(xy) for xy in zip(df[x], df[y])]

    # Create a geodataframe
    gdf = gpd.GeoDataFrame(df, geometry=geometry)

    # Create a marker cluster
    marker_cluster = ipyleaflet.MarkerCluster(
        markers=[ipyleaflet.Marker(location=[point.y, point.x]) for point in gdf.geometry]
    )

    # Add the marker cluster to the map
    self.add_layer(marker_cluster)

    return marker_cluster

add_raster(self, url, name='Raster', fit_bounds=True, **kwargs)

Adds a raster layer to the map.

Parameters:

Name Type Description Default
url str

The URL of the raster layer.

required
name str

The name of the raster layer. Defaults to 'Raster'.

'Raster'
fit_bounds bool

Whether to fit the map bounds to the raster layer. Defaults to True.

True
Source code in rasterarea/ipyleafletmap.py
def add_raster(self, url, name='Raster', fit_bounds=True, **kwargs):
    """Adds a raster layer to the map.

    Args:
        url (str): The URL of the raster layer.
        name (str, optional): The name of the raster layer. Defaults to 'Raster'.
        fit_bounds (bool, optional): Whether to fit the map bounds to the raster layer. Defaults to True.
    """
    import httpx

    titiler_endpoint = "https://titiler.xyz"

    r = httpx.get(
        f"{titiler_endpoint}/cog/info",
        params = {
            "url": url,
        }
    ).json()

    bounds = r["bounds"]

    r = httpx.get(
        f"{titiler_endpoint}/cog/tilejson.json",
        params = {
            "url": url,
        }
    ).json()

    tile = r["tiles"][0]

    self.add_tile_layer(url=tile, name=name, **kwargs)

    if fit_bounds:
        bbox = [[bounds[1], bounds[0]], [bounds[3], bounds[2]]]
        self.fit_bounds(bbox)

add_search_control(self, position='topleft', **kwargs)

Adds a search control to the map.

Parameters:

Name Type Description Default
kwargs

Keyword arguments to pass to the search control.

{}
Source code in rasterarea/ipyleafletmap.py
def add_search_control(self, position="topleft", **kwargs):
    """Adds a search control to the map.

    Args:
        kwargs: Keyword arguments to pass to the search control.
    """
    if "url" not in kwargs:
        kwargs["url"] = 'https://nominatim.openstreetmap.org/search?format=json&q={s}'


    search_control = ipyleaflet.SearchControl(position=position, **kwargs)
    self.add_control(search_control)

add_shp(self, data, name='Shapefile', **kwargs)

Adds a Shapefile layer to the map.

Parameters:

Name Type Description Default
data str

The path to the Shapefile.

required
Source code in rasterarea/ipyleafletmap.py
def add_shp(self, data, name='Shapefile', **kwargs):
    """Adds a Shapefile layer to the map.

    Args:
        data (str): The path to the Shapefile.
    """
    import geopandas as gpd
    gdf = gpd.read_file(data)
    geojson = gdf.__geo_interface__
    self.add_geojson(geojson, name=name, **kwargs)

add_tile_layer(self, url, name, attribution='', **kwargs)

Adds a tile layer to the map.

Parameters:

Name Type Description Default
url str

The URL template of the tile layer.

required
attribution str

The attribution of the tile layer.

''
name str

The name of the tile layer. Defaults to "OpenStreetMap".

required
kwargs

Keyword arguments to pass to the tile layer.

{}
Source code in rasterarea/ipyleafletmap.py
def add_tile_layer(self, url, name, attribution="", **kwargs):
    """Adds a tile layer to the map.

    Args:
        url (str): The URL template of the tile layer.
        attribution (str): The attribution of the tile layer.
        name (str, optional): The name of the tile layer. Defaults to "OpenStreetMap".
        kwargs: Keyword arguments to pass to the tile layer.
    """
    tile_layer = ipyleaflet.TileLayer(url=url, attribution=attribution, name=name, **kwargs)
    self.add_layer(tile_layer)

add_toolbar(self, position='topright', **kwargs)

Adds a toolbar to the map.

Parameters:

Name Type Description Default
toolbar str

The toolbar to add. Defaults to 'draw'.

required
position str

The position of the toolbar. Defaults to 'topright'.

'topright'
Source code in rasterarea/ipyleafletmap.py
def add_toolbar(self, position='topright', **kwargs):
    """Adds a toolbar to the map.

    Args:
        toolbar (str, optional): The toolbar to add. Defaults to 'draw'.
        position (str, optional): The position of the toolbar. Defaults to 'topright'.
    """
    widget_width = "250px"
    padding = "0px 0px 0px 5px"  # upper, right, bottom, left

    toolbar_button = widgets.ToggleButton(
        value=False,
        tooltip="Toolbar",
        icon="wrench",
        layout=widgets.Layout(width="28px", height="28px", padding=padding),
    )

    close_button = widgets.ToggleButton(
        value=False,
        tooltip="Close the tool",
        icon="times",
        button_style="primary",
        layout=widgets.Layout(height="28px", width="28px", padding=padding),
    )
    toolbar = widgets.HBox([toolbar_button])

    def toolbar_click(change):
        if change["new"]:
            toolbar.children = [toolbar_button, close_button]
        else:
            toolbar.children = [toolbar_button]

    toolbar_button.observe(toolbar_click, "value")

    def close_click(change):
        if change["new"]:
            toolbar_button.close()
            close_button.close()
            toolbar.close()

    close_button.observe(close_click, "value")
    rows = 2
    cols = 2
    grid = widgets.GridspecLayout(rows, cols, grid_gap="0px", layout=widgets.Layout(width="65px"))
    icons = ["folder-open", "map", "info", "question"]

    for i in range(rows):
        for j in range(cols):
            grid[i, j] = widgets.Button(description="", button_style="primary", icon=icons[i*rows+j], 
                                        layout=widgets.Layout(width="28px", padding="0px"))
    toolbar = widgets.VBox([toolbar_button])
    def toolbar_click(change):
        if change["new"]:
            toolbar.children = [widgets.HBox([close_button, toolbar_button]), grid]
        else:
            toolbar.children = [toolbar_button]

    toolbar_button.observe(toolbar_click, "value")

    toolbar_ctrl = WidgetControl(widget=toolbar, position=position)

    output = widgets.Output()
    output_ctrl = WidgetControl(widget=output, position="bottomright")
    def tool_click(b):    
        with output:
            output.clear_output()
            print(f"You clicked the {b.icon} button")

    for i in range(rows):
        for j in range(cols):
            tool = grid[i, j]
            tool.on_click(tool_click)

    with output:
        output.clear_output()
        print("Click on a button to see the output")

    basemap = widgets.Dropdown(
        options=["roadmap", "satellite"],
        value=None,
        description="Basemap:",
        style={"description_width": "initial"},
        layout=widgets.Layout(width="200px"),
    )
    basemap_ctrl = WidgetControl(widget=basemap, position="topright")

    csv_input = widgets.Text(
        value='',
        placeholder='Type the path to the CSV file',
        description='CSV File:',
        disabled=False
    )

    csv_input_ctrl = WidgetControl(widget=csv_input, position="topright")

    def tool_click(b):    
        with output:
            output.clear_output()
            print(f"You clicked the {b.icon} button")

            if b.icon == "map":
                self.add_control(basemap_ctrl)
            elif b.icon == "folder-open":
                self.add_control(csv_input_ctrl)

    for i in range(rows):
        for j in range(cols):
            tool = grid[i, j]
            tool.on_click(tool_click)

    def change_basemap(change):
        if change["new"]:
            self.add_basemap(basemap.value)


    def change_csv_input(change):
        if change["new"]:
            csv_file = csv_input.value
            self.add_marker_from_csv(in_csv=csv_file, x="longitude", y="latitude")

    csv_input.observe(change_csv_input, names="value")

    basemap.observe(change_basemap, names='value')

    self.add_control(toolbar_ctrl)

add_vector(self, data, name='Vector', **kwargs)

Adds a vector layer to the map.

Parameters:

Name Type Description Default
data str

The path to the vector file.

required
Source code in rasterarea/ipyleafletmap.py
def add_vector(self, data, name='Vector', **kwargs):
    """Adds a vector layer to the map.

    Args:
        data (str): The path to the vector file.
    """
    import geopandas as gpd
    gdf = gpd.read_file(data)
    geojson = gdf.__geo_interface__
    self.add_geojson(geojson, name=name, **kwargs)

csv_to_geojson(in_csv, out_geojson, x='longitude', y='latitude')

This function takes a csv file and converts it to a geojson file.

Source code in rasterarea/ipyleafletmap.py
def csv_to_geojson(in_csv, out_geojson, x="longitude", y="latitude"):
    """
    This function takes a csv file and converts it to a geojson file.
    """

    # Read in the csv file
    df = pd.read_csv(in_csv)

    # Create a geometry column
    geometry = [Point(xy) for xy in zip(df[x], df[y])]

    # Create a geodataframe
    gdf = gpd.GeoDataFrame(df, geometry=geometry)

    # Save the geodataframe as a geojson file
    gdf.to_file(out_geojson, driver="GeoJSON")

    return gdf

csv_to_shp(in_csv, out_shp, x='longitude', y='latitude')

This function takes a csv file and converts it to a shapefile.

Source code in rasterarea/ipyleafletmap.py
def csv_to_shp(in_csv,out_shp, x="longitude", y="latitude"):
    """
    This function takes a csv file and converts it to a shapefile.
    """

    # Read in the csv file
    df = pd.read_csv(in_csv)

    # Create a geometry column
    geometry = [Point(xy) for xy in zip(df[x], df[y])]

    # Create a geodataframe
    gdf = gpd.GeoDataFrame(df, geometry=geometry)

    # Save the geodataframe as a shapefile
    gdf.to_file(out_shp,driver='ESRI Shapefile')

    return gdf

Last update: 2023-05-11