from snippetarcgis102 import CreateMXD,CType,NewObj
import snippetarcgis102
import os


import comtypes.gen.esriCarto as esriCarto
import comtypes.gen.esriGeoDatabase as esriGDB
import comtypes.gen.esriDisplay as esriDisplay
import comtypes.gen.esriArcMapUI as esriArcMap
import comtypes.gen.esriFramework as esriFramework
import comtypes.gen.esriMaplex as esriMaplex
import comtypes.gen.esriSystem as esriSystem
import comtypes.gen.esriGeometry as esriGeometry

def coordinate_system(projected_EPSG,geographic_EPSG):#return list [geographic,projected] coordinate system
  coord_factory = NewObj(esriGeometry.SpatialReferenceEnvironment,  esriGeometry.ISpatialReferenceFactory3)
  gcs = coord_factory.CreateGeographicCoordinateSystem(geographic_EPSG)
  spref_gcs =  CType(gcs,esriGeometry.ISpatialReference)
  spref_gcs.SetFalseOriginAndUnits(-180, -90, 1000000)
  pcs = coord_factory.CreateProjectedCoordinateSystem(projected_EPSG)
  pcs.SetFalseOriginAndUnits (0, 0, 1000)
  spref_pcs = CType(pcs,esriGeometry.ISpatialReference)
  return [spref_gcs,spref_pcs]

1     import comtypes.gen.esriDataSourcesGDB as esriDataSourcesGDB
2     workspace_factory = NewObj(esriDataSourcesGDB.FileGDBWorkspaceFactory, esriGDB.IWorkspaceFactory2)  
3     if_gdb = os.path.abspath(gdb_path)  #rabochaya versija
4     if os.path.isfile(if_gdb):  #if not exsists then create gdb  
5       workspace_factory.Create(os.path.dirname(gdb_path),os.path.basename(gdb_path[:-4]), None, 0)
6     workspace = workspace_factory.OpenFromFile(gdb_path,0)  
7     feature_workspace = CType(workspace,esriGDB.IFeatureWorkspace)
9     cs =coordinate_system(28413,4284)  
11    fcDesc = NewObj(esriGDB.FeatureClassDescription,esriGDB.IFeatureClassDescription)
12    ocDesc = CType(fcDesc, esriGDB.IObjectClassDescription)  
13    fields_ = ocDesc.RequiredFields
14    #fields = NewObj(esriGDB.Fields, esriGDB.IFields2) 
15    fields = CType(fields_,esriGDB.IFields2)
16    for i in range(fields.FieldCount):
17      print fields.Field(i).Name
19    fields_edit = CType(fields,esriGDB.IFieldsEdit)  
20    fields_edit.FieldCount_2 =3  
21    field = NewObj(esriGDB.Field,esriGDB.IField2)
22    field_edit = CType(field, esriGDB.IFieldEdit2) 
24    geometry_def = NewObj(esriGDB.GeometryDef,esriGDB.IGeometryDef) 
25    geometry_def_edit  = CType(geometry_def, esriGDB.IGeometryDefEdit)  
27    geometry_def_edit.GeometryType_2 = 3  #1 -point,2-multypoint,13-line, 3 - polyline, 4 - polygon
28    print geometry_def_edit.GeometryType
29    print geometry_def.GeometryType

30    geometry_def_edit.GridCount_2 = 1 #number of spatial indexes 
31    geometry_def_edit.HasM_2= False
32    geometry_def_edit.HasZ_2= False
33    geometry_def_edit.SpatialReference_2 = cs[0]
34    geometry_def_edit.Project_2 = cs[1]
36    """editing geometry field and appending it to fields"""
37    field_edit.Name_2 = 'SHAPE'
38    field_edit.Type_2 = 7 # geometry
39    field_edit.GeometryDef_2 = geometry_def
40    field_edit.IsNullable_2 = True
41    field_edit.Required_2  = True
42    fields_edit.AddField(field)
43    for i in range(fields.FieldCount):
44      print fields.Field(i).Name  
46    feature_workspace.CreateFeatureClass('test_line',fields,None,None,1,"SHAPE","") 

the last line 46 works fine, but it creates polygon without spatial reference(by default). the SET property needs "_2" appending according snippets from arcgis help

[Visual Basic .NET] Public WriteOnly Property GeometryType_2

[C#] public void GeometryType_2 {set;}

but when i check and print read only property,(line 28 ,29) it returns 0 instead of 3,which i set in line 27. if I delete "_2" from property name python throws error 'Can't set attribute' -and it is obvious, because without "_2" it is read only property. In other interfaces I've used there were read-write properties with the same name, and it works excelent. In this case the names are different,how can i correctly set this properties??

P.S. before arcobjects, i've used arcpy, but it too slow, and functionality of arcpy isn't enough for me in future work.

  • Why are you trying to use python if it's too slow? VB.net express is a free download and very easy to learn. Yes, the _2 is needed in C# but not in VB for some reason; it is not intended that you access the read-only attributes from the IGeometryDefEdit (line 28) but it should be set on line 29 to the IGeometryDef, perhaps it's a python issue. Creating a feature class and adding a field is something that I would do with arcpy as it's much easier and doesn't take much time in the overall process but iterating geometries and accessing interfaces that aren't exposed I would do in ArcObjects. – Michael Stimson Dec 14 '15 at 2:25
  • IGeometryDef have only read-only properties, so i can set (write) property using onlyIGeometryDefEdit interface – Dmitriy Litvinov Dec 14 '15 at 7:53
  • That's the way it works... like IField/IFieldEdit and IFields/IFieldsEdit - IField and IFields are read-only and need their respective Edit interface to set the properties. This parity isn't exactly common amongst the interfaces however read-write properties often exhibit property(read)/property_2(write) behavior which isn't exactly explained in the documentation. Your best reference is the samples. If you have installed ArcObjects there will be a sample folder showing some basic tasks or online. – Michael Stimson Dec 14 '15 at 20:54

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Browse other questions tagged or ask your own question.