Skip to content

09 csv to points

image image image

Converting CSV to points

Uncomment the following line to install leafmap if needed.

1
# !pip install leafmap

This notebook example requires the ipyleaflet plotting backend. Folium is not supported.

1
2
import os
import leafmap.leafmap as leafmap

Read a CSV as a Pandas DataFrame.

1
in_csv = "https://raw.githubusercontent.com/opengeos/data/main/world/world_cities.csv"
1
2
df = leafmap.csv_to_df(in_csv)
df

Create a point layer from a CSV file containing lat/long information.

1
2
3
Map = leafmap.Map()
Map.add_xy_data(in_csv, x="longitude", y="latitude", layer_name="World Cities")
Map

Set the output directory.

1
2
3
4
out_dir = os.path.expanduser("~/Downloads")
if not os.path.exists(out_dir):
    os.makedirs(out_dir)
out_shp = os.path.join(out_dir, "world_cities.shp")

Convert a CSV file containing lat/long information to a shapefile.

1
leafmap.csv_to_shp(in_csv, out_shp)
1
2
out_geojson = os.path.join(out_dir, "world_cities.geojson")
leafmap.csv_to_geojson(in_csv, out_geojson)

Convert a CSV file containing lat/long information to a GeoPandas GeoDataFrame.

1
2
# gdf = leafmap.csv_to_gdf(in_csv)
# gdf