09 csv to points
In [1]:
Copied!
# !pip install leafmap
# !pip install leafmap
This notebook example requires the ipyleaflet plotting backend. Folium is not supported.
In [2]:
Copied!
import os
import leafmap.leafmap as leafmap
import os
import leafmap.leafmap as leafmap
Read a CSV as a Pandas DataFrame.
In [3]:
Copied!
in_csv = "https://raw.githubusercontent.com/opengeos/data/main/world/world_cities.csv"
in_csv = "https://raw.githubusercontent.com/opengeos/data/main/world/world_cities.csv"
In [4]:
Copied!
df = leafmap.csv_to_df(in_csv)
df
df = leafmap.csv_to_df(in_csv)
df
Out[4]:
id | name | country | latitude | longitude | population | |
---|---|---|---|---|---|---|
0 | 1 | Bombo | UGA | 0.58330 | 32.53330 | 75000 |
1 | 2 | Fort Portal | UGA | 0.67100 | 30.27500 | 42670 |
2 | 3 | Potenza | ITA | 40.64200 | 15.79900 | 69060 |
3 | 4 | Campobasso | ITA | 41.56300 | 14.65600 | 50762 |
4 | 5 | Aosta | ITA | 45.73700 | 7.31500 | 34062 |
... | ... | ... | ... | ... | ... | ... |
1244 | 1245 | Rio de Janeiro | BRA | -22.92502 | -43.22502 | 11748000 |
1245 | 1246 | Sao Paulo | BRA | -23.55868 | -46.62502 | 18845000 |
1246 | 1247 | Sydney | AUS | -33.92001 | 151.18518 | 4630000 |
1247 | 1248 | Singapore | SGP | 1.29303 | 103.85582 | 5183700 |
1248 | 1249 | Hong Kong | CHN | 22.30498 | 114.18501 | 7206000 |
1249 rows × 6 columns
Create a point layer from a CSV file containing lat/long information.
In [5]:
Copied!
Map = leafmap.Map()
Map.add_xy_data(in_csv, x="longitude", y="latitude", layer_name="World Cities")
Map
Map = leafmap.Map()
Map.add_xy_data(in_csv, x="longitude", y="latitude", layer_name="World Cities")
Map
Set the output directory.
In [6]:
Copied!
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")
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.
In [7]:
Copied!
leafmap.csv_to_shp(in_csv, out_shp)
leafmap.csv_to_shp(in_csv, out_shp)
In [8]:
Copied!
out_geojson = os.path.join(out_dir, "world_cities.geojson")
leafmap.csv_to_geojson(in_csv, out_geojson)
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.
In [9]:
Copied!
# gdf = leafmap.csv_to_gdf(in_csv)
# gdf
# gdf = leafmap.csv_to_gdf(in_csv)
# gdf