|
POST
|
True! The with statement does not work for the old cursors, but the list comprehension can be used to determine the min and max...
... View more
09-23-2014
02:17 PM
|
0
|
0
|
2433
|
|
POST
|
Haven't tested it, but this might work in 10.0 SP5:
def normalizeField(in_table, in_field, out_field):
lst = [r.getValue(in_field) for r in arcpy.SearchCursor(in_table)]
minimum = min(lst)
maximum = max(lst)
with arcpy.UpdateCursor(in_table) as cur:
for row in cur:
row.setValue(out_field, (float(row.getValue(in_field)) - minimum)/(maximum - minimum))
cur.updateRow(row)
row.setValue
... View more
09-23-2014
01:53 PM
|
2
|
2
|
2433
|
|
POST
|
The data access module (da) was introduced at 10.1...
... View more
09-23-2014
01:38 PM
|
0
|
0
|
2433
|
|
POST
|
Using the attached sample file and the code below I obtained a list of precipitation values:
infile = r"D:\Xander\txt\precip.txt"
sep = "\t" # example TAB as seperator
lst_months = [4, 5, 6, 7, 8]
growing = []
header = 1
i = 0
with open(infile,"r") as f:
for line in f:
i += 1
if i > header:
line = line.strip("\n")
data = line.split(sep)
if len(data) == 2:
print data
date = data[0]
prec = float(data[1])
month = int(date.split("-")[1])
if month in lst_months:
growing.append(prec)
print growing
Kind regards, Xander
... View more
09-23-2014
12:58 PM
|
2
|
10
|
3236
|
|
POST
|
You are absolutely right. Memory considerations should be taken into account.
... View more
09-23-2014
12:42 PM
|
0
|
0
|
7406
|
|
POST
|
Nice example Joshua Bixby, but maybe to make it a little shorter you can determine the min and max as follows:
def normalizeField(in_table, in_field, out_field):
lst = [r[0] for r in arcpy.da.SearchCursor(in_table, (in_field))]
minimum = min(lst)
maximum = max(lst)
# ect
Not sure how this affects performance though. Kind regards, Xander
... View more
09-23-2014
12:12 PM
|
0
|
2
|
7406
|
|
POST
|
Hi Manny, My explanation was a little short, sorry for that... and creating these type of "one-liners" doesn't allow for easy interpretation. Let's break it down into pieces:
# let's say we have a list with duplicate values
list1 = [1, 3, 5, 7, 3, 2, 5, 6, 8, 7, 5, 4, 3, 2, 6, 2]
# the set() is used to convert it to a set of unique values
set1 = set(list1)
print set1
# returns: set([1, 2, 3, 4, 5, 6, 7, 8])
# if you want to convert it to a list again (to loop over it)
list2 = list(set1)
print list2
# returns [1, 2, 3, 4, 5, 6, 7, 8]
A set is very powerful to get a unique list of values and combining sets allows you to obtain values that are in one list but not in the other and info like this. More on this in my post: Some Python Snippets (scroll down to comparing lists) To create the list some list comprehensions are used (check out this document I posted: Using Python List Comprehensions ). Let's break down the code:
unique_list = list(set("{0}_{1}".format(r[0], r[1]) for r in arcpy.da.SearchCursor(FC_or_TBL, (fld_name1, fld_name2, fld_search), where)))
The set() and list() at the beginning of the line have been explained above. Within the brackets of the set() command a lot is going on. Let's not start from left to right, but with the SearchCursor. It takes a table or featureclass, a tuple (or list) of field names and in this case a where clause. This is pretty standard and I don't think needs much explanation. Normally when you use a search cursor you obtain a cursor object which you use to loop through each row (or feature) in the cursor. Next you probably access the fields in each row and do something with it like this:
with arcpy.da.SearchCursor(FC_or_TBL, (fld_name1, fld_name2, fld_search), where) as curs:
for row in curs:
val_fld1 = row[0]
# etc
In case of the list comprehension this is done on one line. It retrieves each row "r" in the SeachCursor and returns this:
"{0}_{1}".format(r[0], r[1])
string.format() allows you to create a string and pass in variables. It is a pretty need way to combine values and string in a certain format r[0] refers to the value of fld_name1 r[1] refers to the value of fld_name2 {0} refers to the first parameter specified in the format command {1} refers to the second parameter specified in the format command ... so it the value for fld_name1 is "abc" and the value in fld_name2 would be 1, this would result in the string "abc_1". I used something similar for creating the where clause:
where = "{0} = '{1}'".format(arcpy.AddFieldDelimiters(FC_or_TBL, fld_search), search_value)
It starts with a string "{0} = '{1}'". The {0} is replaced with the first argument of the format function:
arcpy.AddFieldDelimiters(FC_or_TBL, fld_search)
This will put quotes or brackets or nothing around the field name based on the type of workspace (that can be useful if you don't know if your input is a shapefiles, personal, file or enterprise geodatabase... The {1} is replace with the value in the variable search_value. Please note that this where clause will fail if your field is not of type TEXT (string). It is possible to extend this functionality and detect the columns type, but for simplicity reasons (read I was to lazy) I did not do that. If you have any other doubt, please let me know... Kind regards, Xander
... View more
09-09-2014
07:17 PM
|
0
|
2
|
1181
|
|
POST
|
Some small example on creating a unique list of values in a field:
# create a unique list of the values in a field in a table or featureclass
import arcpy
FC_or_TBL = r"D:\Xander\Genesis\Tablas LayerRef\bk_DLLO_931.gdb\AG_LAYERREF"
fld_name1 = "COLUMNA"
unique_list = list(set(r[0] for r in arcpy.da.SearchCursor(FC_or_TBL, (fld_name1))))
unique_list.sort()
print "\n".join(unique_list)
print "\n#####\n"
On line 5 a list is created using list comprehensions, which is converted to a set (unique values) and parsed back to a list
# using a combination of 2 columns
import arcpy
FC_or_TBL = r"D:\Xander\Genesis\Tablas LayerRef\bk_DLLO_931.gdb\AG_LAYERREF"
fld_name1 = "COLUMNA"
fld_name2 = "ID_LAYERREF"
unique_list = list(set("{0},{1}".format(r[0], r[1]) for r in arcpy.da.SearchCursor(FC_or_TBL, (fld_name1, fld_name2))))
print "\n".join(unique_list)
print "\n#####\n"
On line 6 the list is created from the combination of 2 fields
# including a where clause
import arcpy
FC_or_TBL = r"D:\Xander\Genesis\Tablas LayerRef\bk_DLLO_931.gdb\AG_LAYERREF"
fld_name1 = "COLUMNA"
fld_name2 = "ID_LAYERREF"
fld_search = "COLUMNA"
search_value = "OBJECTID"
where = "{0} = '{1}'".format(arcpy.AddFieldDelimiters(FC_or_TBL, fld_search), search_value)
unique_list = list(set("{0}_{1}".format(r[0], r[1]) for r in arcpy.da.SearchCursor(FC_or_TBL, (fld_name1, fld_name2, fld_search), where)))
print "\n".join(unique_list)
print "\n#####\n"
In this case an additional where clause is used to limited the results. As James already showed, Pandas is a powerful library. If you have it, use it. If you don't and you don't want to install it, the above might be an alternative. Kind regards, Xander
... View more
09-09-2014
10:21 AM
|
3
|
4
|
4339
|
|
POST
|
There is a Point Density (in the Spatial Analyst Toolbox) that might work for you. Kind regards, Xander
... View more
09-03-2014
10:00 AM
|
0
|
2
|
1364
|
|
POST
|
Observe code sample below. Once again I haven't tested the code, so be careful. Changes in syntax can be found on lines 16, 18-20 + 35 and 37
def main():
global arcpy
import arcpy, os
# fixed settings
fldBLOB = 'DATA'
fldAttName = 'ATT_NAME'
fldRelOID = 'REL_OBJECTID'
# your settings (edit these)
fc = r"C:\Project\_LearnPython\Attachments\test.gdb\Meldingen"
tbl = r"C:\Project\_LearnPython\Attachments\test.gdb\Meldingen__ATTACH"
outFolder = r"C:\Project\_LearnPython\Attachments"
fldRelatedInfo = 'SomeFieldInFeatureClass' # should be valid column in FC
with arcpy.SearchCursor(tbl) as cursor:
for row in cursor:
binaryRep = row.getValue(fldBLOB)
fileName = row.getValue(fldAttName)
relOID = row.getValue(fldRelOID)
# access related information
myRelatedInfo = QueryRelatedData(fc, fldRelatedInfo, relOID)
# do something with the related info
# save attachment to disk
open(outFolder + os.sep + fileName, 'wb').write(binaryRep.tobytes())
del row
del binaryRep
del fileName
def QueryRelatedData(fc, fldRelatedInfo, relOID):
fldOID = 'OBJECTID'
expression = arcpy.AddFieldDelimiters(fc, fldOID) + " = {0}".format(relOID)
with arcpy.SearchCursor(fc, where_clause=expression) as cursor:
for row in cursor:
return row.getValue(fldRelatedInfo)
break
del row
if __name__ == '__main__':
main()
... View more
09-03-2014
09:50 AM
|
0
|
2
|
2608
|
|
POST
|
If you are interested in using some python you could use something like this:
import arcpy
from arcpy.sa import *
dem = "C:/examples/data/dem" # reference to your DEM
outname = "C:/examples/data/result" # output raster to be created
dem_ras = arcpy.Raster(dem) # create a raster object
dem_min = dem_ras.minimum # get minimum of dem
outras = Con(dem_ras == dem_min, dem_ras)
outras.save(outname)
Con http://resources.arcgis.com/en/help/main/10.2/index.html#//009z00000005000000 Raster http://resources.arcgis.com/en/help/main/10.2/index.html#//018z00000051000000
... View more
08-30-2014
08:50 PM
|
2
|
1
|
1496
|
|
POST
|
When exporting a raster converted to points to Excel be aware that the amount of information (# of points) can be very high making it hard to manage in Excel. I suppose that the shapefile you have does not have the X and Y columns yet, right? If that is the case, you could add them as attributes to the shapefile. Open the attribute table of the shapefile and add the fields X and Y as double fields. Right click on the column name and choose calculate geometry. Calculate the geometry property X for field X and Y for Y. You can read more about this here: ArcGIS Help (10.2, 10.2.1, and 10.2.2) Next you can use the Table to Excel tool in the conversion toolbox to convert it to an Excel file. More on the tool can be found here: ArcGIS Help (10.2, 10.2.1, and 10.2.2) What are you planning to do with the data in Excel? Kind regards, Xander
... View more
08-29-2014
07:22 PM
|
2
|
1
|
12320
|
|
POST
|
The help mentions some requirements (ArcGIS Help 10.1 ) but I suppose you have already checked those. The page also shows some examples in which the sub type field is explicitly set. What happens if you do this?
... View more
08-28-2014
09:04 PM
|
0
|
0
|
1076
|
| Title | Kudos | Posted |
|---|---|---|
| 1 | 01-09-2020 09:26 AM | |
| 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 |
| Online Status |
Offline
|
| Date Last Visited |
Tuesday
|