Hot answers tagged

8 votes
Accepted

Changing spatial reference of Map using ArcPy in ArcGIS Pro

You're pretty much there with your code. You just need to apply the SR on the map ras= "my_layer.tif" sr=arcpy.Describe(ras).spatialReference p = arcpy.mp.ArcGISProject(r"\\ptemplate\...
KHibma's user avatar
  • 16.7k
7 votes
Accepted

Turning off layer visibility in ArcGIS Pro with Python

I expect that your for loop never runs, because there is no layout element that matches your "Layer" wildcard. You should be getting your layers from the map, not from the layout. Use m....
Son of a Beach's user avatar
6 votes
Accepted

Shapefile not Z-enabled although the environment is Z-enabled in ArcPy

Raster to Point doesn't add Z information, it just creates an attribute in the output 2D feature class's attribute table. From the Raster To Point documentation: Converts a raster dataset to point ...
user2856's user avatar
  • 65.3k
6 votes
Accepted

Calculating day difference between two field using ArcPy

The format code for "month" is a lower-case %m. You're currently specifying "year minute day". https://docs.python.org/3/library/datetime.html#strftime-and-strptime-format-codes
mikewatt's user avatar
  • 5,063
6 votes
Accepted

How to add a custom tool to ArcGIS Pro tab/ribbon

Watch the video I have prepared below, this is how you create custom ribbons with all the tools you want. I show a custom python script tool being added but it could also have been a standard UI tool ...
Hornbydd's user avatar
  • 43.2k
5 votes

Having trouble adding a layer from a Geodatabase using ArcPy

This code won't work in PyCharm. The "CURRENT" (currently open) project is only applicable when run from within ArcGIS Pro. When running outside of ArcGIS Pro such as from PyCharm, there is ...
user2856's user avatar
  • 65.3k
5 votes

Zonal Statistics not executing in arcpy

You still have logical errors in your code. You list the feature classes and do nothing with the data. shapefiles = arcpy.ListFeatureClasses() You initialize an empty list and then try to iterate. ...
Mark Bryant's user avatar
  • 1,437
5 votes
Accepted

Looping in folders subfolders not working using Arcpy

Your fc variable's value is the name of the file (shapefile), but does not include the path to the file, so the system does not know where the feature class is. This may be fine for feature classes ...
Son of a Beach's user avatar
5 votes
Accepted

How to get description instead of coded value for a field with domain in hosted feature layer

Use the ArcGIS for Python API from arcgis.gis import GIS ## or use GIS("agol_url", "username", "password") agol = GIS("home") item = agol.content.get("...
Clubdebambos's user avatar
  • 1,680
5 votes
Accepted

Calculating SUM of multiple fields into new field using ArcPy

Your code appears to attempt to copy all the "STREAMS" values (row[0]) to the "RANKS" field and ignores all the other fields (row[1:4]). But then you use an InsertCursor instead of ...
user2856's user avatar
  • 65.3k
5 votes
Accepted

How to get the index of shape field in ArcPy cursor

You can get the the geometry field name using the Describe function. geometry_field_name = arcpy.Describe(table).shapeFieldName shape_index = cursor.fields.index(geometry_field_name)
Mark Bryant's user avatar
  • 1,437
5 votes
Accepted

Add a basemap for ArcGIS Pro using ArcPy

You pass a name, not a URL. From the documentation: addBasemap (basemap_name) Parameter Explanation Data Type basemap_name The name of the basemap as it appears in the basemap gallery. String If ...
user2856's user avatar
  • 65.3k
5 votes
Accepted

ArcPy and filename.endswith not finding "l" (lower case L)

It's not about "l" at all, it's an error in the Walk line of code. I overlooked this and realized i had it set to tpye = "point" and the feature classes that ended in "l" ...
Dan  Amrine's user avatar
5 votes
Accepted

Search cursor skipping every other row

for row in sCur: is iterating over the cursor, i.e. requesting the next item. Then below that, you request the next item again by using sCur.next(), thus advancing the original iterator. So the ...
mikewatt's user avatar
  • 5,063
4 votes
Accepted

Python code migration

To migrate your code from ArcMap to ArcGIS Pro, you need to change all references from arcpy.mapping to arcpy.mp. This is covered in the documentation here. Have a look at the code sample #2 for ...
Mark Bryant's user avatar
  • 1,437
4 votes

Remove specific characters from row using a list

