In [1]:
Copied!
# Uncomment to install
#!pip install leafmap spatialcsv geopandas
import leafmap
import spatialcsv
import requests
import geopandas
# This function will allow us to preview the csv before processing
def preview(url):
response = requests.get(url)
for line in response.text.splitlines()[:4]:
print(line)
# Uncomment to install
#!pip install leafmap spatialcsv geopandas
import leafmap
import spatialcsv
import requests
import geopandas
# This function will allow us to preview the csv before processing
def preview(url):
response = requests.get(url)
for line in response.text.splitlines()[:4]:
print(line)
This package takes csv files and standardizes them for web mapping. It will automatically change coordidantes from degree,minute format to decimal.
If using streamlit, it will change the column headers to what streamlit uses, and remove any empty coordinate fields (which causes streamlit to error out). To view in streamlit, go to https://spatialcsv.streamlit.app/
If using leafmap, you can add the following arguments:
- tags = [ ] Will select what columns are included in the pin popup
- epsg = '####' Will reproject from given crs to lat/long coordinates
If you don't want to use a web map, but only want to convert all the coordinates, there is also a function updated_csv() that will export it back to an csv file.
Basic Example¶
In [2]:
Copied!
m1 = leafmap.Map()
fp1 = 'https://github.com/TJHomer/spatialcsv/raw/main/spatialcsv/example_files/test.csv'
preview(fp1)
m1 = leafmap.Map()
fp1 = 'https://github.com/TJHomer/spatialcsv/raw/main/spatialcsv/example_files/test.csv'
preview(fp1)
City,State,lat,long Arizona,Phoenix,33.448143,-112.096962 Arkansas,Little Rock,34.746613,-92.288986 California,Sacramento,38.576668,-121.493629
In [3]:
Copied!
points1 = spatialcsv.Points(fp1, ['lat', 'long'])
points1.df.head()
points1 = spatialcsv.Points(fp1, ['lat', 'long'])
points1.df.head()
Out[3]:
| City | State | lat | long | |
|---|---|---|---|---|
| 0 | Arizona | Phoenix | 33.448143 | -112.096962 |
| 1 | Arkansas | Little Rock | 34.746613 | -92.288986 |
| 2 | California | Sacramento | 38.576668 | -121.493629 |
| 3 | Colorado | Denver | 39.739227 | -104.984856 |
| 4 | Connecticut | Hartford<br> | 41.764046 | -72.682198 |
In [4]:
Copied!
data1 = points1.to_leafmap()
m1.add_points_from_xy(data1, x='long', y='lat')
m1
data1 = points1.to_leafmap()
m1.add_points_from_xy(data1, x='long', y='lat')
m1
Example changing from degree to decimal format¶
In [5]:
Copied!
m2 = leafmap.Map()
fp2 = 'https://github.com/TJHomer/spatialcsv/raw/main/spatialcsv/example_files/dms.csv'
preview(fp2)
m2 = leafmap.Map()
fp2 = 'https://github.com/TJHomer/spatialcsv/raw/main/spatialcsv/example_files/dms.csv'
preview(fp2)
State Capital,lat,long Tallahassee,30°27'N, 84°16'W Raleigh,35°46'N, 78°39'W Austin,30°14'N, 97°45'W
In [6]:
Copied!
points2 = spatialcsv.Points(fp2, ['lat', 'long'])
points2.df.head()
points2 = spatialcsv.Points(fp2, ['lat', 'long'])
points2.df.head()
Out[6]:
| State Capital | lat | long | |
|---|---|---|---|
| 0 | Tallahassee | 30.450000 | -84.266667 |
| 1 | Raleigh | 35.766667 | -78.650000 |
| 2 | Austin | 30.233333 | -97.750000 |
| 3 | Sacramento | 38.900000 | -121.483333 |
| 4 | Richmond | 37.533333 | -77.450000 |
In [7]:
Copied!
data2 = points2.to_leafmap()
m2.add_points_from_xy(data2, x='long', y='lat')
m2
data2 = points2.to_leafmap()
m2.add_points_from_xy(data2, x='long', y='lat')
m2
Example changing projection¶
In [8]:
Copied!
m3 = leafmap.Map()
fp3 = 'https://github.com/TJHomer/spatialcsv/raw/main/spatialcsv/example_files/projected_2274.csv'
preview(fp3)
m3 = leafmap.Map()
fp3 = 'https://github.com/TJHomer/spatialcsv/raw/main/spatialcsv/example_files/projected_2274.csv'
preview(fp3)
ObjectID,X,Y,School,City,GradeLevels 1,767637.23167577,315674.50617952,1ST CLASS MONTESSORI - MEMPHIS,Memphis,"PK,K" 2,828609.47361461,327549.00838232,1ST CLASS MONTESSORI SCHOOL - CORDOVA,Cordova,"PK,K,1,2,3" 3,757231.10957349,307890.57621781,A B HILL ELEMENTARY,Memphis,"PK,K,1,2,3,4,5"
In [9]:
Copied!
points3 = spatialcsv.Points(fp3, ['X', 'Y'], epsg='2274')
points3.df.head()
points3 = spatialcsv.Points(fp3, ['X', 'Y'], epsg='2274')
points3.df.head()
Out[9]:
| ObjectID | X | Y | School | City | GradeLevels | |
|---|---|---|---|---|---|---|
| 0 | 1 | 35.132828 | -90.017079 | 1ST CLASS MONTESSORI - MEMPHIS | Memphis | PK,K |
| 1 | 2 | 35.172125 | -89.814876 | 1ST CLASS MONTESSORI SCHOOL - CORDOVA | Cordova | PK,K,1,2,3 |
| 2 | 3 | 35.110283 | -90.050772 | A B HILL ELEMENTARY | Memphis | PK,K,1,2,3,4,5 |
| 3 | 4 | 36.387252 | -85.319641 | A H ROBERTS ELEMENTARY | Livingston | PK,K,1,2,3,4 |
| 4 | 5 | 35.882628 | -84.083299 | A L LOTTS ELEMENTARY | Knoxville | K,1,2,3,4,5 |
In [10]:
Copied!
data3 = points3.to_leafmap()
m3.add_points_from_xy(data3, x='Y', y='X')
m3
data3 = points3.to_leafmap()
m3.add_points_from_xy(data3, x='Y', y='X')
m3
Example assigning tags¶
In [11]:
Copied!
m4 = leafmap.Map()
fp4 = 'https://github.com/TJHomer/spatialcsv/raw/main/spatialcsv/example_files/tags.csv'
preview(fp4)
m4 = leafmap.Map()
fp4 = 'https://github.com/TJHomer/spatialcsv/raw/main/spatialcsv/example_files/tags.csv'
preview(fp4)
name,sov_a3,latitude,longitude,pop_max,region San Bernardino,USA,34.12038,-117.30003,1745000,West Bridgeport,USA,41.17998,-73.19996,1018000,Northeast Rochester,USA,43.17043,-77.61995,755000,Northeast
In [12]:
Copied!
points4 = spatialcsv.Points(fp4, ['latitude', 'longitude'], tags=['name', 'pop_max'])
points4.df.head()
points4 = spatialcsv.Points(fp4, ['latitude', 'longitude'], tags=['name', 'pop_max'])
points4.df.head()
Out[12]:
| name | sov_a3 | latitude | longitude | pop_max | region | |
|---|---|---|---|---|---|---|
| 0 | San Bernardino | USA | 34.12038 | -117.30003 | 1745000 | West |
| 1 | Bridgeport | USA | 41.17998 | -73.19996 | 1018000 | Northeast |
| 2 | Rochester | USA | 43.17043 | -77.61995 | 755000 | Northeast |
| 3 | St. Paul | USA | 44.94399 | -93.08497 | 734854 | Midwest |
| 4 | Billings | USA | 45.78830 | -108.54000 | 104552 | West |
In [13]:
Copied!
data4 = points4.to_leafmap()
m4.add_points_from_xy(data4, x='longitude', y='latitude')
m4
# Click on the location icon to see selected tags
data4 = points4.to_leafmap()
m4.add_points_from_xy(data4, x='longitude', y='latitude')
m4
# Click on the location icon to see selected tags
Downloading updated csv¶
In [14]:
Copied!
#enter a filepath and uncomment to download one of the updated csvs
filepath = 'updated.csv'
points2.updated_csv(filepath)
#enter a filepath and uncomment to download one of the updated csvs
filepath = 'updated.csv'
points2.updated_csv(filepath)
In [ ]:
Copied!
Last update:
2023-05-08