{ "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": 1, "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": 24, "metadata": {}, "outputs": [], "source": [ "#set input data filenames\n", "fname_need = '../Data/raw/Basic_Data/1_Conservation_Need.shp'\n", "fname_hex = '../Data/raw/Hexagon_files/100km2_D1_LamAzimNeed.shp'\n", "\n", "#set output data filenames\n", "fname_hex_new = '../scratch/need_hexagons.shp'" ] }, { "cell_type": "code", "execution_count": 6, "metadata": {}, "outputs": [], "source": [ "#read data into spatial dataframes\n", "sdf_need = pd.DataFrame.spatial.from_featureclass(fname_need)\n", "sdf_hex = pd.DataFrame.spatial.from_featureclass(fname_hex)" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "#### Proof of concept\n", "Before iterating through all features, let's examine just one. We want to extract the first shape from the Needs geodataframe, then use that to select hexagons.\n", "\n", "More info:\n", "* https://developers.arcgis.com/python/guide/spatially-enabled-dataframe-advanced-topics/" ] }, { "cell_type": "code", "execution_count": 7, "metadata": {}, "outputs": [], "source": [ "#Get the first needs shape\n", "the_poly = sdf_need.at[0,'SHAPE']" ] }, { "cell_type": "code", "execution_count": 8, "metadata": {}, "outputs": [], "source": [ "the_hex = sdf_hex.at[0,'SHAPE']\n", "x,y = the_hex.centroid" ] }, { "cell_type": "code", "execution_count": 11, "metadata": {}, "outputs": [], "source": [ "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": 12, "metadata": {}, "outputs": [], "source": [ "#Create a column of centroids - takes a moment\n", "sdf_hex['centroid'] = sdf_hex['SHAPE'].apply(get_centroid)" ] }, { "cell_type": "code", "execution_count": 19, "metadata": {}, "outputs": [], "source": [ "#Apply the function to all hexagons - takes a moment\n", "sdf_hex['need1']=sdf_hex['centroid'].apply(lambda x: 1 if x.within(the_poly) else 0)\n", "sdf_hex.head()" ] }, { "cell_type": "code", "execution_count": 23, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "'C:\\\\Workspace\\\\ENV859_2020_prep\\\\Projects\\\\MarineProtectedAreas\\\\AntarcticMPAs\\\\Scratch\\\\need_hexagons.shp'" ] }, "execution_count": 23, "metadata": {}, "output_type": "execute_result" } ], "source": [ "#Save as feature class\n", "sdf_hex.loc[:,['FID','SHAPE','need1']].spatial.to_featureclass(fname_hex_new)" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "#### The real deal" ] }, { "cell_type": "code", "execution_count": 25, "metadata": {}, "outputs": [], "source": [ "#re-read data into spatial dataframes\n", "sdf_need = pd.DataFrame.spatial.from_featureclass(fname_need)\n", "sdf_hex = pd.DataFrame.spatial.from_featureclass(fname_hex)" ] }, { "cell_type": "code", "execution_count": 26, "metadata": {}, "outputs": [], "source": [ "#re-add centroids to the imported spatial dataframe\n", "the_centroids = sdf_hex['SHAPE'].apply(get_centroid)" ] }, { "cell_type": "code", "execution_count": 27, "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "0 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 " ] } ], "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": 28, "metadata": {}, "outputs": [], "source": [ "#Tally sum of all added columns\n", "sdf_hex['TOTAL'] = totals = sdf_hex.iloc[:,-i-1:].sum(axis=1)" ] }, { "cell_type": "code", "execution_count": 29, "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 }