|
4 | 4 | "cell_type": "markdown", |
5 | 5 | "metadata": {}, |
6 | 6 | "source": [ |
7 | | - "# Working with Data\n", |
| 7 | + "# Introduction to the Spatial DataFrame\n", |
8 | 8 | "\n", |
9 | | - "The [Spatial DataFrame](https://esri.github.io/arcgis-python-api/apidoc/html/arcgis.features.toc.html#spatialdataframe) (SDF) creates a simple, intutive object that can easily manipulate geometric and attribute data without forcing you to use a full-fledged copy of any source information. The SDF is based on data structures inherently suited to data analysis, with natural operations for the filtering and inspecting of subsets of values which are fundamental to statistical manipulations. \n", |
| 9 | + "The [Spatial DataFrame](https://esri.github.io/arcgis-python-api/apidoc/html/arcgis.features.toc.html#spatialdataframe) (SDF) creates a simple, intutive object that can easily manipulate geometric and attribute data. The Spatial DataFarme extends the popular Pandas](https://pandas.pydata.org/) [DataFrames](http://pandas.pydata.org/pandas-docs/stable/dsintro.html#dataframe) with spatial abilities, allowing you to use intutive pandorable operations, on both the attribute as well as spatial columns. Thus the SDF is based on data structures inherently suited to data analysis, with natural operations for the filtering and inspecting of subsets of values which are fundamental to statistical and geographic manipulations.\n", |
10 | 10 | "\n", |
11 | 11 | "Data can be read from many **sources**, including shapefiles, [Pandas](https://pandas.pydata.org/) [DataFrames](http://pandas.pydata.org/pandas-docs/stable/dsintro.html#dataframe), feature classes, GeoJSON, and Feature Layers.\n", |
12 | 12 | "\n", |
13 | | - "This document outlines some fundamentals of using the Spatial DataFrame object for working with GIS data.\n", |
| 13 | + "This document outlines some fundamentals of using the `SpatialDataFrame` object for working with GIS data.\n", |
14 | 14 | "\n", |
15 | | - "* [Accessing Published Data](#Accessing-Published-Data)\n", |
16 | | - " * [Reading Service Data](#Reading-Service-Data)\n", |
| 15 | + "* [Accessing GIS Data](#Accessing-GIS-data)\n", |
| 16 | + " * [Reading Web Layers](#Reading-Web-Layers)\n", |
17 | 17 | " * [Reading Feature Layer Data](#Reading-Feature-Layer-Data)\n", |
18 | 18 | " * [Examining Feature Layer Content](#Example:-Examining-Feature-Layer-content)\n", |
19 | 19 | " * [Example: Feature Layer Query Results to a Spatial DataFrame](#Example:-Feature-Layer-Query-Results-to-a-Spatial-DataFrame)\n", |
20 | | - "* [Accessing Feature Class Data](#Accessing-Feature-Class-Data)\n", |
| 20 | + "* [Accessing local GIS Data](#Accessing-local-GIS-data)\n", |
21 | 21 | " * [Example: Reading a Shapefile](#Example:-Reading-a-Shapefile)\n", |
22 | 22 | "* [Saving Spatial DataFrames](#Saving-Spatial-DataFrames)\n", |
23 | 23 | " * [Export Options](#Export-Options)\n", |
|
39 | 39 | "cell_type": "markdown", |
40 | 40 | "metadata": {}, |
41 | 41 | "source": [ |
42 | | - "## Accessing Published Data\n", |
| 42 | + "## Accessing GIS data\n", |
| 43 | + "GIS users need to work with both layers published on remote servers (web layers) and local data, but the ability to manipulate these datasets without permanentently copying the data is lacking. The `SpatialDataFrame` solves this problem because it is an in-memory object that can read, write and manipulate geospatial data.\n", |
43 | 44 | "\n", |
44 | | - "Software users need to work with both published data on remote servers and local data, but the ability to manipulate these datasets without permanentently copying the data is lacking. The `Spatial DataFrame` solves this problem because it is an in-memory object that can read, write and manipulate geospatial data. \n", |
45 | | - "\n", |
46 | | - "The SDF integrates with Esri's [`ArcPy site-package`](http://pro.arcgis.com/en/pro-app/arcpy/get-started/what-is-arcpy-.htm) as well as the open source [`pyshp`](https://github.com/GeospatialPython/pyshp/), [`shapely`](https://github.com/Toblerity/Shapely) and [`fiona`](https://github.com/Toblerity/Fiona) packages. This means the ArcGIS API for Python SDF can use either of these geometry engines to provide you options for easily working with geospatial data regardless of your platform. The SDF transforms data into the formats you desire so you can use Python functionality to analyze and visualize geographic information. \n", |
| 45 | + "The SDF integrates with Esri's [`ArcPy site-package`](http://pro.arcgis.com/en/pro-app/arcpy/get-started/what-is-arcpy-.htm) as well as the open source [`pyshp`](https://github.com/GeospatialPython/pyshp/), [`shapely`](https://github.com/Toblerity/Shapely) and [`fiona`](https://github.com/Toblerity/Fiona) packages. This means the ArcGIS API for Python SDF can use either of these geometry engines to provide you options for easily working with geospatial data regardless of your platform. The SDF transforms data into the formats you desire so you can use Python functionality to analyze and visualize geographic information.\n", |
47 | 46 | "\n", |
48 | 47 | "Data can be read and scripted to automate workflows and just as easily visualized on maps in [`Jupyter notebooks`](../using-the-jupyter-notebook-environment/). The SDF can export data as feature classes or publish them directly to servers for sharing according to your needs.\n", |
49 | 48 | "\n", |
50 | 49 | "Let's explore some of the different options available with the versatile `SpatialDataFrame` object:\n", |
51 | 50 | "\n", |
52 | | - "### Reading Service Data\n", |
| 51 | + "### Reading Web Layers\n", |
53 | 52 | "\n", |
54 | | - "[`Feature layers`](https://doc.arcgis.com/en/arcgis-online/share-maps/hosted-web-layers.htm) hosted on [**ArcGIS Online**](https://www.arcgis.com) provide a wealth of information accessible from any device. However, any individual user will want to format the information to suit his or her needs. The SDF has a [`from_layer`](https://esri.github.io/arcgis-python-api/apidoc/html/arcgis.features.toc.html#arcgis.features.SpatialDataFrame.from_layer) method that allows for consuming a `feature layer`, reporting data from it, and manipulating the data to a form that's comfortable and makes sense for the intended purpose.\n", |
| 53 | + "[`Feature layers`](https://doc.arcgis.com/en/arcgis-online/share-maps/hosted-web-layers.htm) hosted on [**ArcGIS Online**](https://www.arcgis.com) or [**ArcGIS Enterprise**](http://enterprise.arcgis.com/en/) can be easily read into a Spatial DataFrame using the [`from_layer`](https://esri.github.io/arcgis-python-api/apidoc/html/arcgis.features.toc.html#arcgis.features.SpatialDataFrame.from_layer) method. Once you read it into a SDF object, you can create reports, manipulate the data to a form that's comfortable and makes sense for the intended purpose.\n", |
55 | 54 | "\n", |
56 | 55 | "**Example: Retrieving an ArcGIS Online [`item`](https://developers.arcgis.com/rest/users-groups-and-items/publish-item.htm) and using the [`layers`](https://esri.github.io/arcgis-python-api/apidoc/html/arcgis.gis.toc.html#layer) property to inspect the first 5 records of the layer**" |
57 | 56 | ] |
|
568 | 567 | "source": [ |
569 | 568 | "# Obtain the first feature layer from the item\n", |
570 | 569 | "fl = known_item.layers[0]\n", |
| 570 | + "\n", |
571 | 571 | "# Use the `from_layer` method of the Spatial DataFrame to create a new Spatial DataFrame\n", |
572 | 572 | "sdf = SpatialDataFrame.from_layer(fl)\n", |
| 573 | + "\n", |
573 | 574 | "# Return the first 5 records. \n", |
574 | 575 | "sdf.head()" |
575 | 576 | ] |
|
585 | 586 | "cell_type": "markdown", |
586 | 587 | "metadata": {}, |
587 | 588 | "source": [ |
588 | | - "You can also use sql queries to return a subset of records by leveraging the ArcGIS API for Python's [`Feature Layer`](https://esri.github.io/arcgis-python-api/apidoc/html/arcgis.features.toc.html#featurelayer) object itself. Instantiate a `Pandas data frame` directly from the [`feature layer.query`](https://esri.github.io/arcgis-python-api/apidoc/html/arcgis.features.toc.html#arcgis.features.FeatureLayer.query) method and use the data frame's [`head`](http://pandas.pydata.org/pandas-docs/stable/generated/pandas.core.groupby.GroupBy.head.html#pandas.core.groupby.GroupBy.head) method to return the first 5 records and a subset of columns from the DataFrame:\n", |
| 589 | + "You can also use sql queries to return a subset of records by leveraging the ArcGIS API for Python's [`Feature Layer`](https://esri.github.io/arcgis-python-api/apidoc/html/arcgis.features.toc.html#featurelayer) object itself. Instantiate a Pandas `DataFrame` directly from the [`FeatureLayer.query()`](https://esri.github.io/arcgis-python-api/apidoc/html/arcgis.features.toc.html#arcgis.features.FeatureLayer.query) method and use the data frame's [`head()`](http://pandas.pydata.org/pandas-docs/stable/generated/pandas.core.groupby.GroupBy.head.html#pandas.core.groupby.GroupBy.head) method to return the first 5 records and a subset of columns from the DataFrame:\n", |
589 | 590 | "\n", |
590 | 591 | "#### Example: Feature Layer Query Results to a Spatial DataFrame" |
591 | 592 | ] |
|
594 | 595 | "cell_type": "markdown", |
595 | 596 | "metadata": {}, |
596 | 597 | "source": [ |
597 | | - "We'll use the `AGE_45_54` column to query the dataframe and return a new dataframe with a subset of records. We can use the built-in [`zip`](https://docs.python.org/3/library/functions.html#zip) function to print the dataframe atrribute field names in columns, and then use dataframe syntax to view specific attribute fields in the output:" |
| 598 | + "We'll use the `AGE_45_54` column to query the dataframe and return a new `DataFrame` with a subset of records. We can use the built-in [`zip()`](https://docs.python.org/3/library/functions.html#zip) function to print the data frame attribute field names, and then use data frame syntax to view specific attribute fields in the output:" |
598 | 599 | ] |
599 | 600 | }, |
600 | 601 | { |
|
603 | 604 | "metadata": {}, |
604 | 605 | "outputs": [], |
605 | 606 | "source": [ |
606 | | - "# Filter feature layer records with a sql query. See [`where clause documentation`](https://developers.arcgis.com/rest/services-reference/query-feature-service-layer-.htm).\n", |
| 607 | + "# Filter feature layer records with a sql query. \n", |
| 608 | + "# See https://developers.arcgis.com/rest/services-reference/query-feature-service-layer-.htm\n", |
| 609 | + "\n", |
607 | 610 | "df = fl.query(where=\"AGE_45_54 < 1500\").df" |
608 | 611 | ] |
609 | 612 | }, |
|
725 | 728 | "cell_type": "markdown", |
726 | 729 | "metadata": {}, |
727 | 730 | "source": [ |
728 | | - "## Accessing Feature Class Data\n", |
| 731 | + "## Accessing local GIS data\n", |
729 | 732 | "\n", |
730 | 733 | "The SDF can also access local geospatial data. Depending upon what Python modules you have installed, you'll have access to a wide range of functionality: \n", |
731 | 734 | "\n", |
732 | | - "* If the **`ArcPy`** module is installed, meaning you have installed either [`ArcGIS Desktop`](http://desktop.arcgis.com/en/) or [`ArcGIS Pro`](http://pro.arcgis.com/en/pro-app/) (or both) and have installed the ArcGIS API for Python in that same environment, the Spatial DataFrame has methods to read a subset of the ArcGIS Desktop [supported geographic formats](http://desktop.arcgis.com/en/arcmap/10.3/manage-data/datatypes/about-geographic-data-formats.htm#ESRI_SECTION1_4835793C55C0439593A46FD5BC9E64B9), most notably:\n", |
| 735 | + "* If the **`ArcPy`** module is installed, meaning you have installed [`ArcGIS Pro`](http://pro.arcgis.com/en/pro-app/) and have installed the ArcGIS API for Python in that same environment, the `SpatialDataFrame` has methods to read a subset of the ArcGIS Desktop [supported geographic formats](http://desktop.arcgis.com/en/arcmap/10.3/manage-data/datatypes/about-geographic-data-formats.htm#ESRI_SECTION1_4835793C55C0439593A46FD5BC9E64B9), most notably:\n", |
733 | 736 | " * [`feature classes`](http://desktop.arcgis.com/en/arcmap/latest/manage-data/feature-classes/a-quick-tour-of-feature-classes.htm)\n", |
734 | 737 | " * [`shapefiles`](http://desktop.arcgis.com/en/arcmap/latest/manage-data/shapefiles/what-is-a-shapefile.htm), \n", |
735 | 738 | " * [`ArcGIS Server Web Services`](https://enterprise.arcgis.com/en/server/latest/publish-services/windows/what-types-of-services-can-you-publish.htm) and [`ArcGIS Online Hosted Feature Layers`](https://doc.arcgis.com/en/arcgis-online/share-maps/publish-features.htm) \n", |
736 | 739 | " * [`OGC Services`](http://www.opengeospatial.org/standards) \n", |
737 | 740 | "* If the **ArcPy** module is not installed, the SDF [`from_featureclass`](https://esri.github.io/arcgis-python-api/apidoc/html/arcgis.features.toc.html#arcgis.features.SpatialDataFrame.from_featureclass) method only supports consuming an Esri [`shapefile`](http://desktop.arcgis.com/en/arcmap/latest/manage-data/shapefiles/what-is-a-shapefile.htm)\n", |
738 | | - "> Please note that you must install the `pyshp` module to read shapefiles with Python interpreters that don't have access to `ArcPy`.\n", |
| 741 | + "> Please note that you must install the `pyshp` package to read shapefiles in environments that don't have access to `ArcPy`.\n", |
739 | 742 | " \n", |
740 | 743 | "### Example: Reading a Shapefile\n", |
741 | 744 | "> You must authenticate to `ArcGIS Online` or `ArcGIS Enterprise` to use the `from_featureclass()` method to read a shapefile with a Python interpreter that does not have access to `ArcPy` through either an ArcGIS Desktop or ArcGIS Pro installation.\n", |
|
1002 | 1005 | "#### Example: Export a whole dataset to a shapefile:" |
1003 | 1006 | ] |
1004 | 1007 | }, |
1005 | | - { |
1006 | | - "cell_type": "markdown", |
1007 | | - "metadata": {}, |
1008 | | - "source": [ |
1009 | | - "> **Note:** You may receive an error message that ends with the line `SystemError: <built-in function isinstance> returned a result with an error set` the first time you run the `sdf.to_featureclass()` method. If you run the command again, the output reports the path to the feature class and the error message does not appear. As a workaround, you can direct the output to your `temp` directory and retrieve the output (although the error message may still appear):" |
1010 | | - ] |
1011 | | - }, |
1012 | | - { |
1013 | | - "cell_type": "code", |
1014 | | - "execution_count": null, |
1015 | | - "metadata": {}, |
1016 | | - "outputs": [], |
1017 | | - "source": [ |
1018 | | - "# Workaround to the `SystemError` warning message appearing upon first run of `sdf.to_featureclass()`\n", |
1019 | | - "import tempfile\n", |
1020 | | - "folder = tempfile.gettempdir()\n", |
1021 | | - "sdf.to_featureclass(out_location=folder, out_name=\"my_export2.shp\")" |
1022 | | - ] |
1023 | | - }, |
1024 | 1008 | { |
1025 | 1009 | "cell_type": "code", |
1026 | 1010 | "execution_count": 13, |
|
1039 | 1023 | ], |
1040 | 1024 | "source": [ |
1041 | 1025 | "sdf.to_featureclass(out_location=r\"path\\to\\your\\data\\output_example\",\n", |
1042 | | - " out_name=\"output_cities.shp\")\n" |
| 1026 | + " out_name=\"output_cities.shp\")" |
1043 | 1027 | ] |
1044 | 1028 | }, |
1045 | 1029 | { |
1046 | 1030 | "cell_type": "markdown", |
1047 | 1031 | "metadata": {}, |
1048 | 1032 | "source": [ |
1049 | | - "> The ArcGIS API for Python on all `macOS` and `Linux` machines, as well as those `Windows` machines not using Python interpreters that access ArcGIS Desktop or ArcGIS Pro installations will only be able to write out to shapefile format with the `to_featureclass` method. Writing to file geodatabases requires the `ArcPy` site-package." |
| 1033 | + "> The ArcGIS API for Python installs on all `macOS` and `Linux` machines, as well as those `Windows` machines not using Python interpreters that have access to `ArcPy` will only be able to write out to shapefile format with the `to_featureclass` method. Writing to file geodatabases requires the `ArcPy` site-package." |
1050 | 1034 | ] |
1051 | 1035 | }, |
1052 | 1036 | { |
|
1124 | 1108 | "name": "python", |
1125 | 1109 | "nbconvert_exporter": "python", |
1126 | 1110 | "pygments_lexer": "ipython3", |
1127 | | - "version": "3.6.5" |
| 1111 | + "version": "3.6.4" |
1128 | 1112 | } |
1129 | 1113 | }, |
1130 | 1114 | "nbformat": 4, |
|
0 commit comments