POST
|
I'm running this as a script from a toolbox; sorry, forgot to mention that initially. I made a slight change to your code, and the rasters load correctly as you said they would. I used the path including the file name as the first parameter to MakeRasterLayer rather than just the dir name. This also worked nicely to load mosaic datasets. field = 'FILENAME'
cursor = arcpy.SearchCursor(myRefLayer)
for row in cursor:
path = row.getValue(field)
# path = os.path.dirname(fullpath)
name = os.path.basename(path)
addLayer = arcpy.management.MakeRasterLayer(path, name)[0]
arcpy.mapping.AddLayer(df, addLayer, 'TOP') A couple of follow-up questions... When do I code using arcpy.management.<function>() versus arcpy.<function>_management() syntax? Eventually I'll want to put this code in an one of my addIns. Should I expect it to work pretty much as is? Thank you, Scott
... View more
10-17-2013
11:34 AM
|
0
|
0
|
475
|
POST
|
I tried replacing this: field = 'FILENAME'
cursor = arcpy.SearchCursor(myRefLayer)
for row in cursor:
path = row.getValue(field)
addLayer = arcpy.mapping.Layer(path)
arcpy.mapping.AddLayer(df, addLayer, 'TOP')
with this: field = 'FILENAME'
cursor = arcpy.SearchCursor(myRefLayer)
for row in cursor:
path = row.getValue(field)
name = os.path.basename(path)
arcpy.MakeRasterLayer_management(path, name)
The former loads the imagery with the weird colors; the latter doesn't load any images. Is there something additional required with the MakeRasterLayer function? It didn't seem so from the help pages. I did try adding a RefreshTOC(), but that didn't change the result. Thank you, Scott
... View more
10-17-2013
08:43 AM
|
0
|
0
|
475
|
POST
|
Hello, My arcpy script uses the arcpy.mapping.AddLayer() function to add imagery raster datasets to the TOC. However, the symbology is defaulted to blue stretched rather than the appropriate RGB or grey stretched for color or b&w images respectively. This doesn't happen if I drag-and-drop from Arc Catalog, a windows folder, or use the add data button. Any idea why this happens? Thank you, Scott
... View more
10-16-2013
02:38 PM
|
0
|
6
|
1114
|
POST
|
OK, The following code works ONLY IF coordinate system of my data frame and the target selection layer are the same. Note there is no explicit mention of spatial reference for env.outputCoordinateSystem or the instantiation of the PointGeometry; it's handled implicitly.
import arcpy
mxd = arcpy.mapping.MapDocument("CURRENT")
df = arcpy.mapping.ListDataFrames(mxd)[0]
myPt = arcpy.Point((df.extent.XMax + df.extent.XMin)/2.0, (df.extent.YMax + df.extent.YMin)/2.0)
myPtGeometry = arcpy.PointGeometry(myPt)
arcpy.SelectLayerByLocation_management('DOQQImport_prj', 'INTERSECT', myPtGeometry)
del df, mxd The following code ALMOST works when the coordinate system of my data frame and the target selection layer are different. If I zoom to layer on the target layer, it will select the correct polygons. If I THEN change the scale of the data frame, it still works. But if I pan to a different location it doesn't work.
import arcpy
mxd = arcpy.mapping.MapDocument("CURRENT")
df = arcpy.mapping.ListDataFrames(mxd)[0]
arcpy.env.outputCoordinateSystem = df.spatialReference
myPt = arcpy.Point((df.extent.XMax + df.extent.XMin)/2.0, (df.extent.YMax + df.extent.YMin)/2.0)
myPtGeometry = arcpy.PointGeometry(myPt, df.spatialReference)
arcpy.SelectLayerByLocation_management('DOQQImport_prj', 'INTERSECT', myPtGeometry)
del df, mxd Scott
... View more
07-02-2013
01:27 PM
|
0
|
0
|
186
|
POST
|
I'm currently using 10.1 SP 1. Ahm.. Gentlemen, in the code above I still do not see you doing this - Code: myPtGeometry = arcpy.PointGeometry(myPt, sr) Even though I didn't post code with pointGeometry(myPoint, sr), I did try that with a variety of coordinate system setups and with and without the env.outputCoordinateSystem setting. No combination has worked when running the script from a toolbox. Incidentally, my intention is to run this from an add-in toolbar which I tried today. It doesn't run there either. I also tried importing my toolbox to the python window and running it by calling interactively. That also didn't run. Only pasting the code directly to the python prompt in the python window runs correctly. In all cases I'm using the identical code. Just to confirm, you are running the script tool within an ArcMap document and when you check the properties of the tool it is checked 'in process'? Yes. Did you add the 2 lines for GetCount and to report back to you the number of features selected? Then, if a feature selection is reported, are you 'zoomed in' too far to see the selected outline? I tried this, and 0 is my result. Maybe it's a 10.1 issue. I'll see if I've got 10.0 available on some machine at work. Scott
... View more
06-12-2013
02:30 PM
|
0
|
0
|
637
|
POST
|
I've been running this as a script in a toolbox. I tried again here at home starting from scratch and get the same result, no selection. Then I opened the python window from the geoprocessing menu and pasted the code in there as is and it successfully selected some polygons! So why does it work in the python window, but not as a script? Thanks, Scott
... View more
06-11-2013
07:15 PM
|
0
|
0
|
637
|
POST
|
I've attached a snippet of the DOQQImport layer. They should show up around the Minneapolis area. Scott
... View more
06-11-2013
12:44 PM
|
0
|
0
|
637
|
POST
|
DOQQImport is a polygon layer whose polygons represent footprints of DOQQs.
... View more
06-11-2013
12:19 PM
|
0
|
0
|
637
|
POST
|
Greetings and thanks for the help so far. I had high hopes regarding the spatial reference issue, but I'm still not getting this to work. I've got the code updated to match Wayne's example. import arcpy
mxd = arcpy.mapping.MapDocument("CURRENT")
df = arcpy.mapping.ListDataFrames(mxd)[0]
DOQQImport = arcpy.mapping.ListLayers(mxd, 'DOQQImport', df)[0]
sr_dataframe = df.spatialReference
sr_featureclass = arcpy.Describe(DOQQImport).spatialReference
arcpy.env.outputCoordinateSystem = sr_dataframe
myPt = arcpy.Point((df.extent.XMax + df.extent.XMin)/2.0, (df.extent.YMax + df.extent.YMin)/2.0)
myPtGeometry = arcpy.PointGeometry(myPt)
##fc = 'points'
##cursor = arcpy.da.InsertCursor(fc, ["SHAPE@XY"])
##xy =(myPt.X, myPt.Y)
##cursor.insertRow([xy])
arcpy.AddMessage("\nThe fetched polygon layer from the TOC, {0}".format(DOQQImport.name))
arcpy.AddMessage("\nThe data frame spatial reference name: {0}".format(sr_dataframe.name))
arcpy.AddMessage("\nThe feature class (to select by) spatial reference name: {0}".format(sr_featureclass.name))
arcpy.AddMessage("\nThe data frame centerpoint (myPt) - X: {0} Y: {1}".format(myPt.X, myPt.Y))
arcpy.AddMessage("\nThe geometry (myPtGeometry) - {0} {1}".format(myPtGeometry.firstPoint.X, myPtGeometry.firstPoint.Y))
arcpy.AddMessage("\n...now proceeding with the selection, please wait...\n")
arcpy.SelectLayerByLocation_management(DOQQImport, 'INTERSECT', myPtGeometry)
arcpy.RefreshActiveView()
del df, mxd
It successfully runs; here's my output: [INDENT]Executing: gotoSiteLatLong Start Time: Tue Jun 11 14:38:19 2013 Running script gotoSiteLatLong... The fetched polygon layer from the TOC, DOQQImport The data frame spatial reference name: NAD_1983_UTM_Zone_15N The feature class (to select by) spatial reference name: GCS_WGS_1984 The data frame centerpoint (myPt) - X: 466834.903914 Y: 4975689.29185 The geometry (myPtGeometry) - 466834.903914 4975689.29185 ...now proceeding with the selection, please wait... Completed script gotoSiteLatLong... Succeeded at Tue Jun 11 14:38:24 2013 (Elapsed Time: 5.00 seconds)[/INDENT] However, I still don't get any selected polygons. I tried changing the data frame coordinate system to match the feature class with similar results. Using the commented out code, ##fc = 'points'
##cursor = arcpy.da.InsertCursor(fc, ["SHAPE@XY"])
##xy =(myPt.X, myPt.Y)
##cursor.insertRow([xy])
I was able to add a point at the center of my current extent, so my geometry is indeed creating a point in the correct location. And there are polygons in my feature class (DOQQImport) that cover the point. I'm stumped as to what I'm missing. Scott
... View more
06-11-2013
11:49 AM
|
0
|
0
|
637
|
POST
|
Hello! I tested making a reference as suggested with no change in results - nothing gets selected. I don't believe the "DOQQImport" needs to be referenced. The layer is in the TOC, and I've used layers in other scripts without making such a reference. As a test I tried the following. I used an existing point layer ("somePtLayer") and manually selected one of its features that is covered by my DOQQImport polygon layer. I changed the code to use the point layer and the script successfully selected the correct polygons. import arcpy
mxd = arcpy.mapping.MapDocument("CURRENT")
df = arcpy.mapping.ListDataFrames(mxd)[0]
##myPt = arcpy.Point((df.extent.XMax+df.extent.XMin)/2, (df.extent.YMax+df.extent.YMin)/2)
##arcpy.AddMessage("{0} {1}".format(myPt.X, myPt.Y))
##myPtGeometry = arcpy.PointGeometry(myPt)
##arcpy.AddMessage("{0} {1}".format(myPtGeometry.firstPoint.X, myPtGeometry.firstPoint.Y))
##arcpy.SelectLayerByLocation_management ("DOQQImport", "COMPLETELY_CONTAINS", myPtGeometry)
arcpy.SelectLayerByLocation_management ("DOQQImport", "COMPLETELY_CONTAINS", "somePtLayer") I'm thinking that while geometries can be used for geoprocessing, maybe they can't be used this way to make selections. Technically, selectByLocation isn't geoprocessing. Thanks, Scott
... View more
06-10-2013
02:59 PM
|
0
|
0
|
1473
|
POST
|
I updated my code to include printing out x and y values of myPt and myPtGeometry with an AddMessage (I'm running the script from a toolbox). Both are correct, matching what I read on the screen with my cursor at the center of my data frame. import arcpy
mxd = arcpy.mapping.MapDocument("CURRENT")
df = arcpy.mapping.ListDataFrames(mxd)[0]
myPt = arcpy.Point((df.extent.XMax+df.extent.XMin)/2, (df.extent.YMax+df.extent.YMin)/2)
arcpy.AddMessage("{0} {1}".format(myPt.X, myPt.Y))
myPtGeometry = arcpy.PointGeometry(myPt)
arcpy.AddMessage("{0} {1}".format(myPtGeometry.firstPoint.X, myPtGeometry.firstPoint.Y))
arcpy.SelectLayerByLocation_management ("DOQQImport", "COMPLETELY_CONTAINS", myPtGeometry)
The layer I'm trying this with is in a group. I tried moving it outside the group, but still nothing gets selected.
... View more
06-07-2013
02:51 PM
|
0
|
0
|
1473
|
POST
|
Greetings, I'm writing some arcpy to select polygons that contain the centroid of my current data frame extent. Here's my code: import arcpy
mxd = arcpy.mapping.MapDocument("CURRENT")
df = arcpy.mapping.ListDataFrames(mxd)[0]
myPt = arcpy.Point((df.extent.XMax+df.extent.XMin)/2, (df.extent.YMax+df.extent.YMin)/2)
myPtGeometry = arcpy.PointGeometry(myPt)
arcpy.SelectLayerByLocation_management ("DOQQImport", "COMPLETELY_CONTAINS", myPtGeometry)
The code runs without error, and the coordinates of myPt/myPtGeometry are correct. Yet, nothing gets selected. Am I missing some coding step, or can you not use geometry this way? Thanks, Scott
... View more
06-07-2013
12:44 PM
|
0
|
21
|
9788
|
POST
|
Hi Folks, The following arcpy script adds a raster image to the TOC: import arcpy
mxd = arcpy.mapping.MapDocument("CURRENT")
df = arcpy.mapping.ListDataFrames(mxd)[0]
r = arcpy.mapping.Layer('C:/myimage.sid')
arcpy.mapping.AddLayer(df, r, "TOP")
It adds images successfully but defaults them all to an unacceptable symbology. [ATTACH=CONFIG]23085[/ATTACH] [ATTACH=CONFIG]23086[/ATTACH] Any suggestions on how to get the images to load with appropriate colors, i.e. grey scale or rgb? Thanks, Scott
... View more
03-29-2013
01:44 PM
|
0
|
1
|
2571
|
POST
|
I see; my bad. Got it to work. Thank you very much!! Scott
... View more
12-19-2011
01:19 PM
|
0
|
0
|
280
|
Title | Kudos | Posted |
---|---|---|
1 | 02-02-2011 04:35 PM |
Online Status |
Offline
|
Date Last Visited |
11-11-2020
02:23 AM
|