Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Inline exercises for notebooks 1, 2, 3, and 4 #15

Open
wants to merge 5 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
120 changes: 119 additions & 1 deletion 01-introduction-geospatial-data.ipynb
Original file line number Diff line number Diff line change
Expand Up @@ -182,6 +182,40 @@
"africa.plot()"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"---\n",
"\n",
"**Exercise**: create a plot of South America\n"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"<!--\n",
"countries[countries['continent'] == 'South America'].plot()\n",
"-->"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"---"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"countries.head()"
]
},
{
"cell_type": "markdown",
"metadata": {},
Expand Down Expand Up @@ -486,6 +520,31 @@
"Note the different scale of x and y."
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"---\n",
"\n",
"**Exercise**: project the countries to [Web Mercator](http://epsg.io/3857), the CRS used by Google Maps, OpenStreetMap and most web providers.\n"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"<!--\n",
"countries.to_crs(epsg=3857)\n",
"-->"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"---"
]
},
{
"cell_type": "markdown",
"metadata": {},
Expand All @@ -512,6 +571,34 @@
"See the [04-more-on-visualization.ipynb](04-more-on-visualization.ipynb) notebook for more details on visualizing geospatial datasets."
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"---\n",
"\n",
"**Exercise**: replicate the figure above by coloring the countries in black and cities in yellow\n"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"<!--\n",
"ax = countries.plot(edgecolor='w', facecolor='k', figsize=(15, 10))\n",
"rivers.plot(ax=ax)\n",
"cities.plot(ax=ax, color='yellow')\n",
"ax.set(xlim=(-20, 60), ylim=(-40, 40))\n",
"-->"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"---"
]
},
{
"cell_type": "markdown",
"metadata": {},
Expand Down Expand Up @@ -629,6 +716,37 @@
"source": [
"See http://geopandas.readthedocs.io/en/latest/gallery/create_geopandas_from_pandas.html#sphx-glr-gallery-create-geopandas-from-pandas-py for full example"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"---\n",
"\n",
"**Exercise**: use [geojson.io](http://geojson.io) to mark five points, and create a `GeoDataFrame` with it. Note that coordinates will be expressed in longitude and latitude, so you'll have to set the CRS accordingly.\n"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"<!--\n",
"df = pd.DataFrame(\n",
" {'Name': ['Hotel', 'Capitol', 'Barton Springs'],\n",
" 'Latitude': [30.28195889019179, 30.274782936992608, 30.263728440902543],\n",
" 'Longitude': [-97.74006128311157, -97.74038314819336, -97.77013421058655]})\n",
"df['Coordinates'] = list(zip(df.Longitude, df.Latitude))\n",
"df['Coordinates'] = df['Coordinates'].apply(Point)\n",
"gdf = geopandas.GeoDataFrame(df, geometry='Coordinates', crs={'init': 'epsg:4326'})\n",
"-->"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"---"
]
}
],
"metadata": {
Expand All @@ -647,7 +765,7 @@
"name": "python",
"nbconvert_exporter": "python",
"pygments_lexer": "ipython3",
"version": "3.5.5"
"version": "3.6.5"
}
},
"nbformat": 4,
Expand Down
111 changes: 108 additions & 3 deletions 02-spatial-relationships-operations.ipynb
Original file line number Diff line number Diff line change
Expand Up @@ -125,8 +125,41 @@
"cell_type": "markdown",
"metadata": {},
"source": [
"You can recognize the abstract shape of Belgium.\n",
"You can recognize the abstract shape of Belgium."
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"---\n",
"\n",
"**Exercise**: produce a similar figure as above that includes France, Spain, Paris, Madrid, and a line that connects both capitals"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"<!--\n",
"sp = countries.loc[countries['name'] == 'Spain', 'geometry'].squeeze()\n",
"mad = cities.loc[cities['name'] == 'Madrid', 'geometry'].squeeze()\n",
"fr = countries.loc[countries['name'] == 'France', 'geometry'].squeeze()\n",
"geopandas.GeoSeries([sp, fr, mad, paris, LineString([paris, mad])]).plot(cmap='tab10')\n",
"-->"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"---"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"Brussels, the capital of Belgium, is thus located within Belgium. This is a spatial relationship, and we can test this using the individual shapely geometry objects as follow:"
]
},
Expand Down Expand Up @@ -302,6 +335,32 @@
"</div>"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"---\n",
"\n",
"**Exercise**: find all of the countries contiguous to Brazil"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"<!--\n",
"br = countries.query('name == \"Brazil\"')['geometry'].squeeze()\n",
"countries[countries.touches(br)] # or .intersects\n",
"-->"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"---"
]
},
{
"cell_type": "markdown",
"metadata": {},
Expand All @@ -326,7 +385,7 @@
"metadata": {},
"outputs": [],
"source": [
"geopandas.GeoSeries([belgium, brussels.buffer(1)]).plot(alpha=0.5, cmap='tab10')"
"geopandas.GeoSeries([belgium, brussels.buffer(1), brussels]).plot(alpha=0.5, cmap='tab10')"
]
},
{
Expand Down Expand Up @@ -399,6 +458,52 @@
"africa"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"---\n",
"\n",
"**Exercise**: Create a single polygon for Europe"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"<!--\n",
"eu = countries.query('continent == \"Africa\"')\n",
"eu.unary_union\n",
"-->"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"\n",
"**Exercise**: Create a single polygon with all of the countries that you would have to cross to travel directly (as the crow flies) from Beijing to Sydney"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"<!--\n",
"bei = cities.query('name == \"Beijing\"')['geometry'].squeeze()\n",
"syd = cities.query('name == \"Sydney\"')['geometry'].squeeze()\n",
"\n",
"countries[countries.crosses(LineString([bei, syd]))].unary_union\n",
"-->"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"---"
]
},
{
"cell_type": "code",
"execution_count": null,
Expand Down Expand Up @@ -445,7 +550,7 @@
"name": "python",
"nbconvert_exporter": "python",
"pygments_lexer": "ipython3",
"version": "3.5.5"
"version": "3.6.5"
}
},
"nbformat": 4,
Expand Down