========
arcpyext
========
arcpyext provides a collection of helper functions that make common tasks performed with the Esri ArcPy site-package
easier to accomplish. It was chiefly developed to service a command-line tool set (agstools) for managing ArcGIS
environments, but can readily be used within other scripts.
Features
===============
Currently, arcpyext has functionality for changing data sources for map document layers, preparing map documents for
publishing, and performing CRUD operations within an edit session on a geo-database.
arcpyext.conversion
-------------------
The *conversion* module features simple functions for converting workspaces (file geodatabase, enterprise geodatabase),
and the items in them, into other formats.
Supported formats are:
- Shapefile
- MapInfo TAB (requires the ArcGIS Data Interoperability extension)
- GeoPackage
- Keyhole Markup Language (KML)
- Office Open XML Workbook (Excel .xlsx file)
- Comma-Separated Values text file
Example A - Convert File Geodatabase to a GeoPackage
....................................................
.. code-block:: python
import arcpyext.conversion
INPUT_WORKSPACE = "path/to/input_geodatbase.gdb"
OUTPUT_GEOPACKAGE = "path/to/output/geopackage.gpkg"
arcpyext.conversion.to_geopackage.workspace(INPUT_WORKSPACE, OUTPUT_GEOPACKAGE)
Example B - Convert File Geodatabase to an Office Open XML Spreadsheet (Excel spreadsheet)
..........................................................................................
.. code-block:: python
import arcpyext.conversion
INPUT_WORKSPACE = "path/to/input_geodatbase.gdb"
OUTPUT_WORKBOOK = "path/to/output/workbook.xlsx"
arcpyext.conversion.to_ooxml_workbook.workspace(INPUT_WORKSPACE, OUTPUT_WORKBOOK)
arcpyext.data
-------------
The *data* module wraps the basic create, update and delete operations in an edit session, automatically starting/
stoping/aborting an edit operation as appropriate. The functions simply wrap the appropriate *arcpy.da* cursors, so
functionally they work identically. Also provided is a handy function for reading rows into a list.
Example
.......
.. code-block:: python
import arcpy
import arcpyext.data
#WORKSPACE = "path/to/sde_database.sde"
WORKSPACE = "path/to/geodatabase.gdb"
TABLE = "Countries"
edit_session = arcpy.da.Editor(WORKSPACE)
edit_session.startEditing()
try:
arcpyext.data.delete_rows(edit_session, TABLE, "Name LIKE 'A%'", ('Name'))
except RuntimeError, re:
edit_session.stopEditing(False)
edit_session.stopEditing(True)
del edit_session
See the associated tests for more code examples.
arcpyext.mapping
----------------
The *mapping* module provides features for:
- describing a map document/project
- changing the data sources of a layer (or layers) in an ArcGIS Map Document or ArcGIS Project,
- easily checking if a map document/project is in a valid state
- comparing different versions of a map document/project
Describing a Map Document/ArcGIS Project
........................................
Describing a map document/project productions a dictionary detailing many of the details about a map document. Map
documents/projects can be described as follows:
.. code-block:: python
import arcpyext
path_to_mxd_or_project = "path/to/arcgis/map_doc.mxd" # or *.aprx file on ArcGIS Pro
description = arcpyext.mapping.describe(path_to_mxd_or_project)
The ouput description will have the following structure:
.. code-block:: python
{
"filePath": "C:\\projects\\public\\arcpyext\\tests\\samples\\test_mapping_complex.mxd",
# an ordered list of maps contained in the map document/project
"maps": [
{
"name": "Layers",
"spatialReference": "GEOGCS['GCS_GDA_1994',DATUM['D_GDA_1994',SPHEROID['GRS_1980',6378137.0,298.257222101]],PRIMEM['Greenwich',0.0],UNIT['Degree',0.0174532925199433]];-400 -400 1000000000;-100000 10000;-100000 10000;8.98315284119521E-09;0.001;0.001;IsHighPrecision",
# an ordered list of layers contained in the map
"layers": [
{
"dataSource": "C:\\projects\\public\\arcpyext\\tests\\samples\\statesp020_clip1",
"database": "C:\\projects\\public\\arcpyext\\tests\\samples",
"datasetName": "statesp020_clip1",
"datasetType": "Shapefile Feature Class",
"definitionQuery": "FID <1",
"fields": [
{
"alias": "FID",
"index": 0,
"name": "FID",
"type": "OID",
"visible": true
},
{
"alias": "SHAPE",
"index": 1,
"name": "Shape",
"type": "Geometry",
"visible": true
},
{
"alias": "AREA",
"index": 2,
"name": "AREA",
"type": "Double",
"visible": true
},
{
"alias": "PERIMETER",
"index": 3,
"name": "PERIMETER",
"type": "Double",
"visible": true
},
{
"alias": "STATE",
"index": 5,
"name": "STATE",
"type": "String",
"visible": true
}
],
"index": 0,
"isBroken": false,
"isFeatureLayer": true,
"isGroupLayer": false,
"isNetworkAnalystLayer": false,
"isRasterLayer": false,
"isRasterizingLayer": null,
"isServiceLayer": null,
"longName": "Layer 1",
"name": "Layer 1",
"server": null,
"service": null,
"serviceId": 1,
"userName": null,
"visible": true
}
],
# an ordered list of the tables contained in the map
"tables": [
{
"dataSource": "C:\\projects\\public\\arcpyext\\tests\\samples\\statesp020.txt",
"database": "C:\\projects\\public\\arcpyext\\tests\\samples",
"datasetName": "statesp020.txt",
"datasetType": "Text File",
"definitionQuery": "",
"fields": [
{
"alias": "Identification_Information:",
"index": 0,
"name": "Identification_Information:",
"type": "String",
"visible": true
}
],
"index": 0,
"isBroken": false,
"name": "statesp020.txt",
"server": null,
"service": null,
"serviceId": 7,
"userName": null
}
]
}
]
}
Changing Data Sources
.....................
Changing data sources across both map documents and projects is made easy by creating templates with match criteria,
and then evaluating those templates against a map document or project to generate a list of replacement data sources
for layers that match.
Templates have slightly different structures depending on whether you are editing an ArcGIS Map Document or an ArcGIS Project.
ArcGIS Map Document:
.. code-block:: python
"dataSource": {
# The *dataSource* property points to the replacement data source
# The contents of the property depends on whether your changing data sources on a map document or a project
"workspacePath": "path/to/workspace/if/changed",
"datasetName": "nameOfTheNewDatasetIfChanged",
"wokspaceType": "workspaceTypeIfChanged",
"schema": "databaseSchemaNameIfChanged"
},
"matchCriteria": {
# properties that match against properties discovered when describing a layer
# strings are compared ingoring case
# an empty dictionary is also valid, which will match all layers
# Changing user is a common use case for updating data sources
"userName": "ExistingUserName"
}
ArcGIS Project:
.. code-block:: python
"dataSource": {
# Any properties decribed at the following address under the *Using the connectionProperties dictionary*
# section header are valid: https://pro.arcgis.com/en/pro-app/arcpy/mapping/updatingandfixingdatasources.htm
# Example
"connection_info": {
"database": "path/to/database"
},
"dataset": "NewDataset"
},
"matchCriteria": {
# properties that match against properties discovered when describing a layer
# strings are compared ingoring case
# an empty dictionary is also valid, which will match all layers
# Changing user is a common use case for updating data sources
"userName": "ExistingUserName"
}
A list of templates can be used to create a replacement list of data sources for a map document or project.
.. code-block:: python
import arcpyext
path_to_mxd_or_project = "path/to/arcgis/map_doc.mxd" # or *.aprx file on ArcGIS Pro
data_source_templates = [
# one or more templates goes hear
{
"dataSource": {
"workspacePath": "./newDatabaseConnection.sde"
},
"matchCriteria": {
# match everything
}
}
]
replacement_data_source_list = arcpyext.mapping.create_replacement_data_sources_list(
path_to_mxd_or_project,
data_source_templates)
The generated replacement data source list can then be fed back into *arcpyext* to update all of the matched layers
and tables:
.. code-block:: python
arcpyext.mapping.change_data_sources(path_to_mxd_or_project, replacement_data_source_list)
Check a Map Is Valid
....................
A conveniance method exists to quickly test whether a map document/project is in a valid state or not (i.e. has broken
layers/tables or not). This can be called as follows:
.. code-block:: python
import arcpyext
path_to_mxd_or_project = "path/to/arcgis/map_doc.mxd" # or *.aprx file on ArcGIS Pro
arcpyext.mapping.is_valid(path_to_mxd_or_project)
arcpyext.publishing
-------------------
The *publishing* modules provides conveniant functions for creating service definition or drafts from the first map
in an ArcGIS Map Document or ArcGIS Project.
Create a Service Definition Draft from a Map Document
.....................................................
This function checks that the map does not have any broken data sources before crafting a service definition draft.
.. code-block:: python
import arcpyext
path_to_mxd_or_project = "path/to/arcgis/map_doc.mxd" # or *.aprx file on ArcGIS Pro
output_path = "path/to/sddraft/output.sddraft"
service_name = "ExampleMapService"
arcpyext.publishing.convert_map_to_service_draft(path_to_mxd_or_project, output_path, service_name)
Raw data
{
"_id": null,
"home_page": "https://github.com/DavidWhittingham/arcpyext",
"name": "arcpyext",
"maintainer": null,
"docs_url": null,
"requires_python": null,
"maintainer_email": null,
"keywords": "arcgis esri arcpy",
"author": "David Whittingham; David Payne; Adam Kerz; Peter Reyne; Daniel Baternik; Chris Blanchfield; Gary Bagnall",
"author_email": null,
"download_url": null,
"platform": null,
"description": "========\r\narcpyext\r\n========\r\n\r\narcpyext provides a collection of helper functions that make common tasks performed with the Esri ArcPy site-package\r\neasier to accomplish. It was chiefly developed to service a command-line tool set (agstools) for managing ArcGIS\r\nenvironments, but can readily be used within other scripts.\r\n\r\nFeatures\r\n===============\r\n\r\nCurrently, arcpyext has functionality for changing data sources for map document layers, preparing map documents for\r\npublishing, and performing CRUD operations within an edit session on a geo-database.\r\n\r\narcpyext.conversion\r\n-------------------\r\n\r\nThe *conversion* module features simple functions for converting workspaces (file geodatabase, enterprise geodatabase),\r\nand the items in them, into other formats.\r\n\r\nSupported formats are:\r\n\r\n - Shapefile\r\n - MapInfo TAB (requires the ArcGIS Data Interoperability extension)\r\n - GeoPackage\r\n - Keyhole Markup Language (KML)\r\n - Office Open XML Workbook (Excel .xlsx file)\r\n - Comma-Separated Values text file\r\n\r\nExample A - Convert File Geodatabase to a GeoPackage\r\n....................................................\r\n\r\n.. code-block:: python\r\n\r\n import arcpyext.conversion\r\n\r\n INPUT_WORKSPACE = \"path/to/input_geodatbase.gdb\"\r\n OUTPUT_GEOPACKAGE = \"path/to/output/geopackage.gpkg\"\r\n\r\n arcpyext.conversion.to_geopackage.workspace(INPUT_WORKSPACE, OUTPUT_GEOPACKAGE)\r\n\r\nExample B - Convert File Geodatabase to an Office Open XML Spreadsheet (Excel spreadsheet)\r\n..........................................................................................\r\n\r\n.. code-block:: python\r\n\r\n import arcpyext.conversion\r\n\r\n INPUT_WORKSPACE = \"path/to/input_geodatbase.gdb\"\r\n OUTPUT_WORKBOOK = \"path/to/output/workbook.xlsx\"\r\n\r\n arcpyext.conversion.to_ooxml_workbook.workspace(INPUT_WORKSPACE, OUTPUT_WORKBOOK)\r\n\r\narcpyext.data\r\n-------------\r\n\r\nThe *data* module wraps the basic create, update and delete operations in an edit session, automatically starting/\r\nstoping/aborting an edit operation as appropriate. The functions simply wrap the appropriate *arcpy.da* cursors, so\r\nfunctionally they work identically. Also provided is a handy function for reading rows into a list.\r\n\r\nExample\r\n.......\r\n\r\n.. code-block:: python\r\n\r\n import arcpy\r\n import arcpyext.data\r\n\r\n #WORKSPACE = \"path/to/sde_database.sde\"\r\n WORKSPACE = \"path/to/geodatabase.gdb\"\r\n TABLE = \"Countries\"\r\n\r\n edit_session = arcpy.da.Editor(WORKSPACE)\r\n edit_session.startEditing()\r\n\r\n try:\r\n arcpyext.data.delete_rows(edit_session, TABLE, \"Name LIKE 'A%'\", ('Name'))\r\n except RuntimeError, re:\r\n edit_session.stopEditing(False)\r\n\r\n edit_session.stopEditing(True)\r\n del edit_session\r\n\r\nSee the associated tests for more code examples.\r\n\r\narcpyext.mapping\r\n----------------\r\n\r\nThe *mapping* module provides features for:\r\n\r\n - describing a map document/project\r\n - changing the data sources of a layer (or layers) in an ArcGIS Map Document or ArcGIS Project,\r\n - easily checking if a map document/project is in a valid state\r\n - comparing different versions of a map document/project\r\n\r\nDescribing a Map Document/ArcGIS Project\r\n........................................\r\n\r\nDescribing a map document/project productions a dictionary detailing many of the details about a map document. Map\r\ndocuments/projects can be described as follows:\r\n\r\n.. code-block:: python\r\n\r\n import arcpyext\r\n\r\n path_to_mxd_or_project = \"path/to/arcgis/map_doc.mxd\" # or *.aprx file on ArcGIS Pro\r\n\r\n description = arcpyext.mapping.describe(path_to_mxd_or_project)\r\n\r\nThe ouput description will have the following structure:\r\n\r\n.. code-block:: python\r\n\r\n {\r\n \"filePath\": \"C:\\\\projects\\\\public\\\\arcpyext\\\\tests\\\\samples\\\\test_mapping_complex.mxd\",\r\n\r\n # an ordered list of maps contained in the map document/project\r\n \"maps\": [\r\n {\r\n \"name\": \"Layers\",\r\n \"spatialReference\": \"GEOGCS['GCS_GDA_1994',DATUM['D_GDA_1994',SPHEROID['GRS_1980',6378137.0,298.257222101]],PRIMEM['Greenwich',0.0],UNIT['Degree',0.0174532925199433]];-400 -400 1000000000;-100000 10000;-100000 10000;8.98315284119521E-09;0.001;0.001;IsHighPrecision\",\r\n \r\n # an ordered list of layers contained in the map\r\n \"layers\": [\r\n {\r\n \"dataSource\": \"C:\\\\projects\\\\public\\\\arcpyext\\\\tests\\\\samples\\\\statesp020_clip1\",\r\n \"database\": \"C:\\\\projects\\\\public\\\\arcpyext\\\\tests\\\\samples\",\r\n \"datasetName\": \"statesp020_clip1\",\r\n \"datasetType\": \"Shapefile Feature Class\",\r\n \"definitionQuery\": \"FID <1\",\r\n \"fields\": [\r\n {\r\n \"alias\": \"FID\",\r\n \"index\": 0,\r\n \"name\": \"FID\",\r\n \"type\": \"OID\",\r\n \"visible\": true\r\n },\r\n {\r\n \"alias\": \"SHAPE\",\r\n \"index\": 1,\r\n \"name\": \"Shape\",\r\n \"type\": \"Geometry\",\r\n \"visible\": true\r\n },\r\n {\r\n \"alias\": \"AREA\",\r\n \"index\": 2,\r\n \"name\": \"AREA\",\r\n \"type\": \"Double\",\r\n \"visible\": true\r\n },\r\n {\r\n \"alias\": \"PERIMETER\",\r\n \"index\": 3,\r\n \"name\": \"PERIMETER\",\r\n \"type\": \"Double\",\r\n \"visible\": true\r\n },\r\n {\r\n \"alias\": \"STATE\",\r\n \"index\": 5,\r\n \"name\": \"STATE\",\r\n \"type\": \"String\",\r\n \"visible\": true\r\n }\r\n ],\r\n \"index\": 0,\r\n \"isBroken\": false,\r\n \"isFeatureLayer\": true,\r\n \"isGroupLayer\": false,\r\n \"isNetworkAnalystLayer\": false,\r\n \"isRasterLayer\": false,\r\n \"isRasterizingLayer\": null,\r\n \"isServiceLayer\": null,\r\n \"longName\": \"Layer 1\",\r\n \"name\": \"Layer 1\",\r\n \"server\": null,\r\n \"service\": null,\r\n \"serviceId\": 1,\r\n \"userName\": null,\r\n \"visible\": true\r\n }\r\n ],\r\n\r\n # an ordered list of the tables contained in the map\r\n \"tables\": [\r\n {\r\n \"dataSource\": \"C:\\\\projects\\\\public\\\\arcpyext\\\\tests\\\\samples\\\\statesp020.txt\",\r\n \"database\": \"C:\\\\projects\\\\public\\\\arcpyext\\\\tests\\\\samples\",\r\n \"datasetName\": \"statesp020.txt\",\r\n \"datasetType\": \"Text File\",\r\n \"definitionQuery\": \"\",\r\n \"fields\": [\r\n {\r\n \"alias\": \"Identification_Information:\",\r\n \"index\": 0,\r\n \"name\": \"Identification_Information:\",\r\n \"type\": \"String\",\r\n \"visible\": true\r\n }\r\n ],\r\n \"index\": 0,\r\n \"isBroken\": false,\r\n \"name\": \"statesp020.txt\",\r\n \"server\": null,\r\n \"service\": null,\r\n \"serviceId\": 7,\r\n \"userName\": null\r\n }\r\n ]\r\n }\r\n ]\r\n }\r\n \r\nChanging Data Sources\r\n.....................\r\n\r\nChanging data sources across both map documents and projects is made easy by creating templates with match criteria,\r\nand then evaluating those templates against a map document or project to generate a list of replacement data sources\r\nfor layers that match.\r\n\r\nTemplates have slightly different structures depending on whether you are editing an ArcGIS Map Document or an ArcGIS Project.\r\n\r\nArcGIS Map Document:\r\n\r\n.. code-block:: python\r\n\r\n \"dataSource\": {\r\n # The *dataSource* property points to the replacement data source\r\n # The contents of the property depends on whether your changing data sources on a map document or a project\r\n\r\n \"workspacePath\": \"path/to/workspace/if/changed\",\r\n \"datasetName\": \"nameOfTheNewDatasetIfChanged\",\r\n \"wokspaceType\": \"workspaceTypeIfChanged\",\r\n \"schema\": \"databaseSchemaNameIfChanged\"\r\n },\r\n \"matchCriteria\": {\r\n # properties that match against properties discovered when describing a layer\r\n # strings are compared ingoring case\r\n # an empty dictionary is also valid, which will match all layers\r\n\r\n # Changing user is a common use case for updating data sources\r\n \"userName\": \"ExistingUserName\"\r\n }\r\n\r\nArcGIS Project:\r\n\r\n.. code-block:: python\r\n\r\n \"dataSource\": {\r\n # Any properties decribed at the following address under the *Using the connectionProperties dictionary*\r\n # section header are valid: https://pro.arcgis.com/en/pro-app/arcpy/mapping/updatingandfixingdatasources.htm\r\n \r\n # Example\r\n \"connection_info\": {\r\n \"database\": \"path/to/database\"\r\n },\r\n \"dataset\": \"NewDataset\"\r\n },\r\n \"matchCriteria\": {\r\n # properties that match against properties discovered when describing a layer\r\n # strings are compared ingoring case\r\n # an empty dictionary is also valid, which will match all layers\r\n\r\n # Changing user is a common use case for updating data sources\r\n \"userName\": \"ExistingUserName\"\r\n }\r\n\r\nA list of templates can be used to create a replacement list of data sources for a map document or project.\r\n\r\n.. code-block:: python\r\n\r\n import arcpyext\r\n\r\n path_to_mxd_or_project = \"path/to/arcgis/map_doc.mxd\" # or *.aprx file on ArcGIS Pro\r\n data_source_templates = [\r\n # one or more templates goes hear\r\n {\r\n \"dataSource\": {\r\n \"workspacePath\": \"./newDatabaseConnection.sde\"\r\n },\r\n \"matchCriteria\": {\r\n # match everything\r\n }\r\n }\r\n ]\r\n\r\n replacement_data_source_list = arcpyext.mapping.create_replacement_data_sources_list(\r\n path_to_mxd_or_project,\r\n data_source_templates)\r\n\r\nThe generated replacement data source list can then be fed back into *arcpyext* to update all of the matched layers\r\nand tables:\r\n\r\n.. code-block:: python\r\n\r\n arcpyext.mapping.change_data_sources(path_to_mxd_or_project, replacement_data_source_list)\r\n\r\nCheck a Map Is Valid\r\n....................\r\n\r\nA conveniance method exists to quickly test whether a map document/project is in a valid state or not (i.e. has broken\r\nlayers/tables or not). This can be called as follows:\r\n\r\n.. code-block:: python\r\n\r\n import arcpyext\r\n\r\n path_to_mxd_or_project = \"path/to/arcgis/map_doc.mxd\" # or *.aprx file on ArcGIS Pro\r\n\r\n arcpyext.mapping.is_valid(path_to_mxd_or_project)\r\n\r\n\r\narcpyext.publishing\r\n-------------------\r\nThe *publishing* modules provides conveniant functions for creating service definition or drafts from the first map\r\nin an ArcGIS Map Document or ArcGIS Project.\r\n\r\nCreate a Service Definition Draft from a Map Document\r\n.....................................................\r\n\r\nThis function checks that the map does not have any broken data sources before crafting a service definition draft.\r\n\r\n.. code-block:: python\r\n\r\n import arcpyext\r\n\r\n path_to_mxd_or_project = \"path/to/arcgis/map_doc.mxd\" # or *.aprx file on ArcGIS Pro\r\n output_path = \"path/to/sddraft/output.sddraft\"\r\n service_name = \"ExampleMapService\"\r\n \r\n arcpyext.publishing.convert_map_to_service_draft(path_to_mxd_or_project, output_path, service_name)\r\n",
"bugtrack_url": null,
"license": "BSD 3-Clause",
"summary": "Extended functionality for Esri's ArcPy site-package",
"version": "0.7.35",
"project_urls": {
"Homepage": "https://github.com/DavidWhittingham/arcpyext"
},
"split_keywords": [
"arcgis",
"esri",
"arcpy"
],
"urls": [
{
"comment_text": "",
"digests": {
"blake2b_256": "6bf40d728ecadf95b3e530d11a0e466caa7b841bc5813f5cd8f84ba253eea0b9",
"md5": "2449f0db3db007c30e85d7d348a02b7c",
"sha256": "42b883ede9f9419235387ab9588f0db6192272d662184dded619b84cb577ca04"
},
"downloads": -1,
"filename": "arcpyext-0.7.35-py2.py3-none-any.whl",
"has_sig": false,
"md5_digest": "2449f0db3db007c30e85d7d348a02b7c",
"packagetype": "bdist_wheel",
"python_version": "py2.py3",
"requires_python": null,
"size": 82520,
"upload_time": "2024-04-22T05:11:16",
"upload_time_iso_8601": "2024-04-22T05:11:16.132850Z",
"url": "https://files.pythonhosted.org/packages/6b/f4/0d728ecadf95b3e530d11a0e466caa7b841bc5813f5cd8f84ba253eea0b9/arcpyext-0.7.35-py2.py3-none-any.whl",
"yanked": false,
"yanked_reason": null
}
],
"upload_time": "2024-04-22 05:11:16",
"github": true,
"gitlab": false,
"bitbucket": false,
"codeberg": false,
"github_user": "DavidWhittingham",
"github_project": "arcpyext",
"travis_ci": false,
"coveralls": false,
"github_actions": false,
"requirements": [],
"lcname": "arcpyext"
}