Thursday, 3 November 2016

Arcobjects in Python: assign interface to objects with feature (shape) geometry


I am trying to use ArcObject in Python. I do have problem with assigning an interface (i.e. IZ.InterpolateZsBetween) to my object with geometry data, code is:


from Comtypes.client import CreateObject, GetModule
import arcgisscripting

gp = arcgisscripting.create(9.3)
gp.setproduct("ArcInfo")


esriSystem = GetModule("C:/Program Files (x86)/ArcGIS/com/esriSystem.olb")
esriGeometry = GetModule("C:/Program Files (x86)/ArcGIS/com/esriGeometry.olb")
esriDataSourcesGDB = GetModule("C:/Program Files (x86)/ArcGIS/com/esriDataSourcesGDB.olb")
esriGeoDatabase = GetModule("C:/Program Files (x86)/ArcGIS/com/esriGeoDatabase.olb")

sPath ="c:/temp/test.gdb"
pWSF = CreateObject(esriDataSourcesGDB.FileGDBWorkspaceFactory,\
interface=esriGeoDatabase.IWorkspaceFactory)
pWS = pWSF.OpenFromFile(sPath, 0)
pFWS = pWS.QueryInterface(esriGeoDatabase.IFeatureWorkspace)

pFClass = pFWS.OpenFeatureClass("test_z")
pFCursor = pFClass.Search(None, True)
pFeat = pFCursor.NextFeature()
pIZ = pFeat.QueryInferface(esriGeometry.IZ2)

Why this is not working? The output is:


 Traceback (most recent call last):
File "", line 1, in
pIZ = pFeat.QueryInterface(esriGeometry.IZ2)
File "C:\Python25\lib\site-packages\comtypes\__init__.py", line 1078, in QueryInterface

self.__com_QueryInterface(byref(iid), byref(p))
COMError: (-2147467262, 'Taki interfejs nie jest obs\xb3ugiwany.', (None, None, None, 0,
None))

So I tried:


pShape = pFeat.ShapeCopy
pIZ = pShape.QueryInterface(esriGeometry.IZ2)
pShape.InterpolateZsBetween(0, 0, 0, 4)

And this is working. But how to stick it back and save it in my feature? I am learning Python and ArcObjects, so please be understandful. Any help will be appreciated.




Answer



The IZ interface is implemented only on the Polygon and Polyline classes, which you obtain from the Shape or ShapeCopy properties of an IFeature. You can't QI between interfaces that aren't supported (either implemented or inherited) by an object. Use the documentation for one of the ArcObjects SDKs to see the interfaces each class implements. Reading the Object Model Diagrams (listed under each namespace in the help, example) can also be very helpful to see the inheritance/implementation relationships between the various classes and interfaces.


As for your actual workflow, you'll need to use the Update instead of Search method on your feature class to obtain an update cursor, and then set the Shape property of the feature to the result of the InterpolateZsBetween method and finally call IFeatureCursor.UpdateFeature (thanks for the correction @Ragi).


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...