OSGeoLive-Notebooks/Geopandas/gpd-geodataframes.ipynb

154 lines
3.2 KiB
Plaintext

{
"cells": [
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"from __future__ import absolute_import\n",
"\n",
"import json\n",
"import os\n",
"import tempfile\n",
"import shutil\n",
"\n",
"import numpy as np\n",
"import pandas as pd\n",
"from shapely.geometry import Point, Polygon\n",
"import fiona\n",
"\n",
"import geopandas\n",
"from geopandas import GeoDataFrame, read_file, GeoSeries\n",
"#from geopandas.geodataframe import points_from_xy\n"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"# NEW GeoDataFrame via read_file()\n",
"\n",
"tempdir = tempfile.mkdtemp()\n",
"\n",
"nybb_filename = geopandas.datasets.get_path('nybb')\n",
"df_nybb = read_file(nybb_filename)\n",
"df_nybb\n"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"# NEW GeoDataFrame via list\n",
"\n",
"nybb_filename = geopandas.datasets.get_path('nybb')\n",
"with fiona.open(nybb_filename) as f:\n",
" features = list(f)\n",
" crs = f.crs\n",
"\n",
"df = GeoDataFrame.from_features(features, crs=crs)\n",
"df"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"# NEW GeoDataFrame via dict\n",
"\n",
"crs = {'init': 'epsg:4326'}\n",
"N = 10\n",
"\n",
"df2_synthetic = GeoDataFrame([\n",
" {'geometry': Point(x, y), 'value1': x + y, 'value2': x * y}\n",
" for x, y in zip(range(N), range(N))], crs=crs)\n",
"df2_synthetic\n"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"df2_synthetic.info()"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"df2_synthetic.to_json()"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"## Filter to subset\n",
"## [ EXPR ] returns a Dataframe of Boolean\n",
"## df[ EXPR ] returns the subset as a new Dataframe\n",
"\n",
"df_nybb_bs= df_nybb[ df_nybb['BoroName'].str.contains('B') ]\n",
"df_nybb_bs"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"## show all defined variables\n",
"%whos"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": []
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": []
}
],
"metadata": {
"kernelspec": {
"display_name": "Python 3",
"language": "python",
"name": "python3"
},
"language_info": {
"codemirror_mode": {
"name": "ipython",
"version": 3
},
"file_extension": ".py",
"mimetype": "text/x-python",
"name": "python",
"nbconvert_exporter": "python",
"pygments_lexer": "ipython3",
"version": "3.8.5"
}
},
"nbformat": 4,
"nbformat_minor": 2
}