|
POST
|
Hi Christian, After calculating the slope and aspect, the only raster you need to reclassify is the aspect raster. This reclassified raster should be used as the zone raster in the zonal statistics as table tool. The slope raster can be of type float. The result will provide a table with all the slope statistics per aspect class. I did a test with a larger area of elevation, but the result showed little variation in mean slope per aspect class. Maybe it might be more interesting to focus on a small area or create regions (Region Group tool) and investigate individual areas instead of aggregating all areas with the same aspect class into a single class. Kind regards, Xander
... View more
05-28-2014
01:43 PM
|
0
|
0
|
382
|
|
POST
|
Hi Matt, The code below might work for you: import arcpy
fc = r'C:\Users\Xander\Documents\ArcGIS\Default.gdb\polyline_flipped'
with arcpy.da.UpdateCursor(fc, ("SHAPE@")) as curs:
for row in curs:
polyline = row[0]
arr_pol = arcpy.Array()
for part in polyline:
lst_part = list(part)
lst_part.reverse()
arr = arcpy.Array()
for pnt in lst_part:
arr.add(pnt)
arr_pol.add(arr)
polyline_new = arcpy.Polyline(arr_pol)
row[0] = polyline_new
curs.updateRow(row) Kind regards, Xander
... View more
05-28-2014
01:05 PM
|
2
|
0
|
544
|
|
POST
|
Hi Martin, You can directly read the extent of a polygon and assign it to the dataframe extent: import arcpy
from datetime import datetime, timedelta
strPolygons = arcpy.GetParameterAsText(0)
# Init
mxd = arcpy.mapping.MapDocument("CURRENT")
df = arcpy.mapping.ListDataFrames(mxd)[0]
xtnInit = df.extent # remember the original extent
# Build list of SHAPEs
lst_shapes = [row[0] for row in arcpy.da.SearchCursor(strPolygons, ['SHAPE@'])]
# Loop through polygons
lst_drawtimes = []
for shape in lst_shapes:
tm_before = datetime.now()
df.extent = shape.extent
arcpy.RefreshActiveView()
tm_after = datetime.now()
duration = tm_after - tm_before
lst_drawtimes.append(duration)
# closing up ...
df.extent = xtnInit
arcpy.RefreshActiveView()
# list stats
print "Number of draws : {0}".format(len(lst_drawtimes))
print "Total time of draws : {0}".format(sum(lst_drawtimes, timedelta()))
if len(lst_drawtimes)> 0:
print "Average time of draws: {0}".format(sum(lst_drawtimes, timedelta())/len(lst_drawtimes))
Kind regards, Xander
... View more
05-21-2014
09:21 AM
|
1
|
0
|
768
|
|
POST
|
Hi Alicia, The line of code where it goes wrong is: arcpy.SelectLayerByAttribute_management("cwdpts4Anlys","SUBSET_SELECTION",'"SAMPLE_DATE" <= date.earliest ' ) This is due to the where clause you're using. Just to be sure, you could put a hash tag "#" before that line and see if there are any results. To create a proper where clause with a date, try this: where = "{0} <= date '{1}'".format(arcpy.AddFieldDelimiters("cwdpts4Anlys", "SAMPLE_DATE"), earliest) arcpy.SelectLayerByAttribute_management("cwdpts4Anlys", "SUBSET_SELECTION", where) The "arcpy.AddFieldDelimiters(datasource, fieldname)" is used to add correct field delimiters to a field name. The format syntax is used to create a correct string to be used as where clause. In this case the "{0}" will be replaced by the field name including the delimiters and {1} will be replaced by the content of the variable "earliest". To use a date in a SQL expression, you use "date" and place the date string between single quotes. It is a little difficult to test without any data, but this might just work. Hope it'll work for you. Kind regards, Xander
... View more
05-19-2014
09:15 AM
|
0
|
0
|
691
|
|
POST
|
Hi Andrés, The only way I found to load images into raster fields (not doing it manually as described in Help topic "Adding raster datasets as attributes in a feature class") is using a raster catalog. You can create a new raster catalog (set Managed By GDB to Yes) in a file geodatabase and load all the images that you may have stored in a folder with the tool "Workspace To Raster Catalog". This will load all the images into a raster catalog, which is kinda like a table. It also contains a raster field holding the raster. If in the table where you want to load the rasters as attribute, you have a field with the name of the raster, this field can be used to join the raster catalog into the table. Please note the a table or featureclass cannot have more than 1 field of type raster! After joining the raster catalog the result can be exported to make the join permanent. The output table or featureclass will contain a field called Raster with the actual raster inside. Kind regards, Xander Any intent I did with code to load the raster resulted in an error. Since I could not find any documentation on doing this in Python I assume it is not supported.
... View more
05-08-2014
05:07 AM
|
0
|
1
|
2654
|
|
POST
|
Hi Andrés, The thing that goes wrong is that you are not writing the binary representation of the image, but a string containing the path to the image. Changing the code to something like this might help: arcpy.env.workspace = r'D:\LABSIG\CAU\trabajos_diarios\Pruebas Raster\mbd_proyecto_arbolado.mdb'
entityDataset = arcpy.ListDatasets("arbolado")
featureClass = arcpy.ListFeatureClasses("Arbol_censo", "", entityDataset[0])
image = r'D:/Proyecto Arbolado/C12/C12_657.JPG'
image_blob = open(image,'rb').read()
cursor = arcpy.UpdateCursor(featureClass[0])
for row in cursor:
row.setValue("raster_field", image_blob)
cursor.updateRow(row) Now I notice you are using a field of type raster. The code above might not work with a raster field. It does work using a BLOB field. What I don't know is if BLOB fields are supported in a personal geodatabase (MDB). I would recommend switching to a file geodatabase. You can read more about reading and writing images to a BLOB field in the following blog: http://anothergisblog.blogspot.nl/2012/06/working-with-blob-data-at-101-arcpyda.html Kind regards, Xander
... View more
05-07-2014
01:56 AM
|
0
|
0
|
2654
|
|
POST
|
Hi Melissa, Maybe the thread below can help you further: http://forums.arcgis.com/threads/95111-multiplication-raster-by-table Although it is based on a single table and does not use ModelBuilder but Python coding, it should be able to provide you some insights. Kind regards, Xander
... View more
05-07-2014
01:07 AM
|
0
|
0
|
1378
|
|
POST
|
Hi Xander The following is exactly what I'm trying to achieve. I've not worked with python dictionaries yet. Would you mind explaining to me how to convert the python dictionary into a File Geodatabase Table? Thanks for all your help so far, its much appreciated. Regards Hi Peter, It is nice to see that James Crandall has joined this thread with some panda and numpy magic. On previous occasions James has pointed out the strength of these modules. Since panda is not available by default on a ArcGIS system and numpy is, I will try to show you how you could write the dictionary to a table, by using numpy. If you want to use the arpy way of creating an empty table, adding field and using an insert cursor to fill the table, you can look at the thread below, where an example is shown: http://forums.arcgis.com/threads/105303-Take-identical-fields-and-concatenate-additional-field?p=375982&viewfull=1#post375982 Here the code to use numpy to store the table: import arcpy, os, numpy
arcpy.env.overwriteOutput = True
# your data:
tbl = r'C:\Project\_Forums\_maxDict\fgdb\test.gdb\aTable'
fld_quin = 'Quinary'
fld_wma = 'WMA' # Water Management Area
fld_perc = 'Percentage'
fld_oid = arcpy.Describe(tbl).OIDfieldname # or specify ID fieldname
# extra vars for output
fld_id = 'ID'
out_ws = r'C:\Project\_Forums\_maxDict\fgdb\test.gdb'
tbl_name = 'aNewOutputTable'
# create empty dictionary to store results
dct = {}
# loop through table
flds = (fld_oid, fld_quin, fld_wma, fld_perc)
with arcpy.da.SearchCursor(tbl, flds) as curs:
for row in curs:
oid, perc = row[0], row[3]
quin, wma = row[1], row[2]
if quin in dct:
max_perc = dct[quin][2]
if perc > max_perc:
dct[quin] = (oid, wma, perc)
else:
dct[quin] = (oid, wma, perc)
# create list for use in numpy
lst_out = [(val[0], quin, val[1], val[2]) for quin, val in dct.items()]
# create a numpy array
npa = numpy.array(lst_out, numpy.dtype([(fld_id, numpy.int32), (fld_quin, '|S8'), (fld_wma, '|S8'), (fld_perc, numpy.float)]))
# output list of fields
flds = (fld_id, fld_quin, fld_wma, fld_perc)
# store the table
arcpy.da.NumPyArrayToTable(npa, os.path.join(out_ws, tbl_name), flds) Kind regards, Xander
... View more
03-26-2014
11:34 PM
|
0
|
0
|
2584
|
|
POST
|
Hi Jeremy, I normally prefer using an insert cursor, although James Crandall once showed me a more elegant way using panda and numpy. Below the code using the insert cursor: I added a reference to the os module two extra variables to indicate the output workspace (path of fgdb) and the output table name I copied your code for creating the empty table I changed adding the fields slightly (now uses the same variables used earlier in the script) The insert cursor create a row (tuple in the same order of fields as the 'flds' variable) and finally insert the row into the cursor import arcpy, os
arcpy.env.overwriteOutput = True
# your data:
tbl = r'C:\Project\_Forums\_maxDict\fgdb\test.gdb\aTable2'
fld_freq = 'Frequency'
fld_mapcode = 'Map_Code'
fld_datasource = 'DataSource'
# extra vars for output table
out_ws = r'C:\Output' # r'C:\Project\_Forums\_maxDict\fgdb\test.gdb'
tbl_name = 'mmc2.dbf' # 'mmc2'
# create empty dictionary to store results
dct = {}
# loop through table
flds = (fld_freq, fld_mapcode, fld_datasource)
with arcpy.da.SearchCursor(tbl, flds) as curs:
for row in curs:
freq = row[0]
mc = row[1]
ds = row[2]
if mc in dct:
freq_tot = dct[mc][0] + freq
lst_ds = dct[mc][1]
lst_ds.append(ds)
dct[mc] = (freq_tot, lst_ds)
else:
dct[mc] = (freq, [ds])
# write results to table
arcpy.CreateTable_management(out_ws, tbl_name)
tbl_out = os.path.join(out_ws, tbl_name)
arcpy.AddField_management(tbl_out, fld_mapcode, "Text", 9, "", 10, "", "NULLABLE", "")
arcpy.AddField_management(tbl_out, fld_freq, "LONG", 9, 0, "", "", "NULLABLE", "")
arcpy.AddField_management(tbl_out, fld_datasource, "Text", 9, "", 255, "", "NULLABLE", "")
# we still have the var flds
with arcpy.da.InsertCursor(tbl_out, flds) as curs:
for mc, val in dct.items():
lst_ds = sorted(set(val[1]))
freq_tot = val[0]
row = (freq_tot, mc, ";".join(lst_ds))
curs.insertRow(row) With the chance of confusing you a little bit here is a numpy solution: import arcpy, os, numpy
arcpy.env.overwriteOutput = True
# your data:
tbl = r'C:\Project\_Forums\_maxDict\fgdb\test.gdb\aTable2'
fld_freq = 'Frequency'
fld_mapcode = 'Map_Code'
fld_datasource = 'DataSource'
# extra vars for output
out_ws = r'C:\Output' # r'C:\Project\_Forums\_maxDict\fgdb\test.gdb'
tbl_name = 'mmc2.dbf' # 'mmc2'
# create empty dictionary to store results
dct = {}
# loop through table
flds = (fld_freq, fld_mapcode, fld_datasource)
with arcpy.da.SearchCursor(tbl, flds) as curs:
for row in curs:
freq = row[0]
mc = row[1]
ds = row[2]
if mc in dct:
freq_tot = dct[mc][0] + freq
lst_ds = dct[mc][1]
lst_ds.append(ds)
dct[mc] = (freq_tot, lst_ds)
else:
dct[mc] = (freq, [ds])
# create list for use in numpy; each item is a tuple of frequency, map code and list of datasources
lst_out = [(val[0], mc, ";".join(sorted(set(val[1])))) for mc, val in dct.items()]
# convert the list to a numpy array
npa = numpy.array(lst_out, numpy.dtype([(fld_freq, numpy.int32), (fld_mapcode, '|S9'), (fld_datasource, '|S255')]))
# store the table
arcpy.da.NumPyArrayToTable(npa, os.path.join(out_ws, tbl_name), flds)
... View more
03-26-2014
11:02 PM
|
0
|
0
|
1307
|
|
POST
|
Hi Jeremy, Take a look at the code below. Basically I reference a table (could also be a feature class) and specify the fields. In the loop through the table of feature class I check to see if the mapcode (mc) is already in the dictionary. If so the frequency is added (summed) and the datasource is added to the list. If the mapcode is not in the dictionary a new entry is created with the frequency and a list holding a single datasource. At the end I create a unique list of the datasources (per mapcode) with the set command and I join the list by putting a semicolon between each datasource. import arcpy # your data: tbl = r'C:\Project\_Forums\_maxDict\fgdb\test.gdb\aTable2' fld_freq = 'Frequency' fld_mapcode = 'Map_Code' fld_datasource = 'DataSource' # create empty dictionary to store results dct = {} # loop through table flds = (fld_freq, fld_mapcode, fld_datasource) with arcpy.da.SearchCursor(tbl, flds) as curs: for row in curs: freq = row[0] mc = row[1] ds = row[2] if mc in dct: freq_tot = dct[mc][0] + freq lst_ds = dct[mc][1] lst_ds.append(ds) dct[mc] = (freq_tot, lst_ds) else: dct[mc] = (freq, [ds]) # print results for mc, val in dct.items(): lst_ds = sorted(set(val[1])) # make a unique sorted list freq_tot = val[0] print freq_tot, mc, ";".join(lst_ds) The result is this: 122.0 !@s DSP19;DSP27 110.0 !@f DSP14;DSP77;DSP81 Kind regards, Xander
... View more
03-26-2014
12:52 AM
|
0
|
0
|
1307
|
|
POST
|
Hi Peter, Looking at the desired result, you could use something like this: import arcpy
# your data:
tbl = r'C:\Project\_Forums\_maxDict\fgdb\test.gdb\aTable'
fld_quin = 'Quinary'
fld_wma = 'WMA' # Water Management Area
fld_perc = 'Percentage'
fld_oid = arcpy.Describe(tbl).OIDfieldname # or specify ID fieldname
# create empty dictionary to store results
dct = {}
# loop through table
flds = (fld_oid, fld_quin, fld_wma, fld_perc)
with arcpy.da.SearchCursor(tbl, flds) as curs:
for row in curs:
oid, perc = row[0], row[3]
quin, wma = row[1], row[2]
if quin in dct:
max_perc = dct[quin][2]
if perc > max_perc:
dct[quin] = (oid, wma, perc)
else:
dct[quin] = (oid, wma, perc)
# print results
for quin, val in dct.items():
print val[0], quin, val[1], val[2] This produces the following output: 3 Q1 W2 50.0 4 Q2 W3 90.0 The dictionary holds the Quinary as key value and a tuple of OID, WMA and Percentage as value. In this case the maximum percentage is stored per Quinary. In case you are interested in determining the maximum percentage per combination of Quinary and Water Management Area, the code would slightly change: import arcpy
# your data:
tbl = r'C:\Project\_Forums\_maxDict\fgdb\test.gdb\aTable'
fld_quin = 'Quinary'
fld_wma = 'WMA' # Water Management Area
fld_perc = 'Percentage'
fld_oid = arcpy.Describe(tbl).OIDfieldname
# create empty dictionary to store results
dct = {}
# loop through table
flds = (fld_oid, fld_quin, fld_wma, fld_perc)
with arcpy.da.SearchCursor(tbl, flds) as curs:
for row in curs:
oid, perc = row[0], row[3]
quin, wma = row[1], row[2]
key = (quin, wma)
if key in dct:
max_perc = dct[key][1]
if perc > max_perc:
dct[key] = (oid, perc)
else:
dct[key] = (oid, perc)
# print results
for key, val in dct.items():
print val[0], key[0], key[1], val[1] Kind regards, Xander
... View more
03-26-2014
12:32 AM
|
0
|
0
|
2584
|
|
POST
|
The Calculate Value tool requires an expression, and an output type (which could be variant) and an optional expression. If you create a string as result, which combines the X and Y like this "{0} {1}".format(X, Y), then you can connect it to the coordinates input of the tool (which you can just drag to your model). You should define some of the tool parameters as model parameters. Now, it might be better to take the calculation you are planning inside the tool, but in order to judge that, I would have to know what type of calculation you are planning to perform and what the input data will be. Kind regards, Xander
... View more
03-21-2014
06:48 AM
|
0
|
0
|
900
|
|
POST
|
Hi Maher, I don't think that the problem you are facing has anything to do with the underlying Python code. However, I did attach a zip with the toolbox, and this toolbox worked for me specifying X=4 and Y=5 using WGS84. If this doesn't work for you, you will have to provide some more details on what you specify in the dialog, version of ArcGIS, write privileges on output location, etc. If you use a tool from the standard ArcGIS toolboxes and write to an output featureclass (same location as in the provided tool), does that work? Kind regards, Xander
... View more
03-21-2014
04:46 AM
|
0
|
0
|
900
|
|
POST
|
A few pointers: You can define in your tool a parameter type "Point". This will provide two input boxes to specify the X and Y coordinates. The result (when read as text) is a string containing "Xvalue Yvalue" When I tested a snippet of code I received an error indicating that the input feature falls outside the output geometry domain. So an spatial reference is probably required. Define a second parameter with type "Spatial Reference" The third parameter in this case will be output featureclass. Set the direction to output. In the line where you use the "arcpy.CopyFeatures_management" you have to provide an output featureclass as second parameter Se below the code for this tool: import arcpy
arcpy.env.overwriteOutput = True
# Point, input
pnt_in = arcpy.GetParameterAsText(0)
arcpy.AddMessage("Input pnt_in: {0}".format(pnt_in))
# Spatial Reference, input
sr = arcpy.GetParameter(1)
arcpy.AddMessage("Input sr: {0}".format(sr))
# Featureclass, output
fc = arcpy.GetParameter(2)
arcpy.AddMessage("Input fc: {0}".format(fc))
# split the XY coordinates text into a list
lst = pnt_in.split()
arcpy.AddMessage("lst: {0}".format(lst))
# create a point object from the X and Y values in the list
pnt = arcpy.Point(lst[0], lst[1])
arcpy.AddMessage("pnt: X={0} Y={1}".format(pnt.X, pnt.Y))
# create a point geometry object point object and apply the spatial reference
pnt_geom = arcpy.PointGeometry(pnt, sr)
arcpy.AddMessage("pnt_geom: X={0} Y={1}".format(pnt_geom.firstPoint.X, pnt_geom.firstPoint.Y))
# create the output featureclass,
arcpy.CopyFeatures_management(pnt_geom, fc)
arcpy.AddMessage(arcpy.GetMessages())
# your third parameter is already set in the dialog, no need to use the "arcpy.SetParameterAsText" Hope this helps you. Kind regards, Xander
... View more
03-21-2014
12:09 AM
|
0
|
0
|
900
|
|
POST
|
You could use something like: filename = raw_input('Enter a file name: ') More info here: http://docs.python.org/2.7/library/functions.html#raw_input Kind regards, Xander
... View more
03-14-2014
12:53 AM
|
0
|
0
|
493
|
| Title | Kudos | Posted |
|---|---|---|
| 6 | 12-20-2019 08:41 AM | |
| 1 | 01-21-2020 07:21 AM | |
| 2 | 01-30-2020 12:46 PM | |
| 1 | 05-30-2019 08:24 AM | |
| 1 | 05-29-2019 02:45 PM |
| Online Status |
Offline
|
| Date Last Visited |
11-26-2025
02:43 PM
|