|
DOC
|
In order to decide what tool to use on-the-fly, you would have to have either an if..elif...else structure, dictionary map, or some other control structure. In that case, why not call the tool like normal. The other hang up is arguments because different tools have different arguments. Granted, some tools share syntax/arguments, but not enough to make one argument list to rule them all. From a learning perspective, and understanding more nuts and bolts of Python, code snippets like this are useful even if the real-world need is limited.
... View more
02-19-2015
01:49 PM
|
0
|
0
|
3091
|
|
POST
|
Owen Earley, although I agree that GeoNet is really about Esri and ArcGIS, the GeoNet home page does say "all things geo." Maybe the intern was excited and got a bit carried away the scope statement, but one could argue Esri did open the door to non-Esri questions, not that one should expect to get many responses.
... View more
02-19-2015
10:36 AM
|
1
|
0
|
880
|
|
POST
|
How are you "seeing" the geodatabases tables? Are you logging into SQL Server using SQL Server Management Studio (SSMS) or SQLCMD? If the former, are there no views showing up under the Views folder/tab? Originally with 10.1, versioned views ended with "_VW" but Esri changed that in 10.2 to end in "_EVW".
... View more
02-19-2015
09:13 AM
|
1
|
2
|
5328
|
|
POST
|
Starting with ArcGIS 10.1, versioned views are automatically created for tables or feature classes that are registered as versioned. Are you sure the "Enable SQL Access" isn't greyed out because a versioned view already exists?
... View more
02-19-2015
06:42 AM
|
1
|
7
|
5328
|
|
POST
|
Try the following, see if this is what you are after: import arcpy
census = #census unit feature class
percentField= #field where percentage will be input
study = #walk or bikeshed feature class
#make feature layer for using with SelectLayerByLocation
arcpy.MakeFeatureLayer_management(census, "census")
#make search cursor over study areas, even if just one.
studyCursor = arcpy.da.SearchCursor(study, ["OID@", "SHAPE@"])
for oid, s_shape in studyCursor:
#for given study area, select census blocks that intersect
arcpy.SelectLayerByLocation_management("census", "INTERSECT", s_shape)
#make update cursor for census blocks selected above
censusCursor = arcpy.da.UpdateCursor("census", ["SHAPE@", percentField])
for c_shape, _ in censusCursor:
#get area of census block
area = c_shape.area
#find pct of census block that intersects study area
pct = c_shape.intersect(s_shape, 4).area / area * 100
#update record
censusCursor.updateRow([c_shape, pct])
del censusCursor
del studyCursor The code above is designed to be run as a standalone script, not as a Python tool. If the functional code works for you, it can be moved into a Python tool without much effort.
... View more
02-18-2015
01:50 PM
|
1
|
5
|
3021
|
|
POST
|
I couldn't agree more. From the perspective of managing ArcGIS software and not just using it, things have degenerated ever since the dot releases came to light. Beyond the sheer number of patches and lack of any roll-up, all of the back porting of patches has made for an interesting landscape. Now we have patches that need to be applied to 10.2.1 but then re-applied after upgrading to 10.2.2, which seldom if ever happened when service packs were around because back porting was practically nonexistent between service packs. To complicate the matter, the PatchFinder.exe hasn't had its metadata updated in more than a year. The file properties still show the same version # as one I downloaded 12 months ago, but the one from 12 months ago doesn't see all of the newer patches. If the file properties aren't being updated as new PatchFinder.exes are released, how are users supposed to know they have an out of date version?
... View more
02-18-2015
11:19 AM
|
1
|
0
|
1707
|
|
POST
|
Do you have 64-bit Background Geoprocessing installed? If so, then you might be running into an issue where a different ArcPy site package is being loaded than what you expect. If you don't have 64-bit Background Geoprocessing installed, then the only issue should be which Python interpreter you are using, the 32-bit tied to Desktop or the 64-bit tied to Server. In general, though, assuming which ArcPy site package is loaded based on ArcGIS installation order isn't a good practice. You can find the path to the specific interpreter being used with sys.executable, just write a short script to dump that value to a file. That being said, how are you running the scheduled task? Are you specifying the Python executable and passing it your script as an argument or running the script directly and relying on Windows file extension associations to determine which interpreter is being used?
... View more
02-18-2015
11:03 AM
|
0
|
1
|
984
|
|
POST
|
Did some quick tests selecting 25 random records against a ~700,000 record non-versioned feature class in Oracle. Of the three different approaches I mentioned above (random.sample, reservoir sampling, and SQL sample), random.sample was the quickest taking about 4.5 seconds on average to make a feature layer. SQL sample took about 1.4x longer than random.sample while reservoir sampling took 5.1x longer. It seems the overhead of calling random over N items becomes quite impactful with hundreds of thousands of records, and that impact will only grow as N grows. Also, the memory impact from fully populating an OID list was quite a bit smaller than I anticipated. The reservoir sampling code above is quite simple and isn't distributed, which I have seen some high-performing distributed reservoir sampling code, but it still demonstrates the trade-offs of different approaches. The SQL sampling performed fairly well, a bit better than I expected. Given it wasn't that much slower than random.sampling, it seems a more efficient piece of SQL might make the approach more competitive. It would be interesting to see an SQL Server comparison, but that will have to wait for another day.
... View more
02-15-2015
10:18 PM
|
1
|
0
|
3798
|
|
POST
|
A completely different approach to relying on application logic to select random records would be to rely on the database management system to select random records. If someone is using an enterprise DBMS (Oracle, SQL Server, PostgreSQL, etc..), the database will support some way of returning randomly selected records. def main():
import arcpy
import os
fc_in = arcpy.GetParameterAsText(0) # input featureclass
fl_out = arcpy.GetParameterAsText(1) # output layerfile
cnt = arcpy.GetParameter(2) # number of features to select
dbms = # use 'oracle' or 'sqlserver'
oid_fld = arcpy.Describe(fc_in).OIDFieldName
oracle_where_clause = (
"{} IN (SELECT {} FROM "
"(SELECT {} FROM {} ORDER BY dbms_random.value) "
"WHERE rownum <= {})".format(
oid_fld, oid_fld, oid_fld, os.path.split(fc_in)[1], cnt
)
)
sqlserver_where_clause = (
"{} IN (SELECT TOP {} {} FROM {} ORDER BY NEWID())".format(
oid_fld, cnt, oid_fld, os.path.split(fc_in)[1]
)
)
if dbms == 'oracle':
where_clause = oracle_where_clause
elif dbms == 'sqlserver':
where_clause = sqlserver_where_clause
arcpy.MakeFeatureLayer_management(fc_in, "tmpLayer")
arcpy.SelectLayerByAttribute_management("tmpLayer", "NEW_SELECTION", where_clause)
arcpy.MakeFeatureLayer_management("tmpLayer", "selection")
arcpy.SaveToLayerFile_management("selection", fl_out)
if __name__ == '__main__':
main() In many ways, this would not be a very good "general" approach for several reasons. One, relying on the DBMS makes the code more involved or less portable because every DBMS seems to have a different approach to selecting random records. Second, passing SQL through ArcGIS tools always seems to have a sketchiness about it. It does work, but I have definitely run into issues as well. Looking at the code above, one might wonder why lines 29 and 30 exist, i.e., why not just pass the SQL directly to the MakeFeatureLayer tool. When I first ginned up this code, I tried doing just that, but I found an interesting/odd behavior. The SQL to select random records actually became embedded in the definition of the feature layer so every time ArcMap was refreshed, the records kept changing. ArcMap didn't like that, there would be issues with displaying polygons at times. It would be neat, though, with attribute-only data to load a dynamically, randomly changing table into ArcMap for testing at times.
... View more
02-15-2015
01:33 PM
|
1
|
0
|
3798
|
|
POST
|
If we are already importing random, then we can rely on random.sample to do the heavy lifting for us, assuming we have already went to the effort of building an OID list and want sampling without replacement. def main():
import arcpy
from random import sample
fc_in = arcpy.GetParameterAsText(0) # input featureclass
fl_out = arcpy.GetParameterAsText(1) # output layerfile
cnt = arcpy.GetParameter(2) # number of features to select
fld_oid = arcpy.Describe(fc_in).OIDFieldname
lst_oids = [oid for oid, in arcpy.da.SearchCursor(fc_in, (fld_oid))]
oids = ", ".join(map(str, sample(lst_oids, cnt)))
where = "{0} IN ({1})".format(arcpy.AddFieldDelimiters(fc_in, fld_oid), oids)
arcpy.MakeFeatureLayer_management(fc_in, "selection", where)
arcpy.SaveToLayerFile_management("selection", fl_out)
if __name__ == '__main__':
main() If building an OID list in-memory becomes an issue, one could resort to using a reservoir sampling approach. def stream_sample(iterator, k):
from random import randint
result = [next(iterator) for _ in range(k)]
n = k
for item in iterator:
n += 1
s = randint(0, n)
if s < k:
result = item
return result
def main():
import arcpy
fc_in = arcpy.GetParameterAsText(0) # input featureclass
fl_out = arcpy.GetParameterAsText(1) # output layerfile
cnt = arcpy.GetParameter(2) # number of features to select
fld_oid = arcpy.Describe(fc_in).OIDFieldname
sample_oids = [oid for oid, in stream_sample(arcpy.da.SearchCursor(fc_in, "OID@"), cnt)]
oids = ", ".join(map(str, sample_oids))
where = "{0} IN ({1})".format(arcpy.AddFieldDelimiters(fc_in, fld_oid), oids)
arcpy.MakeFeatureLayer_management(fc_in, "selection", where)
arcpy.SaveToLayerFile_management("selection", fl_out)
if __name__ == '__main__':
main() A plus of using reservoir sampling is that the memory footprint can be quite modest to trivial when working with very large datasets. A minus of using reservoir sampling is that calling random so many times can add noticeable overhead; that said, I can still sample from a million records in a couple seconds. The stream_sample function is taken from JesseBuesking on the StackExchange thread: pick N items at random. The code from JesseBuesking is basically just implementing Don Knuth's algorithm for picking random elements from a set whose cardinality is unknown.
... View more
02-15-2015
01:13 PM
|
1
|
0
|
3798
|
|
POST
|
OK, now we are getting down to business. Hopefully Vince Angelo can find some time to chime in, he always has good information to share on these types of question. I will have to take some time to think them over.
... View more
02-12-2015
10:39 AM
|
0
|
0
|
3595
|
|
POST
|
You can see database views in ArcGIS Desktop just by connecting to a database, what functionality are you hoping to gain by registering a database view? Is it not functionality but performance related?
... View more
02-12-2015
10:06 AM
|
0
|
7
|
4608
|
|
POST
|
Providing a bit more information would be helpful. You mention ArcSDE 10.1, have you applied any Service Packs or patches? What edition of ArcSDE (Personal, Workgroup, Enterprise)? What version and edition of SQL Server are you using? What version of MS Access are you using? What driver(s) and version(s) have you tried?
... View more
02-12-2015
07:21 AM
|
0
|
0
|
1665
|
|
POST
|
There are a couple of things going on here. First, arcpy.GetInstallInfo has no parameters. It will accept an argument and not throw an error, but any argument that is passed doesn't affect the results of what is returned. The arcpy.GetInstallInfo function gets the installation information that relates to the currently loaded ArcPy site package, which in your case is ArcGIS Engine. If you install ArcGIS Desktop and ArcGIS Engine on the same machine using standard installation instructions, the two will share a single Python interpreter, usually C:\Python27\ArcGIS10.x (10.2 in this case). Another way to look at it is that a single Python interpreter has 2 ArcPy site packages registered/installed. Since the site packages have the same name (arcpy), they both can't be loaded into the interpreter at the same time. When the interpreter encounters an import arcpy statement, it will find and import whichever site package is found first in the search path for modules, i.e., sys.path. Before importing ArcPy, you can quickly determine which ArcPy site package will be loaded by running: import imp
imp.find_module('arcpy') In this case, the result will come back with ...\Engine10.2\... since it comes first in the sys.path. Reversing the order of sys.path before importing ArcPy will likely find and import the Desktop site package for your situation. import imp
import sys
sys.path.reverse()
imp.find_module('arcpy') The issue with reversing sys.path out of hand is that it will import the Engine-based site package if ArcGIS Engine was installed first. There are a couple of more thoughtful ways to get around this problem. First, setting the PYTHONPATH Windows environment variable to the Desktop-based site package will ensure that it is loaded first regardless of the sys.path order. However, in this case, it seems you don't already know that location and am trying to figure it out. A second approach would be to remove all of the Engine-related entries in sys.path before import arcpy. import sys
for p in sys.path[:]:
if 'Engine' in p:
sys.path.remove(p) If the goal is to determine whether ArcGIS Desktop is installed and where, maybe querying a WMI service for installed applications and information is a better approach. Adapted from the Microsoft Script Center List Installed Software Python script: import win32com.client
objWMIService = win32com.client.Dispatch("WbemScripting.SWbemLocator")
objSWbemServices = objWMIService.ConnectServer(".", "root\cimv2")
colItems = objSWbemServices.ExecQuery("Select * from Win32_Product "
"where Name Like 'ArcGIS % for Desktop'")
for objItem in colItems:
print "Name: ", objItem.Name
print "Install Date: ", objItem.InstallDate
print "Install Location: ", objItem.InstallLocation
print ""
... View more
02-12-2015
06:43 AM
|
3
|
0
|
1967
|
| Title | Kudos | Posted |
|---|---|---|
| 2 | 4 weeks ago | |
| 1 | 05-29-2026 08:22 AM | |
| 1 | a month ago | |
| 3 | a month ago | |
| 1 | 05-22-2026 05:27 AM |
| Online Status |
Online
|
| Date Last Visited |
2 hours ago
|