This code will return a new string with the items in your list removed from that string. Still you do not want to start a new cursor for each list item as that will slow your code down a lot. Better ...
GBG's user avatar
  • 9,624
4 votes
Accepted

ArcPy toolbox - How to limit GPFeatureRecordSetLayer to interactive rectangle input

You can restrict or limit the edit tools for the GPFeatureRecordSetLayer with the use of well defined schema layer template. To create this. either take existing data, or create a new feature class, ...
KHibma's user avatar
  • 16.7k
4 votes
Accepted

Importing CSV file in ArcGIS Pro ArcPy script using relative paths

This: scriptsPath=os.path.dirname(os.path.realpath(__file__)) will give you path to toolbox (works with tbx), so join 'data' and scriptsPath to get location of csv file.
FelixIP's user avatar
  • 22.8k
4 votes
Accepted

Using page query with python

Use Python CIM access as described below. A handy way to see what you want to update is to set it manually, then export the layer to lyrx and open the lyrx in a notepad to view the JSON. ## access ...
Clubdebambos's user avatar
  • 1,680
4 votes
Accepted

Running Python Toolbox in Background

The simple answer is canRunInBackground doesn't do anything in ArcGIS Pro, because as you point out from that article, there is no separate background process to run in. So it doesn't matter if its ...
KHibma's user avatar
  • 16.7k
3 votes

List all feature classes but exclude those which start with a certain character

Given your code you could just check to see if the first character starts with M. fcs = ['databasename.schema.aaa','databasename.schema.Mmm'] for fc in fcs: if fc.split('.')[-1][0] != 'M': ...
GBG's user avatar
  • 9,624
3 votes
Accepted

Changing value of layer attribute based on geometrical conditions of line elements through ArcPy in ArcGIS Pro

You can use pandas and arcpy together #Create a pandas dataframe from your line layer with the columns ID and the line geometry import pandas as pd layer = 'lines_starting_ending_same' fields = ["...
BERA's user avatar
  • 71.2k
3 votes
Accepted

Review contents of arcpy.da.Walk without a for loop

I think that perhaps you're asking the wrong question, and (depending on what the real problem is that you're trying to solve), you may even be better off using something other than 'walk'. As @...
Son of a Beach's user avatar
3 votes
Accepted

Exporting Map Series to multiple PNGs using ArcGIS Pro

The following code can be used to export a MapSeries to several PNGs. It primarily uses arcpy's exportToPNG function (look at the documentation link provided for more details about the function's ...
Felipe D.'s user avatar
  • 2,401
3 votes

Checking topological errors in Esri geodatabase feature class using ArcPy

You are attempting to create the topology in the root level of the GDB. Topology has to be created in a feature dataset. Try the following: # set up workspace and input feature class arcpy.env....
Mark Bryant's user avatar
  • 1,437
3 votes

Remove specific characters from row using a list

This situation really calls for re — Regular expression operations - Python 3.x documentation. It is better to match a pattern than to enumerate all of the possibilities that may exist. What if ...
bixb0012's user avatar
  • 1,951
3 votes
Accepted

Moving large set of points to new lat/long using python in field calculator - ArcMap

Your approach does not appear to work with joined data. What you can do is permanently join the XY fields to the featureclass using Join Field tool then run your expression as shown in your screenshot ...
Hornbydd's user avatar
  • 43.2k
3 votes
Accepted

Changing active definition query for multiple layers in group layer using ArcPy in ArcGIS Pro

I usually set the definition query using the definitionQuery property: lyr.definitionQuery = "TDS_WBS_CODE = 'TC-211606035'" And clear the definitionQueries using: lyr....
dslamb's user avatar
  • 2,166
3 votes
Accepted

How to tell if no features in a layer are selected using python

Try arcpy.da.Describe() on the layer and looking at the FIDSet property. It'll return a list of selected FIDs, or None if there is no selection. desc = arcpy.da.Describe(layer) has_selection = bool(...
mikewatt's user avatar
  • 5,063
3 votes
Accepted

arcpy.da.UpdateCursor() on field values in rows that meet "where_clause"

Your fields are [layer_field, comments_field] but you have the values reversed comment_value = row[0] and layer_value = row[1]. It should be layer_value = row[0] and comment_value = row[1] But as ...
user2856's user avatar
  • 65.3k

Only top scored, non community-wiki answers of a minimum length are eligible