Skip to content

Instantly share code, notes, and snippets.

@linwoodc3
Last active October 1, 2017 15:11
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save linwoodc3/0e9e2b173ec47c677a28bb3664431ebf to your computer and use it in GitHub Desktop.
Save linwoodc3/0e9e2b173ec47c677a28bb3664431ebf to your computer and use it in GitHub Desktop.
Python code to create geodataframe for countries, lakes, and oceans for geospatial analysis in Python. Use geopandas and Python 2 or download the
#Author: Linwood Creekmore
#Date: October 1, 2017
# Instructions:
"""
Download and use my shapefilereader function in Python 2 OR just download the linked zip to your computer and read using geopandas
gist to shapefilereader - > https://gist.github.com/linwoodc3/72b2f24b6d2ff6ffde1597f1ca2dea3f
"""
from shapefilereader import shapefilereader # get shapefilereader from link above and make sure you're using Python 2
import pandas as pd
# continents
gdf = shapefilereader('http://thematicmapping.org/downloads/TM_WORLD_BORDERS-0.3.zip').to_crs({'init':'epsg:3857'})
# primary lakes shapefile
lakes = shapefilereader('http://www.naturalearthdata.com/http//www.naturalearthdata.com/download/10m/physical/ne_10m_lakes.zip')
lakes = lakes.fillna(np.nan)\
.assign(name=np.where(lakes['name'].isnull(),lakes['note'],lakes['name']))
# north american lakes supplement
lakes2 = shapefilereader('http://www.naturalearthdata.com/http//www.naturalearthdata.com/download/10m/physical/ne_10m_lakes_north_america.zip')
lakes2 = lakes2.fillna(np.nan)\
.assign(name=np.where(lakes2['name'].isnull(),lakes2['note'],lakes2['name']))
# european lakes supplement
lakes3 = shapefilereader('http://www.naturalearthdata.com/http//www.naturalearthdata.com/download/10m/physical/ne_10m_lakes_europe.zip')
lakes3 = lakes3.fillna(np.nan)\
.assign(name=np.where(lakes3['name'].isnull(),lakes3['note'],lakes3['name']))
# combining all lakes into one single lake file, drop duplicates using supplement data over original
lakes = pd.concat([pd.concat([lakes,lakes2]).drop_duplicates('name',keep='last'),lakes3]).\
drop_duplicates(subset=['name','featurecla'],keep='last')[['name','geometry','featurecla']]
# oceans and seas shapefile
oceans = shapefilereader('/Users/linwood/projects/Blogs/drafts/geolocated_social_transcends_political_barriers/data/World_Seas_IHO_v2.zip')
oceans = oceans.assign(featurecla = 'Ocean')[['NAME','geometry','featurecla']]\
.rename(columns={'NAME':'name'})
# creating one single bodies of water file
bodiesOfWater = pd.concat([oceans,lakes])
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment