|
POST
|
This one works. I spent an hour trying to figure out how to manage the quotes properly in my expression! # --------------------------------------------------------------------------- # adjacent.py # Created on: 2012-07-12 15:14:17.00000 # (generated by ArcGIS/ModelBuilder) # Usage: adjacent <countyName> # Description: # --------------------------------------------------------------------------- # Import arcpy module import arcpy # Set Geoprocessing environments arcpy.env.scratchWorkspace = "C:\\ESRItest\\model\\process.gdb" arcpy.env.workspace = "C:\\ESRItest\\model\\process.gdb" #overwrite pre-existing files arcpy.env.overwriteOutput = True # Script arguments #countyName = arcpy.GetParameterAsText(0) countyName = raw_input('What County do you want to select?') # Local variables: county = "county" county_Layer = "county_Layer" countyList = [] txt_list = "" #Output_Feature_Class = "C:\\ESRItest\\model\\process.gdb\\county_CopyFeaturesOutput" # Process: Make Feature Layer county_Layer = arcpy.MakeFeatureLayer_management("C:\ESRItest\model\process.gdb\county", county_Layer, "", "", "OBJECTID OBJECTID VISIBLE NONE;Shape Shape VISIBLE NONE;NAME NAME VISIBLE NONE;CNTY_FIPS CNTY_FIPS VISIBLE NONE;FIPS FIPS VISIBLE NONE;POP2005 POP2005 VISIBLE NONE;POP05_SQMI POP05_SQMI VISIBLE NONE;SQMI SQMI VISIBLE NONE;NAME2 NAME2 VISIBLE NONE;Shape_Length Shape_Length VISIBLE NONE;Shape_Area Shape_Area VISIBLE NONE") #Process: Select Layer By Attributes #print countyName whereClause=" \"NAME\" = " + "'"+countyName+"'" #print whereClause countySelection = arcpy.SelectLayerByAttribute_management(county_Layer, "NEW_SELECTION", whereClause) # Process: Select Layer By Location results = arcpy.SelectLayerByLocation_management(countySelection, "BOUNDARY_TOUCHES", "", "", "NEW_SELECTION") # Process: Copy Features #arcpy.CopyFeatures_management(county_Layer, Output_Feature_Class, "", "0", "0", "0") ## Step through the selection to get the values from the name field rows = arcpy.SearchCursor(results,"","","NAME","NAME") for row in rows: neighborVal = row.getValue("NAME") if neighborVal != countyName: countyList.append(neighborVal) txt_list = ','.join(countyList) print txt_list I still need to figure out how in the world to manage this as a call from Java. At least I can give input and receive output from within the Python window. Thanks so much Chris for your assistance!!!!!
... View more
07-20-2012
11:17 AM
|
0
|
0
|
4750
|
|
POST
|
The input worked fine in that I got a prompt. Since I'd been using countyName as my varable I just set it as countyName = raw_input('What County do you want to select?') When I tried your expression as-is, countySelection = arcpy.SelectLayerByAttribute_management(county_Layer, "NEW_SELECTION", "\"NAME\" = countyName") it told me I had a syntax error: Traceback (most recent call last): File "C:\ESRItest\model\adjacent.py", line 36, in <module> countySelection = arcpy.SelectLayerByAttribute_management(county_Layer, "NEW_SELECTION", "\"NAME\" = countyName") File "C:\Program Files\ArcGIS\Desktop10.0\ArcPy\arcpy\management.py", line 4259, in SelectLayerByAttribute raise e ExecuteError: ERROR 000358: Invalid expression Failed to execute (SelectLayerByAttribute). I figured it wanted some quotes, so I changed it to countySelection = arcpy.SelectLayerByAttribute_management(county_Layer, "NEW_SELECTION", "\"NAME\" = 'countyName'") This didn't error, but it didn't return anything either! Thanks for the suggestion of another thread for a question that is only vaguely related to mine!
... View more
07-20-2012
08:47 AM
|
0
|
0
|
2048
|
|
POST
|
I did realize somewhere along the way that I couldn't use a featureclass as input. I'm getting confused on whether I should run this from the python shell or if I can run it as a script in a toolbox. If I run it as a script, it gives me the prompt I expect for the name, but it doesn't seem to return anything. There aren't any errors, either. I'm not sure there's even a place for my results to go since it's just returning a list and not anything like a layer or table. If I run it through the shell, then when do I specify my input string to search on? Also I'm wondering about the syntax of my expression in select by attributes. When I tried hard coding in a county name "NAME" = 'Holt', it created my output list (YEAH!). But when I put back it back in as a variable, it doesn't list anything (BOO!) I had a few lines in there for CopyFeatures, but I don't really think I need that, so I commented it out. Since select by attribute isn't finding anything, then select by location logically isn't either and I end up finding nothing. # ---------------------------------------------------------------------------
# adjacent.py
# Created on: 2012-07-12 15:14:17.00000
# (generated by ArcGIS/ModelBuilder)
# Usage: adjacent <countyName>
# Description:
# ---------------------------------------------------------------------------
# Import arcpy module
import arcpy
# Set Geoprocessing environments
arcpy.env.scratchWorkspace = "C:\\ESRItest\\model\\process.gdb"
arcpy.env.workspace = "C:\\ESRItest\\model\\process.gdb"
#overwrite pre-existing files
arcpy.env.overwriteOutput = True
# Script arguments
countyName = arcpy.GetParameterAsText(0)
if countyName == '#' or not countyName:
countyName = "Boone" # provide a default value if unspecified
# Local variables:
county = "county"
county_Layer = "county_Layer"
countyList = []
txt_list = ""
#Output_Feature_Class = "C:\\ESRItest\\model\\process.gdb\\county_CopyFeaturesOutput"
# Process: Make Feature Layer
county_Layer = arcpy.MakeFeatureLayer_management("C:\ESRItest\model\process.gdb\county", county_Layer, "", "", "OBJECTID OBJECTID VISIBLE NONE;Shape Shape VISIBLE NONE;NAME NAME VISIBLE NONE;CNTY_FIPS CNTY_FIPS VISIBLE NONE;FIPS FIPS VISIBLE NONE;POP2005 POP2005 VISIBLE NONE;POP05_SQMI POP05_SQMI VISIBLE NONE;SQMI SQMI VISIBLE NONE;NAME2 NAME2 VISIBLE NONE;Shape_Length Shape_Length VISIBLE NONE;Shape_Area Shape_Area VISIBLE NONE")
#Process: Select Layer By Attributes
countySelection = arcpy.SelectLayerByAttribute_management(county_Layer, "NEW_SELECTION", "\"NAME\" = '%countyName%' ")
# Process: Select Layer By Location
results = arcpy.SelectLayerByLocation_management(countySelection, "INTERSECT", "", "", "NEW_SELECTION")
# Process: Copy Features
#arcpy.CopyFeatures_management(county_Layer, Output_Feature_Class, "", "0", "0", "0")
## Step through the selection to get the values from the name field
rows = arcpy.SearchCursor(results,"","","NAME","NAME")
for row in rows:
neighborVal = row.getValue("NAME")
countyList.append(neighborVal)
txt_list = ','.join(countyList)
print txt_list It feels very close! I am happy to see those county names listed out in my shell window, even if they aren't yet working when I use a variable instead.
... View more
07-19-2012
12:25 PM
|
0
|
0
|
2048
|
|
POST
|
My original thought process was the one that Kevin is describing. The request that was made to us was "we want to make a call to GIS with a school district number and get returned back to our program as a list of adjacent school district numbers in a comma delimited format". My sample code is county boundaries instead, because I had those already created. (The school districts are in the process of being updated. ) When I was first handed this request, I thought it was more map centric and wrote a small FLEX app to do this (my only area of real programming expertise). However, they don't WANT to interact with the map. Per the last conversation we had "They just want a list". Even Copy Rows is going to require them to parse the JSON output to retrieve just the district ID from each of the rows. I figure they are going to have to figure out themselves how to parse what GIS can output. Part of my problem seems to be that my original code doesn't seem to be selecting anything when I run it as Python. I need to go back to my original model and see if it was truly executing. It's just a select by attribute with that output passed as the input to select by location - it ought to be a piece of cake!
... View more
07-19-2012
05:30 AM
|
0
|
0
|
2048
|
|
POST
|
I was able to get my python install problem taken care of. Tech support recommended a repair on the install. I probably could have just fixed it by just editing the system variable for PYTHONPATH. Anyway, I'm back in business. My code doesn't seem to actually return anything. In my example, I'm just using county boundaries, because there are fewer of those and I have the data at hand. I need to do some more reading on debugging. I don't see that the array I created even gets any values in my searchCursor. The selections by attribute and by location seem to be occuring. # ---------------------------------------------------------------------------
# adjacent.py
# Created on: 2012-07-12 15:14:17.00000
# (generated by ArcGIS/ModelBuilder)
# Usage: adjacent <countyName>
# Description:
# ---------------------------------------------------------------------------
# Import arcpy module
import arcpy
# Set Geoprocessing environments
arcpy.env.scratchWorkspace = "C:/ESRItest/model/process.gdb"
arcpy.env.workspace = "C:/ESRItest/model/process.gdb"
#overwrite pre-existing files
arcpy.env.overwriteOutput = True
# Script arguments
countyName = arcpy.GetParameterAsText(0)
if countyName == '#' or not countyName:
countyName = "Boone" # provide a default value if unspecified
# Local variables:
selected_County = countyName
Adjacent_Counties = selected_County
#county = "county"
county = "C:/ESRItest/model/process.gdb/county"
Output_Layer = "county_Layer"
countList = [] # I added this
countyField = "NAME" #I added this
county_lyr = "C:/ESRItest/model/county.lyr"
# Process: Make Feature Layer
arcpy.MakeFeatureLayer_management(county, Output_Layer, "", "", "OBJECTID OBJECTID VISIBLE NONE;Shape Shape VISIBLE NONE;NAME NAME VISIBLE NONE;CNTY_FIPS CNTY_FIPS VISIBLE NONE;FIPS FIPS VISIBLE NONE;POP2005 POP2005 VISIBLE NONE;POP05_SQMI POP05_SQMI VISIBLE NONE;SQMI SQMI VISIBLE NONE;NAME2 NAME2 VISIBLE NONE;Shape_Length Shape_Length VISIBLE NONE;Shape_Area Shape_Area VISIBLE NONE")
# Process: Select Layer By Attribute
selection = arcpy.SelectLayerByAttribute_management(Output_Layer, "NEW_SELECTION", "\"NAME\" = '%countyName%'")
# Process: Select Layer By Location
results = arcpy.SelectLayerByLocation_management(selection, "BOUNDARY_TOUCHES", "", "", "NEW_SELECTION")
# Process: Select Layer By Attribute
#arcpy.SelectLayerByAttribute_management("C:/ESRItest/model/process.gdb/county", "NEW_SELECTION", "\"NAME\" = '%countyName%'")
## I added this section 7-13-12
rows = arcpy.SearchCursor(results,"","","NAME","NAME")
for row in rows:
neighborVal = row.getValue("NAME")
print(neighborVal)
countyList.append(neighborVal)
print countyList
#txt_list = ','.join(countyList)
... View more
07-18-2012
07:44 AM
|
0
|
0
|
2703
|
|
POST
|
I was actually thinking of it the other way. I have a geoprocessing service that is built from Python that would be called from another program. I've used the other ArcGIS Server tasks like findTask, queryTask, locatorTask. For those, you have a service that you can pass it a parameter and what it returns in in the form of JSON or HTML. Once you start creating your own geoprocessing service, I assume whatever is returned is going to be determined more from what output or results you have from the script. I have to take a step back for the moment because I have two different versions of Python on my machine and they seem to be conflicting with each other!
... View more
07-13-2012
09:53 AM
|
0
|
0
|
2703
|
|
POST
|
That helps, thanks. All I really need is the list of values and I don't want it in an output file or anything, I want it to be the result of a call to a geoprocessing service. I'm still trying to wrap my head around how to get the list to be the result of my geoprocessing call. I'm not sure where to even post this question, it's touching on script syntax and ArcGIS Server geoprocessing services. From my searching, I'm not finding a lot of examples, but it still seems like it should be possible. # ---------------------------------------------------------------------------
# adjacent.py
# Created on: 2012-07-12 15:14:17.00000
# (generated by ArcGIS/ModelBuilder)
# Usage: adjacent <countyName>
# Description:
# ---------------------------------------------------------------------------
# Import arcpy module
import arcpy
# Set Geoprocessing environments
arcpy.env.scratchWorkspace = "C:/ESRItest/model/process.gdb"
arcpy.env.workspace = "C:/ESRItest/model/process.gdb"
# Script arguments
countyName = arcpy.GetParameterAsText(0)
if countyName == '#' or not countyName:
countyName = "Boone" # provide a default value if unspecified
# Local variables:
selected_County = countyName
Adjacent_Counties = selected_County
county = "county"
countList = [] # I added this
countyField = "NAME" #I added this
# Process: Select Layer By Attribute
arcpy.SelectLayerByAttribute_management(county, "NEW_SELECTION", "\"NAME\" = '%countyName%'")
# Process: Select Layer By Location
results = arcpy.SelectLayerByLocation_management(selected_County, "BOUNDARY_TOUCHES", "", "", "NEW_SELECTION")
## I added this section 7-13-12
rows = arcpy.SearchCursor(results,"","","NAME")
for row in rows:
neighborVal = row.NAME
print(neighborVal)
countyList.append(neighborVal)
txt_list = ','.join(countyList)
print txt_list
... View more
07-13-2012
08:26 AM
|
0
|
0
|
2703
|
|
POST
|
So where is select_by_location_layer defined? I was thinking I would need to set a variable on the Select Layer by Location, which is why I changed the line below to have results = in it. result = arcpy.SelectLayerByLocation_management(selected_County, "BOUNDARY_TOUCHES", "", "", "NEW_SELECTION")
... View more
07-13-2012
07:55 AM
|
0
|
0
|
2703
|
|
POST
|
I have been asked to create a service that will be consumed by a non-GIS application. The programmer wants to be able to "make a call to GIS" providing the number of a school district as input. They want the service to return just a list of districts that surround the input number. Their application will be written in Java, which I know nothing about. I have considered publishing a model as a geoprocessing service and I have looked at Python. It seems like I might have more luck in Python, which I've never used before, because at least I could make an array or list of values from the attributes? I'm not finding much documentation on publishing a Python script as a geoprocessing service. I assume I can do this? I started by creating a model that had the tools select by attribute and select by location. Then I exported this to Python. It executes OK when I'm in ArcMap. I'm not sure where to go from there. I know I need to examine each polygon that is the result of the select by location query and find just the attribute of the district field. There is no desire to view or create a map in this application. They just want a list containing the numbers of the adjacent districts. Even after I get the list, I'm still not sure what I might need to do to get this to work properly as an ArcGIS Server geoprocessing service. I am still at version 10.0. # --------------------------------------------------------------------------- # adjacent.py # Created on: 2012-07-12 15:14:17.00000 # (generated by ArcGIS/ModelBuilder) # Usage: adjacent <countyName> # Description: # --------------------------------------------------------------------------- # Import arcpy module import arcpy # Set Geoprocessing environments arcpy.env.scratchWorkspace = "C:\\ESRItest\\model\\process.gdb" arcpy.env.workspace = "C:\\ESRItest\\model\\process.gdb" # Script arguments countyName = arcpy.GetParameterAsText(0) if countyName == '#' or not countyName: countyName = "Boone" # provide a default value if unspecified # Local variables: selected_County = countyName Adjacent_Counties = selected_County county = "county" # Process: Select Layer By Attribute arcpy.SelectLayerByAttribute_management(county, "NEW_SELECTION", "\"NAME\" = '%countyName%'") # Process: Select Layer By Location result = arcpy.SelectLayerByLocation_management(selected_County, "BOUNDARY_TOUCHES", "", "", "NEW_SELECTION") ## NOW WHAT?
... View more
07-13-2012
06:03 AM
|
0
|
19
|
7417
|
|
POST
|
I don't know if it is the fact I have a featureLayer or not. I was off a couple of days. When I opened this code up again today, it started working. Then it stopped. Now it sometimes highlights a row and sometimes not?! Maybe it's having a hard time keeping up? It continues to work with clicking a row to highlight the feature, but not the other way.
... View more
07-09-2012
12:51 PM
|
0
|
0
|
972
|
|
POST
|
Most of my projects have well under 1000 points. Switching to SNAPSHOT seems to have taken care of clicking on the grid to find the point. I didn't like the map just centering , since I'm dealing with the whole state. Instead I'm using centerAndZoom. Clicking on the point and highlighting the record still isn't working though. Like before, the row variable still as data undefined and element null. The dgrid reference is still pretty sparse.
... View more
07-05-2012
12:20 PM
|
0
|
0
|
972
|
|
POST
|
I had the symbol changed and I would have gotten to the polygon vs point issue with the geometry. The place I was getting stuck was the line for query.objectIds. I changed what I saw as a problem in your selectState function, to define fl as getLayer("blockpoints"). This was originally the featureLayer with the ID of states, which doesn't exist is this modified version. All these changes do let me select a grid row and highlight on the map. It still doesn't work the other way, I can't select a point and have the row highlight in the grid. When I look at the value for the row variable defined in var row = grid.row(g.attributes.OBJECTID), it doesn't look to contain all the information needed to correctly select the row. There is a row returned, but its data says undefined and its element is null. This means it doesn't get past the if statement if ( row && row.data && row.data.featureId == id ) { I don't see that it's working in your example either.
... View more
07-05-2012
05:58 AM
|
0
|
0
|
972
|
|
POST
|
I'm trying to work through the sample http://help.arcgis.com/en/webapi/javascript/arcgis/demos/fl/fl_dgrid.html that ties a feature layer to the new dgrid. It works fine for me with polygons, but I can't seem to get this to work with points. The featurelayer draws fine. The grid populates just fine. In the selectGrid function when I debug, the row variable has a null for both its data and its element. It does have an ID number which looks to be OBJECTID. In the grid, where are row.data and row.element coming from? I assume somehow assigned in the populateGrid function? Here's my populateGrid: function populateGrid() {
var qt = new esri.tasks.QueryTask(window.statesUrl);
var query = new esri.tasks.Query();
query.where = "1=1";
query.returnGeometry = false;
query.outFields = window.outFields;
qt.execute(query, function(results) {
var data = dojo.map(results.features, function(feature) {
return {
"featureId": feature.attributes[window.outFields[0]],
"facility": feature.attributes[window.outFields[1]],
"address": feature.attributes[window.outFields[2]],
"city": feature.attributes[window.outFields[3]],
"state": feature.attributes[window.outFields[4]]
}
});
window.grid.renderArray(data);
window.grid.sort('facility');
});
} Here's my selectGrid function: function selectGrid(e) {
console.log("fl click: ", e.graphic.attributes.OBJECTID);
var id = e.graphic.attributes.OBJECTID;
var query = new esri.tasks.Query();
query.objectIds = [e.graphic.attributes.OBJECTID];
var states = map.getLayer("points");
states.selectFeatures(query, esri.layers.FeatureLayer.SELECTION_NEW);
grid.clearSelection();
// dojo.forEach(states.graphics, function(g) {
for ( var i = 0; i < states.graphics.length; i++ ) {
var g = states.graphics;
var row = grid.row(g.attributes.OBJECTID);
// console.log("row is: ", row, row.data.featureId);
if ( row && row.data && row.data.featureId == id ) {
// grid.select(row);
selectRow(row.element);
row.element.scrollIntoView();
break;
}
};
} When I put a breakpoint on the row variable, Firebug shows this as data undefined and element as null. So the selectRow function that follows errors out. When examing the query variable, I see it has a spatialRelationship of esriSpatialRelIntersects. Is that where the problem is? Do I need to define something different for the query since it's not a polygon? I've not used a query.ObjectIds before. My featureLayer does show that I have some selected features so I figure it's finding something, but maybe it's really not?
... View more
07-03-2012
12:33 PM
|
0
|
7
|
1914
|
|
POST
|
Found it - I had my sort before my grid.renderArray. Doesn't work that way!
... View more
07-03-2012
06:06 AM
|
0
|
0
|
614
|
|
POST
|
My code was working well and suddenly the sort doesn't work anymore. I don't see any errors and I've not changed the functions that deal with it. Very frustrating.
... View more
07-03-2012
06:02 AM
|
0
|
0
|
614
|
| Title | Kudos | Posted |
|---|---|---|
| 1 | 06-02-2017 02:38 PM | |
| 2 | 03-18-2022 10:14 AM | |
| 2 | 02-18-2016 06:28 AM | |
| 1 | 03-18-2024 07:29 AM | |
| 4 | 08-02-2023 06:08 AM |
| Online Status |
Offline
|
| Date Last Visited |
02-25-2025
01:56 PM
|