{ "cells": [ { "cell_type": "markdown", "metadata": {}, "source": [ "### Objective\n", "* Create a database of hexagons with a column for each need polygon\n", "\n", "### Workflow\n", "* Import data into spatial dataframes\n", " * Hexagons feature class\n", " * Conservation need feature class\n", "* Iterate through each feature in the Needs dataframe\n", " * Extract the feature's shape\n", " * Select all Hexagons that have their center in the shape\n", " * In a new column in the hexagon's dataframe, set values to 1 for selected row" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "#import packages\n", "from arcgis import GIS\n", "from arcgis.geometry import Point\n", "import pandas as pd" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "#set input data filenames\n", "fname_need = '../Data/raw/Basic_Data/1_Conservation_Need.shp'\n", "fname_opportunity = '../Data/raw/Basic_Data/5_Combined_Opportunity.shp'\n", "fname_hex = '../Data/raw/Hexagon_files/100km2_D1_LamAzimNeed.shp'\n", "\n", "#set output data filenames\n", "fname_hex_new = '../scratch/HexData.shp'" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "#read data into spatial dataframes\n", "sdf_need = pd.DataFrame.spatial.from_featureclass(fname_need)\n", "sdf_opportunity = pd.DataFrame.spatial.from_featureclass(fname_opportunity)\n", "sdf_hex = pd.DataFrame.spatial.from_featureclass(fname_hex)" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "#Create a function that returns the centroid from a provided shape\n", "def get_centroid(theShape):\n", " x,y = theShape.centroid\n", " sr = theShape.spatialReference['wkid']\n", " return Point(F'{{\"x\": {x}, \"y\" :{y}, \"spatialReference\" : {{\"wkid\" : {sr}}}}}')" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "#Extract the centroids of all the hexagon features\n", "the_centroids = sdf_hex['SHAPE'].apply(get_centroid)" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "#Iterate through all features in the NEEDS feature class\n", "for i,row in sdf_need.iterrows():\n", " #Status\n", " print(i,end=' ')\n", " #Get the shape\n", " the_poly = row['SHAPE']\n", " #Add new columns in the hex dataframe with 1's and 0's\n", " col_name=F'need{i+1}'\n", " sdf_hex[col_name]=the_centroids.apply(lambda x: 1 if x.within(the_poly) else 0)" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "#Iterate through all features in the OPPOR feature class\n", "for i,row in sdf_opportunity.iterrows():\n", " #Status\n", " print(i,end=' ')\n", " #Get the shape\n", " the_poly = row['SHAPE']\n", " #Add new columns in the hex dataframe with 1's and 0's\n", " col_name=F'opp{i+1}'\n", " sdf_hex[col_name]=the_centroids.apply(lambda x: 1 if x.within(the_poly) else 0)" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "#Get need and opportunity column names\n", "need_cols = [c for c in sdf_hex.columns.tolist() if c[:4]=='need']\n", "opp_cols = [c for c in sdf_hex.columns.tolist() if c[:3]=='opp']" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "#Tally sum of all added columns\n", "sdf_hex['TOT_NEED'] = sdf_hex.loc[:,need_cols].sum(axis=1)\n", "sdf_hex['TOT_OPP'] = sdf_hex.loc[:,opp_cols].sum(axis=1)" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "#Save to new file\n", "outFC = sdf_hex.spatial.to_featureclass(fname_hex_new)" ] } ], "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.6.9" } }, "nbformat": 4, "nbformat_minor": 4 }