|
POST
|
The answers above don't take case into account, which is the variation that you mentioned. You should add a .lower() function to the string. This will evaluate the string as a lowercase version of itself. For example:
if ras.lower().endswith("_pa"):
#rename the raster
Also, I just tested and the split function is case sensitive, so you'll have to watch out for that. What I suggested above works in this way:
name1 = "harrisburg_pa"
name2 = name1[:-3] #remove the last 3 characters, regardless of what they are
name3 = "PA_" + name2 #adds "PA_" to the beginning of the new name
So, regardless of whether or not the raster ends in "_pa", the last three characters will be removed. This may not be desirable, so you may want to filter the rasters by using the if statement that jamesfreddyc suggested. Just be careful because python is case sensitive.
... View more
05-29-2014
06:46 AM
|
0
|
0
|
3450
|
|
POST
|
Here's some info on the correct tool to use: http://help.arcgis.com/en/arcgisdesktop/10.0/help/index.html#//001700000056000000 Here's an example of how to reconstruct the name:
name = "original_pa"
newname = "PA_" + name[:-3]
Hope that solves it!
... View more
05-29-2014
06:01 AM
|
0
|
0
|
3450
|
|
POST
|
Use the green check mark underneath the 0 at right. Cheers!
... View more
05-15-2014
07:44 AM
|
0
|
0
|
1521
|
|
POST
|
I think we had an overkill of responses for this thread. Yeah, I think they all happened simultaneously too.
... View more
05-15-2014
07:03 AM
|
0
|
0
|
1410
|
|
POST
|
Hi Gaspar, what you're getting is a list of field objects, which, when printed out, looks like gibberish. As jamesfreddyc wrote above, you need to access the name property of each field object. You can condense this by using list comprehension:
if "fieldname" in [f.name for f in arcpy.ListFields(featureclass)]:
#do something
Lists and dictionaries are extremely useful, so it's good to get familiar with them https://docs.python.org/2/tutorial/datastructures.html
... View more
05-15-2014
06:56 AM
|
0
|
0
|
1410
|
|
POST
|
Glad to hear that was helpful! Be sure to mark the question as answered if you are satisfied. Good luck!
... View more
05-15-2014
06:08 AM
|
0
|
0
|
1521
|
|
POST
|
Here's the first strategy that comes to mind: 1. Use feature classes in a file geodatabase so every feature has its area automatically calculated 2. Make sure the data is in a projection with a meaningful unit of measure (e.g. meters, not degrees). 3. Clip the land use polygons to the buffer feature class. 4. Iterate through the features in the buffer feature class, selecting each one and using that selection to select the overlapping, clipped landuse polygons in order to get the area values from them and calculate the percentages. Ok, something like:
buffers_path = "path\to\buffers"
clipped_land_use = "path\to\clipped\land\use"
land_use_fl = arcpy.MakeFeatureLayer(clipped_land_use,"lu")
for row1 in arcpy.SearchCursor(buffers_path):
oid = str(row1.getValue("OBJECTID"))
area1 = row1.getValue("SHAPE_area") #presumably, this will be the same for each feature, because you have a standard buffer distance
name = #get a name value from one of the fields if you want
print name
if arcpy.Exists("fl"):
arcpy.management.Delete("fl")
fl = arcpy.management.MakeFeatureLayer(buffers_path,"fl",'"OBJECTID" = "+oid)
arcpy.management.SelectLayerByLocation(land_use_fl,"INTERSECT",fl,"","NEW_SELECTION")
#now iterate through the selected clipped features and print the info
for row2 in arcpy.SearchCursor(land_use_fl):
area2 = row2.getValue("SHAPE_area")
use = #get the land use type from whatever field it's in
percent = area2*100/area1
print " {0}: {1}%".format(use,str(percent))
I haven't tested this code, so there may be a bit of trouble shooting and configuring to do, but it should serve as a decent outline. I only have 10.0, which is why I'm not using da.SearchCursor(), but this should work fine in 10.1/10.2 as well.
... View more
05-14-2014
02:12 PM
|
0
|
0
|
1521
|
|
POST
|
Hi Emil, I was just testing this again, and realized that the line rand_index = random.randrange(0,len(fids_to_pick_from)+1) should be modified to rand_index = random.randrange(0,len(fids_to_pick_from)) that + 1 will cause an error sometimes ("list index out of range"). Here's the other method, these steps should work just fine and it only takes a few seconds to run: 1. right-click on the desktop to make a new text file (.txt) and name it whatever you want. 2. change the file extension to ".py" from ".txt" 3. right-click on the file and "Edit in IDLE" 4. paste in the code, and enter the correct path for the shapefile with the polygons. 5. press F5 or click Run > Run Module 6. you should get the query when the script is done (takes only a couple of seconds) which you can paste into ArcMap, as a definition query or a Select By Attributes query. 7. just rerun the module as many times as you want for new sets of 50 random polygons.
... View more
04-28-2014
06:34 AM
|
0
|
0
|
1313
|
|
POST
|
Ok. I was working on a reply assuming a single feature class, but I've modified it so this should be fine. What I have in mind uses values from the FID field, SelectLayerByLocation and the randrange() function from the random module. I'll write it as a standalone script, but it wouldn't be hard to create a custom tool from it. I'll add a lot of comments so it should be clear what's going on. I got a little carried away. import random import arcpy ##a couple of quick useful functions def MakeQuery(fid_list): templist = ['"FID" = ' + str(fid) for fid in fid_list] return " OR ".join(templist) def TakeOutTrash(dataset): if arcpy.Exists(dataset): arcpy.management.Delete(dataset) ##first get the feature class, and make feature layer for later use shp = r"C:\CLI_GIS\Intermountain Region\GRTE\850491\New_Shapefile.shp" TakeOutTrash("fl1") flayer = arcpy.management.MakeFeatureLayer(shp,"fl1") ##begin with list of all FIDs rows = arcpy.SearchCursor(shp) fids_to_pick_from = [row.getValue("FID") for row in rows] del rows #make empty list to hold the fids you'll use for final selection use_fids = [] ##use while loop to do the iterative selection process while True: ##get random index number for fid list which will point to a valid id rand_index = random.randrange(0,len(fids_to_pick_from)+1) rand_fid = fids_to_pick_from[rand_index] use_fids.append(rand_fid) fids_to_pick_from.remove(rand_fid) ##make feature layer using fids picked up to this point q = MakeQuery(use_fids) TakeOutTrash("fl2") fl = arcpy.management.MakeFeatureLayer(shp,"fl2",q) ##selection by location to find which of the original features overlap with the polygons ##in the use_fids list thus far, then remove them from the fids_to_pick_from list arcpy.management.SelectLayerByLocation(flayer,"INTERSECT",fl,"","NEW_SELECTION") if not arcpy.management.GetCount(flayer).getOutput(0) ==\ arcpy.management.GetCount(fl).getOutput(0): rows = arcpy.SearchCursor(flayer) all_overlapping = [row.getValue("FID") for row in rows] del rows bad_overlapping = [i for i in all_overlapping if not i in use_fids] fids_to_pick_from = [f for f in fids_to_pick_from if not f in bad_overlapping] ##break loop once 50 fids have been selected if len(use_fids) == 50: break ##make final selection final_q = MakeQuery(use_fids) arcpy.management.SelectLayerByAttribute(flayer,"NEW_SELECTION",final_q) print str(len(use_fids)) + " polygons have been chosen" print final_q I'm finding that running this in the python console takes forever. It's much faster to create a little stand alone script from it, and just copy and paste the printed query at the end into the select by attribute dialogue box in ArcMap. At any rate, this can serve as the core of the script.
... View more
04-24-2014
08:42 AM
|
0
|
0
|
1313
|
|
POST
|
Hi emil, How are these polygons stored, in one feature class, in a variety of feature classes, shapefiles, etc.? This will dramatically change the code, but it is certainly doable.
... View more
04-24-2014
07:03 AM
|
0
|
0
|
1313
|
|
POST
|
I think your best bet is to just right-click the layer in the table of contents that you showed in the attachment, and then save it to a .lyr file. Then you can reference that .lyr file to symbolize any subsequent products of the tool. Try using the ApplySymbologyFromLayer tool, etc. Should work on any feature class/shp that has a GiZScore field that is the right type (float or whatever).
... View more
04-23-2014
10:53 AM
|
0
|
0
|
1212
|
|
POST
|
Looks like in 10.1 (though not in 10.0) this can be controlled as a property of the env class. http://resources.arcgis.com/en/help/main/10.1/index.html#/env/018z0000004s000000/
... View more
04-03-2014
01:31 PM
|
0
|
0
|
1175
|
|
POST
|
When you write: ParLyr = arcpy.MakeFeatureLayer_management(Parcellyr, "Parcel layer") #result object ParLyr is a result object, not a string or feature class object. Try using the getOutput method to make ParLyr a layer object. Then, later on, you can get the layer's name by using ParLyr.name. That will be a string. ParLyr = arcpy.MakeFeatureLayer_management(Parcellyr, "Parcel layer").getOutput(0) #layer object ParLyr_name = ParLyr.name #string object Here's some more info http://resources.arcgis.com/en/help/main/10.1/index.html#//018z00000046000000
... View more
04-03-2014
01:28 PM
|
0
|
0
|
1247
|
|
POST
|
It would be good if you could use the code tags when you post your question, so your indentation is correctly displayed. For example:
for row in rows:
a = row[0]
It's especially important to see indentations when the question refers to loops and stuff like that. Just click the # button and place the formatted text between the tags.
... View more
04-03-2014
08:47 AM
|
0
|
0
|
1432
|
|
POST
|
Yeah, you'll have to make that modification within the function itself, not after you call it later in the script. The following code should work. One note, an empty definition query doesn't equal False, it's just an empty string, "".
#just copy and paste this into the corresponding place in the SetDefQuery function
if layer.supports("DEFINITIONQUERY"):
fixed_layers.append(layer.name)
#get existing definition query
exdefquery = layer.definitionQuery
#if the existing query is empty, set the normal defquery
if exdefquery == "":
layer.definitionQuery = defquery
#otherwise, make a combination defquery
else:
layer.definitionQuery = "{0} AND {1}".format(exdefquery, defquery)
... View more
04-03-2014
06:49 AM
|
0
|
0
|
1920
|
| Title | Kudos | Posted |
|---|---|---|
| 1 | 04-03-2015 07:41 AM | |
| 1 | 04-02-2015 03:42 PM | |
| 3 | 08-14-2014 09:58 AM | |
| 1 | 08-12-2014 07:10 AM |
| Online Status |
Offline
|
| Date Last Visited |
11-11-2020
02:24 AM
|