Tag Info

Hot answers tagged

57

I believe that the user should clearly define their objectives before deciding which set of tools to use. ModelBuilder and Python scripting excel at different tasks. A few thoughts: ModelBuilder has no mapping capabilities, whereas arcpy.mapping does. Python can be used to optimize workflows, such as with the multiprocessing package or with parallel ...


37

I compiled this list a while ago so it may be somewhat out of date. Making it community wiki so anyone can update/correct/add to it. Also see these general tips for new Python programmers in this answer. Presentations: Python Scripting for ArcGIS Getting Started with Python in ArcGIS 10 ArcGIS Geoprocessing: Python Scripting – Advanced Techniques Python ...


25

Usually Python debuggers/IDEs assume the Python script is running in the same process as itself so debugging a script running in ArcMap.exe is right out -- you need to get enough of the GP scripting environment bootstrapped in a Python script as you can to debug with. A method that's worked very well for me over the past few years is to write a simple ...


24

I am using an example with 1 million randomly generated points inside of a filegeodatabase. Attached here. Here is some code to get us started: import time import arcpy arcpy.env.workspace = "C:\CountTest.gdb" time.sleep(5) # Let the cpu/ram calm before proceeding! """Method 1""" StartTime = time.clock() with arcpy.da.SearchCursor("RandomPoints", ...


24

General python optimization techniques can save you substantial amounts of time. One really good technique for getting a lowdown of where the hold ups are in your script is using the built-in cProfile module: from cProfile import run run("code") # replace code with your code or function Testing using a small data sample will allow you to pinpoint which ...


24

