Skip to content
Projects
Groups
Snippets
Help
Loading...
Help
Submit feedback
Contribute to GitLab
Sign in
Toggle navigation
espaciopublico
Project
Project
Details
Activity
Releases
Cycle Analytics
Repository
Repository
Files
Commits
Branches
Tags
Contributors
Graph
Compare
Charts
Issues
0
Issues
0
List
Board
Labels
Milestones
Merge Requests
0
Merge Requests
0
CI / CD
CI / CD
Pipelines
Jobs
Schedules
Charts
Wiki
Wiki
Snippets
Snippets
Members
Members
Collapse sidebar
Close sidebar
Activity
Graph
Charts
Create a new issue
Jobs
Commits
Issue Boards
Open sidebar
Mario Chirinos Colunga
espaciopublico
Commits
d247ed4e
Commit
d247ed4e
authored
Apr 09, 2024
by
Mario Chirinos
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
jupyter
parent
c39daaf6
Changes
1
Hide whitespace changes
Inline
Side-by-side
Showing
1 changed file
with
607 additions
and
0 deletions
+607
-0
espaciopublico.ipynb
jupyter/espaciopublico.ipynb
+607
-0
No files found.
jupyter/espaciopublico.ipynb
0 → 100644
View file @
d247ed4e
{
"cells": [
{
"cell_type": "markdown",
"id": "6ce9e21c-7de8-41ca-a756-09f7bd6b52a7",
"metadata": {},
"source": [
"# Espacio Público en México\n"
]
},
{
"cell_type": "code",
"execution_count": 1,
"id": "74a07b53-efbd-4e8a-aa43-03b99bca1346",
"metadata": {},
"outputs": [],
"source": [
"from OSMPythonTools.overpass import Overpass\n",
"import osm2geojson\n",
"import pandas as pd\n",
"import geopandas as gpd\n",
"import matplotlib.pyplot as plt\n",
"import geocoder\n",
"import os\n",
"os.makedirs('osm/', exist_ok=True) "
]
},
{
"cell_type": "markdown",
"id": "d1235a5b-baed-4e13-aa26-f73a3e7fe6e2",
"metadata": {},
"source": [
"## Datos del Marco Geoestadistico Nacional (INEGI)."
]
},
{
"cell_type": "code",
"execution_count": 2,
"id": "a98d5320-863e-4a81-99c8-1aabb475cb23",
"metadata": {},
"outputs": [],
"source": [
"mun = gpd.read_file(\"/home/mchc/centroGeo/SHP/marco_geoestadistico/889463776079_s/mg_sep2019_integrado/conjunto_de_datos/00mun.shp\", crs=6372)\n",
"ent = gpd.read_file(\"/home/mchc/centroGeo/SHP/marco_geoestadistico/889463776079_s/mg_sep2019_integrado/conjunto_de_datos/00ent.shp\", crs=6372)\n",
"ent = ent[[\"CVE_ENT\", \"NOMGEO\"]].set_index('CVE_ENT')"
]
},
{
"cell_type": "code",
"execution_count": 3,
"id": "886228c4-c23b-4efd-aa48-60f9e54a1c99",
"metadata": {},
"outputs": [],
"source": [
"mun_df = mun.join(ent, on=\"CVE_ENT\", lsuffix='_mun', rsuffix='_ent')"
]
},
{
"cell_type": "code",
"execution_count": 4,
"id": "116a2fb7-bd1d-465b-b95a-0a3b7437a825",
"metadata": {},
"outputs": [],
"source": [
"ageb = gpd.read_file(\"/home/mchc/centroGeo/SHP/marco_geoestadistico/889463776079_s/mg_sep2019_integrado/conjunto_de_datos/00a.shp\", crs=6372)"
]
},
{
"cell_type": "code",
"execution_count": 5,
"id": "084ec274-69f5-43ad-b3bf-cd91325745fd",
"metadata": {},
"outputs": [],
"source": [
"def getMunicipality(mun_id):\n",
" '''\n",
" Datos municiplaes del Marco Geoestadistico Nacional\n",
" ''' \n",
" municipality = mun_df[(mun_df.CVEGEO==mun_id)]\n",
" municipality_6372= mun_df[mun_df.CVEGEO==mun_id].iloc[0][\"geometry\"]\n",
" municipality_4326= mun_df[mun_df.CVEGEO==mun_id].to_crs(4326).iloc[0][\"geometry\"]\n",
" return municipality_6372, municipality_4326"
]
},
{
"cell_type": "markdown",
"id": "9d7b3103-a552-4129-b937-78ade49b5cb4",
"metadata": {},
"source": [
"## Mascara Urbana"
]
},
{
"cell_type": "code",
"execution_count": 7,
"id": "068076d3-d492-4d2c-97ee-21af49a146b9",
"metadata": {},
"outputs": [],
"source": [
"def getUrbanMask(mun_id):\n",
" '''\n",
" Mascara Urbana = Municipio ∩ Agebs Urbanas\n",
" ''' \n",
" \n",
" municipality = mun_df[(mun_df.CVEGEO==mun_id)]\n",
" ent_id=municipality.iloc[0].CVE_ENT\n",
"\n",
" urban_mask_6372 = ageb[(ageb.Ambito==\"Urbana\") & (ageb.CVE_ENT==ent_id)].unary_union\n",
" urban_mun_mask_6372=urban_mask_6372.intersection(municipality_6372)\n",
"\n",
" urban_mask_4326 = ageb[(ageb.Ambito==\"Urbana\") & (ageb.CVE_ENT==ent_id)].to_crs(4326).unary_union\n",
" urban_mun_mask_4326 = urban_mask_4326.intersection(municipality_4326)\n",
" return urban_mun_mask_6372, urban_mun_mask_4326"
]
},
{
"cell_type": "markdown",
"id": "171f83cb-38c7-4df9-8ed1-df0f4e8ca833",
"metadata": {},
"source": [
"## Datos de Open Street Maps (OSM)"
]
},
{
"cell_type": "code",
"execution_count": 8,
"id": "df48320b-db2e-4545-924b-3c0288f94c2e",
"metadata": {},
"outputs": [],
"source": [
"def getAreaId(inegi): \n",
" overpass = Overpass()\n",
" query = f'''\n",
" area[\"admin_level\"=\"2\"][\"name\"=\"México\"]->.country;\n",
" (\n",
" relation(area.country)[\"boundary\"=\"administrative\"][\"admin_level\"=\"6\"][\"INEGI:MUNID\"=\"{inegi}\"];\n",
" );\n",
" out geom;\n",
" ''' \n",
" #print(query)\n",
" query = query.replace(\"\\n\",\"\").replace(\" \",\"\")\n",
" results = overpass.query(query)\n",
" if len(osm2geojson.json2geojson(results.toJSON())[\"features\"]) == 0:\n",
" return None\n",
" return osm2geojson.json2geojson(results.toJSON())[\"features\"][0][\"properties\"][\"id\"]"
]
},
{
"cell_type": "code",
"execution_count": 9,
"id": "ee4293b8-6abd-4bd5-865e-8bbc13d67c3a",
"metadata": {},
"outputs": [],
"source": [
"def getOSMdata(mun_id):\n",
" '''\n",
" Obtener espacio publico utilizand OSM Overpass API\n",
" '''\n",
" municipality = mun_df[(mun_df.CVEGEO==mun_id)].iloc[0]\n",
" print(\"mun_id\", mun_id)\n",
" municipality_name = municipality.NOMGEO_mun+\", \"+ municipality.NOMGEO_ent + \", México\"\n",
" print(\"municipality_name\", municipality_name)\n",
" \n",
" # geocodeosm = geocoder.osm(municipality_name)\n",
" # if geocodeosm.status != 'OK':\n",
" # print(\"Geocode error\")\n",
" # return None\n",
" areaId = getAreaId(mun_id)\n",
"\n",
" if areaId is None:\n",
" print(municipality_name, \"Name Not Found\") \n",
" return\n",
"\n",
" areaId = areaId + 3600000000\n",
" print(areaId)\n",
" query = f\"\"\"\n",
" area({areaId}) -> .area_0;\n",
" (\n",
" node[\"landuse\"=\"grass\"](area.area_0);\n",
" node[\"natural\"](area.area_0);\n",
" node[\"leisure\"](area.area_0);\n",
" node[\"landuse\"=\"recreation_ground\"](area.area_0);\n",
" node[\"landuse\"=\"forest\"](area.area_0);\n",
" node[\"landuse\"=\"plant_nursery\"](area.area_0);\n",
" way[\"landuse\"=\"grass\"](area.area_0);\n",
" way[\"natural\"](area.area_0);\n",
" way[\"leisure\"](area.area_0);\n",
" way[\"landuse\"=\"recreation_ground\"](area.area_0);\n",
" way[\"landuse\"=\"forest\"](area.area_0);\n",
" way[\"landuse\"=\"plant_nursery\"](area.area_0);\n",
" relation[\"landuse\"=\"grass\"](area.area_0);\n",
" relation[\"natural\"](area.area_0);\n",
" relation[\"leisure\"](area.area_0);\n",
" relation[\"landuse\"=\"recreation_ground\"](area.area_0);\n",
" relation[\"landuse\"=\"forest\"](area.area_0);\n",
" relation[\"landuse\"=\"plant_nursery\"](area.area_0);\n",
" );\n",
" (._;>;);\n",
" out body;\n",
" \"\"\"\n",
" \n",
" query = query.replace(\"\\n\",\"\").replace(\" \",\"\")\n",
" overpass = Overpass()\n",
" results = overpass.query(query, timeout=2550)\n",
" if len(results.toJSON()[\"elements\"]) == 0:\n",
" print(\"No Overpass results for area id\", areaId )\n",
" return None\n",
" osm_df = gpd.GeoDataFrame.from_features(osm2geojson.json2geojson(results.toJSON())[\"features\"] )\n",
" return osm_df[(osm_df.type=='Polygon')|(osm_df.type=='Multipolygon')]"
]
},
{
"cell_type": "markdown",
"id": "5d066147-500f-4552-a587-a686b2c12855",
"metadata": {},
"source": [
"# Obtener datos de OSM para cada municipio"
]
},
{
"cell_type": "code",
"execution_count": 6,
"id": "3f71d3e0-eaa0-4cfa-be12-1d252451ffc2",
"metadata": {},
"outputs": [
{
"ename": "NameError",
"evalue": "name 'getUrbanMask' is not defined",
"output_type": "error",
"traceback": [
"\u001b[0;31m---------------------------------------------------------------------------\u001b[0m",
"\u001b[0;31mNameError\u001b[0m Traceback (most recent call last)",
"Cell \u001b[0;32mIn [6], line 4\u001b[0m\n\u001b[1;32m 2\u001b[0m mun_id \u001b[38;5;241m=\u001b[39m row\u001b[38;5;241m.\u001b[39mCVEGEO\n\u001b[1;32m 3\u001b[0m municipality_6372, municipality_4326 \u001b[38;5;241m=\u001b[39m getMunicipality(mun_id)\n\u001b[0;32m----> 4\u001b[0m urban_mun_mask_6372, urban_mun_mask_4326 \u001b[38;5;241m=\u001b[39m \u001b[43mgetUrbanMask\u001b[49m(mun_id)\n\u001b[1;32m 5\u001b[0m path \u001b[38;5;241m=\u001b[39m \u001b[38;5;124m'\u001b[39m\u001b[38;5;124mosm/\u001b[39m\u001b[38;5;124m'\u001b[39m\u001b[38;5;241m+\u001b[39mmun_id\u001b[38;5;241m+\u001b[39m\u001b[38;5;124m'\u001b[39m\u001b[38;5;124m.csv\u001b[39m\u001b[38;5;124m'\u001b[39m\n\u001b[1;32m 6\u001b[0m \u001b[38;5;28;01mif\u001b[39;00m \u001b[38;5;129;01mnot\u001b[39;00m os\u001b[38;5;241m.\u001b[39mpath\u001b[38;5;241m.\u001b[39misfile(path):\n",
"\u001b[0;31mNameError\u001b[0m: name 'getUrbanMask' is not defined"
]
}
],
"source": [
"for index, row in mun_df.iterrows():\n",
" mun_id = row.CVEGEO\n",
" municipality_6372, municipality_4326 = getMunicipality(mun_id)\n",
" urban_mun_mask_6372, urban_mun_mask_4326 = getUrbanMask(mun_id)\n",
" path = 'osm/'+mun_id+'.csv'\n",
" if not os.path.isfile(path):\n",
" print(\"=======================================\")\n",
" print(mun_id)\n",
" osm_df = getOSMdata(mun_id)\n",
" if(osm_df is not None):\n",
" osm_df.to_csv(path)\n",
" else:\n",
" print(mun_id, \"Not Found\")\n"
]
},
{
"cell_type": "markdown",
"id": "893a7b84-60e4-48a1-a99a-21b5ca248213",
"metadata": {},
"source": [
"# Recuperar Datos Guardados"
]
},
{
"cell_type": "code",
"execution_count": 11,
"id": "9978eb95-f7d1-44d6-9bfc-16b208bfae8b",
"metadata": {},
"outputs": [
{
"data": {
"text/html": [
"<div>\n",
"<style scoped>\n",
" .dataframe tbody tr th:only-of-type {\n",
" vertical-align: middle;\n",
" }\n",
"\n",
" .dataframe tbody tr th {\n",
" vertical-align: top;\n",
" }\n",
"\n",
" .dataframe thead th {\n",
" text-align: right;\n",
" }\n",
"</style>\n",
"<table border=\"1\" class=\"dataframe\">\n",
" <thead>\n",
" <tr style=\"text-align: right;\">\n",
" <th></th>\n",
" <th>geometry</th>\n",
" <th>type</th>\n",
" <th>id</th>\n",
" <th>tags</th>\n",
" <th>nodes</th>\n",
" <th>CVEGEO</th>\n",
" </tr>\n",
" </thead>\n",
" <tbody>\n",
" <tr>\n",
" <th>0</th>\n",
" <td>POLYGON ((-102.30251 21.88015, -102.30333 21.8...</td>\n",
" <td>way</td>\n",
" <td>34348266</td>\n",
" <td>{'addr:city': 'Aguascalientes', 'addr:postcode...</td>\n",
" <td>[5517257117, 7810476857, 5517257113, 782437916...</td>\n",
" <td>01001</td>\n",
" </tr>\n",
" <tr>\n",
" <th>1</th>\n",
" <td>POLYGON ((-102.28176 21.85074, -102.28263 21.8...</td>\n",
" <td>way</td>\n",
" <td>34386972</td>\n",
" <td>{'leisure': 'park', 'name': 'CEAR Rodolfo Land...</td>\n",
" <td>[394805164, 394805166, 394805169, 394805170, 3...</td>\n",
" <td>01001</td>\n",
" </tr>\n",
" <tr>\n",
" <th>2</th>\n",
" <td>POLYGON ((-102.31688 21.87859, -102.31558 21.8...</td>\n",
" <td>way</td>\n",
" <td>76217017</td>\n",
" <td>{'leisure': 'park', 'name': 'Parque Benito Jua...</td>\n",
" <td>[899212721, 899212761, 899212788, 8231463086, ...</td>\n",
" <td>01001</td>\n",
" </tr>\n",
" <tr>\n",
" <th>3</th>\n",
" <td>POLYGON ((-102.25898 21.86662, -102.25903 21.8...</td>\n",
" <td>way</td>\n",
" <td>81134041</td>\n",
" <td>{'name': 'El Cedazo', 'natural': 'water', 'wat...</td>\n",
" <td>[945482837, 945482823, 945482844, 945482826, 9...</td>\n",
" <td>01001</td>\n",
" </tr>\n",
" <tr>\n",
" <th>4</th>\n",
" <td>POLYGON ((-102.26229 21.86913, -102.26234 21.8...</td>\n",
" <td>way</td>\n",
" <td>81134055</td>\n",
" <td>{'addr:city': 'Aguascalientes', 'addr:postcode...</td>\n",
" <td>[2294281269, 2294281307, 2294281303, 229428130...</td>\n",
" <td>01001</td>\n",
" </tr>\n",
" <tr>\n",
" <th>...</th>\n",
" <td>...</td>\n",
" <td>...</td>\n",
" <td>...</td>\n",
" <td>...</td>\n",
" <td>...</td>\n",
" <td>...</td>\n",
" </tr>\n",
" <tr>\n",
" <th>2</th>\n",
" <td>POLYGON ((-103.41770 21.52323, -103.41824 21.5...</td>\n",
" <td>way</td>\n",
" <td>919632589</td>\n",
" <td>{'natural': 'water', 'water': 'reservoir'}</td>\n",
" <td>[8540224274, 8540224275, 8540224276, 854022427...</td>\n",
" <td>32058</td>\n",
" </tr>\n",
" <tr>\n",
" <th>3</th>\n",
" <td>POLYGON ((-103.31491 21.51497, -103.31595 21.5...</td>\n",
" <td>way</td>\n",
" <td>925813870</td>\n",
" <td>{'natural': 'water', 'water': 'reservoir'}</td>\n",
" <td>[8591935113, 8591935114, 8591935115, 859193511...</td>\n",
" <td>32058</td>\n",
" </tr>\n",
" <tr>\n",
" <th>4</th>\n",
" <td>POLYGON ((-103.47849 21.49377, -103.47890 21.4...</td>\n",
" <td>way</td>\n",
" <td>1117282903</td>\n",
" <td>{'natural': 'wood'}</td>\n",
" <td>[10218928042, 10218928043, 10218928044, 102189...</td>\n",
" <td>32058</td>\n",
" </tr>\n",
" <tr>\n",
" <th>5</th>\n",
" <td>POLYGON ((-103.24405 21.43371, -103.24349 21.4...</td>\n",
" <td>way</td>\n",
" <td>1119324992</td>\n",
" <td>{'natural': 'wood'}</td>\n",
" <td>[10237456673, 10237456674, 10237456675, 102374...</td>\n",
" <td>32058</td>\n",
" </tr>\n",
" <tr>\n",
" <th>6</th>\n",
" <td>POLYGON ((-103.25937 21.43935, -103.25998 21.4...</td>\n",
" <td>way</td>\n",
" <td>1119326682</td>\n",
" <td>{'natural': 'wood'}</td>\n",
" <td>[10237486732, 10237497335, 10237497336, 102374...</td>\n",
" <td>32058</td>\n",
" </tr>\n",
" </tbody>\n",
"</table>\n",
"<p>184247 rows × 6 columns</p>\n",
"</div>"
],
"text/plain": [
" geometry type id \\\n",
"0 POLYGON ((-102.30251 21.88015, -102.30333 21.8... way 34348266 \n",
"1 POLYGON ((-102.28176 21.85074, -102.28263 21.8... way 34386972 \n",
"2 POLYGON ((-102.31688 21.87859, -102.31558 21.8... way 76217017 \n",
"3 POLYGON ((-102.25898 21.86662, -102.25903 21.8... way 81134041 \n",
"4 POLYGON ((-102.26229 21.86913, -102.26234 21.8... way 81134055 \n",
".. ... ... ... \n",
"2 POLYGON ((-103.41770 21.52323, -103.41824 21.5... way 919632589 \n",
"3 POLYGON ((-103.31491 21.51497, -103.31595 21.5... way 925813870 \n",
"4 POLYGON ((-103.47849 21.49377, -103.47890 21.4... way 1117282903 \n",
"5 POLYGON ((-103.24405 21.43371, -103.24349 21.4... way 1119324992 \n",
"6 POLYGON ((-103.25937 21.43935, -103.25998 21.4... way 1119326682 \n",
"\n",
" tags \\\n",
"0 {'addr:city': 'Aguascalientes', 'addr:postcode... \n",
"1 {'leisure': 'park', 'name': 'CEAR Rodolfo Land... \n",
"2 {'leisure': 'park', 'name': 'Parque Benito Jua... \n",
"3 {'name': 'El Cedazo', 'natural': 'water', 'wat... \n",
"4 {'addr:city': 'Aguascalientes', 'addr:postcode... \n",
".. ... \n",
"2 {'natural': 'water', 'water': 'reservoir'} \n",
"3 {'natural': 'water', 'water': 'reservoir'} \n",
"4 {'natural': 'wood'} \n",
"5 {'natural': 'wood'} \n",
"6 {'natural': 'wood'} \n",
"\n",
" nodes CVEGEO \n",
"0 [5517257117, 7810476857, 5517257113, 782437916... 01001 \n",
"1 [394805164, 394805166, 394805169, 394805170, 3... 01001 \n",
"2 [899212721, 899212761, 899212788, 8231463086, ... 01001 \n",
"3 [945482837, 945482823, 945482844, 945482826, 9... 01001 \n",
"4 [2294281269, 2294281307, 2294281303, 229428130... 01001 \n",
".. ... ... \n",
"2 [8540224274, 8540224275, 8540224276, 854022427... 32058 \n",
"3 [8591935113, 8591935114, 8591935115, 859193511... 32058 \n",
"4 [10218928042, 10218928043, 10218928044, 102189... 32058 \n",
"5 [10237456673, 10237456674, 10237456675, 102374... 32058 \n",
"6 [10237486732, 10237497335, 10237497336, 102374... 32058 \n",
"\n",
"[184247 rows x 6 columns]"
]
},
"execution_count": 11,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"import glob\n",
"from shapely import wkt\n",
"\n",
"osm_df=None\n",
"for f in sorted(glob.glob('osm/*.csv')):\n",
" munid = f[f.find(\"/\")+1: f.find(\".csv\")]\n",
" df = pd.read_csv(f)\n",
" df = df.drop(df.columns[0], axis=1)\n",
" geometry = df['geometry'].apply(wkt.loads)\n",
" df.drop([\"geometry\"], axis=1)\n",
" df['geometry'] = geometry\n",
" gdf = gpd.GeoDataFrame(df, crs=\"EPSG:4326\", geometry=geometry)\n",
" #print(gdf.dtypes)\n",
" gdf[\"CVEGEO\"]=munid\n",
" osm_df = gpd.GeoDataFrame(pd.concat([osm_df, gdf]))\n",
"osm_df"
]
},
{
"cell_type": "markdown",
"id": "42be463b-684b-4e12-89f3-d9178a541410",
"metadata": {},
"source": [
"## Espacio Público Urbano"
]
},
{
"cell_type": "code",
"execution_count": 12,
"id": "cd50d13a-0f42-4e25-a9ec-cbf45c6b42d2",
"metadata": {},
"outputs": [],
"source": [
"data = {\"CVGEO\":[], \"total_area\":[], \"urban_area\":[], \"publicspace_area\":[]}"
]
},
{
"cell_type": "code",
"execution_count": null,
"id": "b2e8808f-9840-4472-a710-8c83b4f584f8",
"metadata": {},
"outputs": [],
"source": [
"if not os.path.isfile(\"results.csv\"):\n",
" for index, row in mun_df.iterrows():\n",
" mun_id = row.CVEGEO\n",
" municipality_6372, municipality_4326 = getMunicipality(mun_id)\n",
" urban_mun_mask_6372, urban_mun_mask_4326 = getUrbanMask(mun_id)\n",
"\n",
" publicspace_df=osm_df[osm_df.CVEGEO==mun_id].copy()\n",
" if(len(publicspace_df)>0):\n",
" #print(mun_id)\n",
" publicspace_df[\"urban\"] = publicspace_df.geometry.intersection(urban_mun_mask_4326)\n",
" #print(publicspace_df[\"urban\"])\n",
" area=publicspace_df.urban.to_crs(6372).area/1000000\n",
" publicspace_df=publicspace_df.assign(area_m2 = area)\n",
" urbanarea = urban_mun_mask_6372.area/1000000\n",
" psak2 = publicspace_df.urban.to_crs(6372).unary_union.area/1000000\n",
" totalArea= municipality_6372.area/1000000\n",
" data[\"CVEGEO\"].append(mun_id)\n",
" data[\"total_area\"].append(totalArea)\n",
" data[\"urban_area\"].append(urbanarea)\n",
" data[\"publicspace_area\"].append(psak2)\n",
" results_df = pd.DataFrame(data=data)\n",
" fltered_df= results_df[results_df.publicspace_area>1].copy()#.sort_values(by=['publicspace_area'], ascending=False)\n",
" fltered_df[\"porcentajeEP\"] = fltered_df.publicspace_area/fltered_df.total_area\n",
" fltered_df.set_index('CVEGEO').join(mun_df.set_index('CVEGEO')).sort_values(by=['publicspace_area'], ascending=False).to_csv(\"results.csv\")\n",
"else:\n",
" fltered_df = pd.read_csv(\"results.csv\")"
]
},
{
"cell_type": "code",
"execution_count": null,
"id": "b8b0ed76-e6e4-4600-8236-18e22fd36a37",
"metadata": {},
"outputs": [],
"source": [
"publicspace_df=osm_df[osm_df.type!='Point'].set_crs(4326, allow_override=True)\n",
"publicspace_df.geometry = publicspace_df.geometry.intersection(urban_mun_mask_4326)\n",
"area=publicspace_df.geometry.to_crs(6372).area/1000000\n",
"publicspace_df=publicspace_df.assign(area_m2 = area)\n",
"publicspace_df.plot()"
]
},
{
"cell_type": "code",
"execution_count": null,
"id": "980c8c7f-18f4-490b-b600-2cdcfde1d441",
"metadata": {},
"outputs": [],
"source": [
"from matplotlib.ticker import ScalarFormatter\n",
"\n",
"fig, ax = plt.subplots(tight_layout=True)\n",
"hist = ax.hist(publicspace_df[\"area_m2\"], edgecolor='black')\n",
"\n",
"formatter = ScalarFormatter()\n",
"formatter.set_scientific(False)\n",
"ax.set_yscale('log')\n",
"ax.yaxis.set_major_formatter(formatter)\n",
"ax.ticklabel_format(style='plain', axis='y')\n",
"ax.ticklabel_format(useOffset=False, style='plain')\n",
"ax.set_xlabel(\"Área en Km²\")\n",
"ax.set_ylabel(\"Numero de áreas\")\n",
"publicspace_df.plot()\n"
]
},
{
"cell_type": "code",
"execution_count": null,
"id": "c2f63fa4-fd79-4344-8934-711fc608ffef",
"metadata": {},
"outputs": [],
"source": [
"urbanarea = urban_mun_mask_6372.area/1000000\n",
"psak2 = publicspace_df.geometry.to_crs(6372).unary_union.area/1000000"
]
},
{
"cell_type": "code",
"execution_count": null,
"id": "c8a17c83-15e5-477e-bdaa-225742abf84d",
"metadata": {},
"outputs": [],
"source": [
"print(\"Área Total: \", municipality_6372.area/1000000, \"Km²\")\n",
"print(\"Área Urbana: \", urbanarea, \"Km²\")\n",
"\n",
"print(\"Espacio Público:\", psak2, \"Km²\")\n",
"print(\"% Espacio Público:\", psak2/(municipality_6372.area/1000000)*100)\n",
"print(\"% Espacio Público:\", psak2/urbanarea*100)"
]
}
],
"metadata": {
"kernelspec": {
"display_name": "Python 3 (ipykernel)",
"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.10.12"
}
},
"nbformat": 4,
"nbformat_minor": 5
}
Write
Preview
Markdown
is supported
0%
Try again
or
attach a new file
Attach a file
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Cancel
Please
register
or
sign in
to comment