Wednesday 10 June 2015

arcgis 10.2 - arcpy SearchCursor return a OBJECTID value not as an integer



I use summary statistics GP tool to extract the max OBJECTID from a feature layer. After that I use arcpy SerachCursor to get the OBJECTID value from the statistics, but the result returned is not an integer.


The statistics contents:


enter image description here


The result of SearchCursor:


enter image description here


The python code:


import arcpy

arcpy.env.workspace = "C:/Users/lyao/Documents/ArcGIS/Default.gdb"
feature = "C:/Users/lyao/Documents/ArcGIS/Default.gdb/BQ_Segment_1_Statistics1"


with arcpy.da.SearchCursor(feature, ("MAX_geri_db_DBO_BQ_Segment_1_OBJECTID",), '"OBJECTID" = 1') as cursor:
for row in cursor:
print str(row[0])

I want to know:




  1. How to get the exact same OBJECTID as in the statistics.





  2. I am using vb.net, so I want to know how to return the result to vb.net.




I am using arcmap 10.2.



Answer



In VB.net:


Dim pWSF As ESRI.ArcGIS.Geodatabase.IWorkspaceFactory = New ESRI.ArcGIS.DataSourcesGDB.FileGDBWorkspaceFactory()
Dim pWS As ESRI.ArcGIS.Geodatabase.IWorkspace = pWSF.OpenFromFile("C:/Users/lyao/Documents/ArcGIS/Default.gdb", 0)
Dim pFws As ESRI.ArcGIS.Geodatabase.IFeatureWorkspace = CType(pWS, ESRI.ArcGIS.Geodatabase.IFeatureWorkspace)

Dim pTable As ESRI.ArcGIS.Geodatabase.ITable = pFws.OpenTable("BQ_Segment_1_Statistics1")
Dim pCur As ESRI.ArcGIS.Geodatabase.ICursor = pTable.Search(Nothing, False) ' open the cursor
Dim pRow As ESRI.ArcGIS.Geodatabase.IRow = pCur.NextRow ' first row
Dim pValue As Integer = pRow.Value(2) ' the value in the 3rd column

That is assuming that the table never deviates from the format shown.


You start with an IWorkspaceFactory (set the type to match the database) from which you create an IWorkspace which gets changed into a IFeatureWorkspace which is able to open the table. With the table start the cursor, get the first row and then retrieve the value.


You can see why arcpy is so popular... The online help will give some explanation and options for the objects shown but that's the way to open a table from scratch.


No comments:

Post a Comment

arcpy - Changing output name when exporting data driven pages to JPG?

Is there a way to save the output JPG, changing the output file name to the page name, instead of page number? I mean changing the script fo...