GDAL is the tool to use. In fact that entire call is one line for gdal_rasterize: gdal_rasterize -l mask -i -burn -9999 mask.shp elevation.tif if you knew the no data value of the dem For some python control: lyr = 'mask' shp = 'mask.shp' dem = 'elevation.tif' ndv = -9999 p = os.Popen('gdal_rasterize -l %s -i -burn %d %s %s' % (lyr,ndv,shp,dem) where ...


23

It seems the most common problem with these types of "flow maps" is that when many lines are included, they collide to such a great extent that it makes it difficult to discern any non-obvious pattern (when reciprocal flows are considered it happens to an even greater extent). Also the long lines tend to dominate the graphic, although it is quite possible ...


23

You will find a number of other similar questions on this site that ask the same basic question and have very good references. The most similar (and detailed) is: What are the Python tools/modules/add-ins crucial in GIS? Others include: Python Script examples for geoprocessing shapefiles without using arcpy Pure Python Library for Geometry Operations ...


22

You can do this using a cursor to grab the data from your table and write to a comma-delimited text file. EDIT: I'm adding a more concise block of code to accomplish the task using the csv module of Python New Answer using arcpy.da cursor: import arcpy,csv table =r'c:\path\to\table' outfile = r'c:\path\to\output\ascii\text\file' #--first lets make a ...


22

If an Esri well-known ID is below 32767, it corresponds to the EPSG ID. WKIDs that are 32767 or above are Esri-defined. Either the object isn't in the EPSG Geodetic Parameter Dataset yet, or it probably won't be added. If an object is later added to the EPSG Dataset, Esri will update the WKID to match the EPSG one, but the previous value will still work. ...


22

Keep in mind that Arcpy is essentially a wrapper around ArcObjects. But if you're just trying to call some Python scripts that you don't want to have to rewrite you can spawn a process that calls the python executable with your args. var startInfo = new ProcessStartInfo() { CreateNoWindow = false, UseShellExecute = false, FileName = ...


22

Welcome to GIS.SE! I would probably just put it in GitHub until you really know what you want to share. Even ESRI started jumping on this bandwagon after GeoIQ acquisition.


19

You can change it from the GP options dialog, just point to your executable of choice for editor/debugger.


19

I've set this up as a ArcToolbox type of script, rather than field calculator as whuber did. This is pretty much a straight port of whubers Avenue code. EDIT: the script assumes the height is stored in a field in the featureclass attribute table, not a featureclass with 3D geometries (PolygonZ) import arcpy,os,math arcpy.env.overwriteOutput=True ...


19

It doesn't work because you haven't called the Xlrd modules to read the Excel spreadsheet. Implement it something like this: import xlrd workbook = xlrd.open_workbook('my_workbook.xls') worksheet = workbook.sheet_by_name('Sheet1') This will allow you to read an XLS file with Python. However, ArcPy will read XLS without Xlrd. You can consider the Excel ...


18

I am not aware of an ArcObjects way to get it, but you can enable the Size column in the Customize menu -> ArcCatalog Options -> Contents tab: This works on file geodatabases but not on SDE geodatabases (in that case you could use some DBMS-specific queries to determine it though). It does not work on personal geodatabases.


18

I've been using "in_memory" quite a bit recently. It can be very useful, as it has the potential to dramatically increase processing speeds for certain tasks, however if you are working with very large datasets, it might cause your program to crash. You can use "in_memory" to define process outputs... often, if I am performing a task on a feature class, ...


17

This routine for arcgis10 returns all fcs (standalone OR within a feature dataset) inside a gdb. Just set your arcpy.env.workspace then do a for loop def listFcsInGDB(): ''' set your arcpy.env.workspace to a gdb before calling ''' for fds in arcpy.ListDatasets('','feature') + ['']: for fc in arcpy.ListFeatureClasses('','',fds): ...


17

I would start with these places: Codecademy offers free online coding lessons, including Python. Use this to get the feel of how python works before working on ArcPy. You'll be glad of having the basics down when you work with ArcPy. Check out the free Virtual Campus presentation on ArcPy. It's a bit dry, but it will help orient you to how ArcGIS ...


17

If you are working solely within the confines of ArcGIS, there are a few considerations I would consider when attempting to determine the approach to take. What are you trying to accomplish, What are your current skill sets, Will you be sharing your work with others to use, learn from, or manipulate, and Who is your intended audience. Given those ...


16

I made a few improvements to @Luke's version of the script, mainly for better performance. The GP methods really don't like being called in a tight loop; by removing an unnecessary GP call and moving the necessary one outside of the innermost loop I sped up performance by 10x at least. This also fixes shadows not being created for inner rings, degrees are ...


16

The Polyline class has a new method called "positionAlongLine" in ArcGIS 10.1. This will return a PointGeometry object with exactly one point at a specified distance from the starting end of the line, or a fraction of the distance between the start and end. To find the midpoint, you would just need to do positionAlongLine(0.5,True). To find the midpoints for ...


16

Any time you have a selection on a layer a cursor object will only return the selected rows. for row in arcpy.SearchCursor("name_of_layer_with_selection"): print row.field1, row.field2


16

If you need to create a second cursor for parcels.shp, do so outside of the loop for your first cursor. As it stands, your script is creating a new cursor object for each row in malls.shp which is what's costing you all that processing time. ... rows = arcpy.UpdateCursor('malls.shp',"","",'ParcelID') polyrows = arcpy.SearchCursor('parcels.shp') for row in ...


16

One of the developers of arcpy.da here. We got the performance where it is because performance was our primary concern: the main gripe with the old cursors were that they were slow, not that they lacked any particular functionality. The code uses the same underlying ArcObjects available in ArcGIS since 8.x (the CPython implementation of the search cursor, ...


15

A couple potential suggestions to help speed up your process are: Select Layer By Attribute can be in a Python-only script, without ever launching ArcGIS Desktop. You need to convert your "buff" reference from a file-based reference to an "ArcGIS layer" reference, which ArcGIS can process selection queries against. Use ...


15

A good starting point would be the Geospatial Data Abstraction Library. It is actually made up oftwo libraries -- GDAL for manipulating geospatial raster data and OGR for manipulating geospatial vector data but people usually just call it GDAL. There's a geoprocessing with Python using open source GIS course at the Utah State University. You might want to ...


15

In a lot of my academic research I work with LiDAR data doing surface analysis for geomorphology. I quickly found that performing a lot of operations using arcpy was very slow, especially on large datasets. As a result I began using: pyshp to manipulate shapefiles and update attribute tables numpy to manage ASCII rasters and perform kernel-based analysis ...


15

Here is a Python script for ArcGIS 10 that basically does what you want, except that the output is a CSV file, not a geodatabase table. Feel free to modify and use as you like. Note that it is not well tested and not supported, so use at your own risk. """ This script looks through the specified geodatabase and reports the names of all data elements, their ...



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