[Gdal-dev] ArcGIS

cemehehko at mail.ru cemehehko at mail.ru
Sun Jul 30 08:29:39 EDT 2006


Hello gdal-dev,

I'm in happy with new 1.3.2 release. I see a new driver for ArcSDE and
ESRI Personal GeoDatabase.
This drivers is read only. And this is correct. There are no need to
try it in write access.


For write, and change ArcSDE&Personal GDB I use ESRI ArgGIS(ArcMap)
CPPAPI.

It is cool to create another driver for read/write/create access to
ArcGIS data in any ESRI format (shapefiles,SDE,Personal GDB and other)
with ArcMap installed on target machine name like ArcGIS API.
The advantage of this API that it is easy and uses 'ESRI'(родное) API
to data access. This API executes neccesary COM extensions of
ArcWorkspace on feature layers.

I attach the files:
1. sdk_windows.h - corrected by me
2. main.cpp - my test program (this can help you)

I can provide more correctly code if you need.


-- 
Best regards,
 cemehehko                          mailto:cemehehko at mail.ru
-------------- next part --------------

#ifndef __ESRI__ARCSDK_COM_SDK_WINDOWS_H__
#define __ESRI__ARCSDK_COM_SDK_WINDOWS_H__

#define STRICT
#define _WIN32_WINNT 0x400
#define _ATL_APARTMENT_THREADED
#define EXT_CLASS _declspec(dllexport)

#define WIN32_LEAN_AND_MEAN             // Exclude rarely-used stuff from Windows headers

#include <windows.h>
#include <ocidl.h>

#include <atlbase.h>

// raw_interfaces_only raw_native_types no_namespace named_guids exclude("OLE_COLOR", "OLE_HANDLE", "ICursorPtr", "VARTYPE")

#import<esriSystem.olb>                 raw_interfaces_only raw_native_types no_namespace named_guids exclude("OLE_COLOR", "OLE_HANDLE", "ICursorPtr", "VARTYPE")
#import<esriSystemUI.olb>               raw_interfaces_only raw_native_types no_namespace named_guids exclude("OLE_COLOR", "OLE_HANDLE", "ICursorPtr", "VARTYPE")
#import<esriGeometry.olb>               raw_interfaces_only raw_native_types no_namespace named_guids exclude("OLE_COLOR", "OLE_HANDLE", "ICursorPtr", "VARTYPE")
#import<esriEditor.olb>                 raw_interfaces_only raw_native_types no_namespace named_guids exclude("OLE_COLOR", "OLE_HANDLE", "ICursorPtr", "VARTYPE")
#import<esriDisplay.olb>                raw_interfaces_only raw_native_types no_namespace named_guids exclude("OLE_COLOR", "OLE_HANDLE", "ICursorPtr", "VARTYPE")
#import<esriServer.olb>                 raw_interfaces_only raw_native_types no_namespace named_guids exclude("OLE_COLOR", "OLE_HANDLE", "ICursorPtr", "VARTYPE")
#import<esriOutput.olb>                 raw_interfaces_only raw_native_types no_namespace named_guids exclude("OLE_COLOR", "OLE_HANDLE", "ICursorPtr", "VARTYPE")
#import<esriGeoDatabase.olb>            raw_interfaces_only raw_native_types no_namespace named_guids exclude("OLE_COLOR", "OLE_HANDLE", "ICursorPtr", "VARTYPE")
#import<esriGeoDatabaseUi.olb>            raw_interfaces_only raw_native_types no_namespace named_guids exclude("OLE_COLOR", "OLE_HANDLE", "ICursorPtr", "VARTYPE")
#import<esriGISClient.olb>              raw_interfaces_only raw_native_types no_namespace named_guids exclude("OLE_COLOR", "OLE_HANDLE", "ICursorPtr", "VARTYPE")
#import<esriDataSourcesFile.olb>        raw_interfaces_only raw_native_types no_namespace named_guids exclude("OLE_COLOR", "OLE_HANDLE", "ICursorPtr", "VARTYPE")
#import<esriDataSourcesGDB.olb>         raw_interfaces_only raw_native_types no_namespace named_guids exclude("OLE_COLOR", "OLE_HANDLE", "ICursorPtr", "VARTYPE")
#import<esriDataSourcesOleDB.olb>       raw_interfaces_only raw_native_types no_namespace named_guids exclude("OLE_COLOR", "OLE_HANDLE", "ICursorPtr", "VARTYPE")
#import<esriDataSourcesRaster.olb>      raw_interfaces_only raw_native_types no_namespace named_guids exclude("OLE_COLOR", "OLE_HANDLE", "ICursorPtr", "VARTYPE")
#import<esriGeoDatabaseDistributed.olb> raw_interfaces_only raw_native_types no_namespace named_guids exclude("OLE_COLOR", "OLE_HANDLE", "ICursorPtr", "VARTYPE")
#import<esriCarto.olb>                  raw_interfaces_only raw_native_types no_namespace named_guids exclude("OLE_COLOR", "OLE_HANDLE", "ICursorPtr", "VARTYPE")
#import<esriLocation.olb>               raw_interfaces_only raw_native_types no_namespace named_guids exclude("OLE_COLOR", "OLE_HANDLE", "ICursorPtr", "VARTYPE")
#import<esriNetworkAnalysis.olb>        raw_interfaces_only raw_native_types no_namespace named_guids exclude("OLE_COLOR", "OLE_HANDLE", "ICursorPtr", "VARTYPE")
#import<esriArcMap.olb>        raw_interfaces_only raw_native_types no_namespace named_guids exclude("OLE_COLOR", "OLE_HANDLE", "ICursorPtr", "VARTYPE")
#import<esriArcMapUi.olb>        raw_interfaces_only raw_native_types no_namespace named_guids exclude("OLE_COLOR", "OLE_HANDLE", "ICursorPtr", "VARTYPE")
#import<esriFramework.olb>        raw_interfaces_only raw_native_types no_namespace named_guids exclude("OLE_COLOR", "OLE_HANDLE", "ICursorPtr", "VARTYPE")
//#import<esriNetworkAnalyst.olb>         raw_interfaces_only raw_native_types no_namespace named_guids exclude("OLE_COLOR", "OLE_HANDLE", "ICursorPtr", "VARTYPE")
#import<esriGeoAnalyst.olb>             raw_interfaces_only raw_native_types no_namespace named_guids exclude("OLE_COLOR", "OLE_HANDLE", "ICursorPtr", "VARTYPE")
#import<esri3DAnalyst.olb>              raw_interfaces_only raw_native_types no_namespace named_guids exclude("OLE_COLOR", "OLE_HANDLE", "ICursorPtr", "VARTYPE")
#import<esriGlobeCore.olb>              raw_interfaces_only raw_native_types no_namespace named_guids exclude("OLE_COLOR", "OLE_HANDLE", "ICursorPtr", "VARTYPE")
//#import<esriSpatialAnalyst.olb>         raw_interfaces_only raw_native_types no_namespace named_guids exclude("OLE_COLOR", "OLE_HANDLE", "ICursorPtr", "VARTYPE")
//#import<esriGeoStatisticalAnalyst.olb>  raw_interfaces_only raw_native_types no_namespace named_guids exclude("OLE_COLOR", "OLE_HANDLE", "ICursorPtr", "VARTYPE")

/* API REDEFINITIONS */

#define AoInitialize   CoInitialize
#define AoUninitialize CoUninitialize

#define AoCreateObject CoCreateInstance

#define AoAllocBSTR    SysAllocString
#define AoFreeBSTR     SysFreeString

#define AoExit         exit

#endif /* __ESRI__ARCSDK_COM_SDK_WINDOWS_H__ */

-------------- next part --------------
#include <iostream>
using namespace std;

.... 

#define ESRI_WINDOWS
#include "ArcSDK.h"

#define MSG(x) You message poster


int main(int argc,char *argv[])
{


  // This for Personal Geodatabase
  /* 
  IPropertySetPtr pProperty(CLSID_PropertySet);
  IWorkspaceNamePtr pWorkspaceName;

  IWorkspaceFactoryPtr pWorksapceFactory(CLSID_AccessWorkspaceFactory);
  pWorksapceFactory->Create(L".",L"mydb",pProperty,0,&pWorkspaceName);*/

  // This for ArcSDE 
  // QI, WorkspaceName implements both IWorkspaceName and
  IPropertySetPtr property(CLSID_PropertySet);

  VARIANT v;
  v.vt=VT_BSTR;
  v.bstrVal=::SysAllocString(L"ANDREW");
  property->SetProperty(CComBSTR(_T("Server")),v);
  v.bstrVal=::SysAllocString(L"5151");
  property->SetProperty(CComBSTR(_T("Instance")),v);
  v.bstrVal=::SysAllocString(L"sde");
  property->SetProperty(CComBSTR(_T("USER")),v);
  v.bstrVal=::SysAllocString(L"sde");
  property->SetProperty(CComBSTR(_T("password")),v);
  property->SetProperty(CComBSTR(_T("Database")),v);
  v.bstrVal=::SysAllocString(L"sde.DEFAULT");
  property->SetProperty(CComBSTR(_T("version")),v);

  IWorkspacePtr ipWorkspace;
  IWorkspaceFactoryPtr factory(CLSID_SdeWorkspaceFactory);
  cerr<<factory->Open(property,0,&ipWorkspace)<<endl;

  if(!ipWorkspace)
  {
    MSG(app.tr("Can't open database"));
    return 1;
  }
  IFeatureWorkspacePtr ws(ipWorkspace);
  if(!ws)
  {
    MSG(app.tr("Can't open database"));
    return 1;
  }

  IWorkspaceEditPtr edit(ws);
  if(!edit)
  {
    MSG(app.tr("Can't edit"));
    return 1;
  }
  if(edit->StartEditing(VARIANT_FALSE)!=S_OK)
  {
    MSG(app.tr("Can't start edit"));
    return 1;
  }

  // Try to open a layer
  IFeatureClassPtr featureClass;
  ws->OpenFeatureClass(L"LayerName",&featureClass);
  if(!featureClass)
  {
    MSG(app.tr("Layer not found"));
    return 1;
  }
  CComBSTR str;
  featureClass->get_AliasName(&str);

  /*
  if(FALSE) // Delete example
  {
    cerr<<"start delete"<<endl;
    edit->StartEditOperation();
    IQueryFilterPtr filter(CLSID_QueryFilter);
    IFeatureCursorPtr cursor;
    featureClass->Search(filter,FALSE,&cursor);
    if(cursor)
    {
      IFeaturePtr feature;
      cursor->NextFeature(&feature);
      int count=0;
      while(feature)
      {
        count++;
        IFeaturePtr delFeature=feature;
        cursor->NextFeature(&feature);

        delFeature->Delete();
      }
      cerr<<"deleted="<<count<<endl;
    }
    edit->StopEditOperation();
  }

  edit->StopEditing(VARIANT_TRUE);
  if(edit->StartEditing(VARIANT_FALSE)!=S_OK)
  {
    MSG(app.tr("Отказ редактирования"));
    return 1;
  }
  */



  int count=0;
  int at=0;
  //  lyr is my Biomap layer
  //  P.S. Biomap is not interesing GIS
  while(lyr.nextFeature(FALSE))
  {
    at++;
    if(lyr.featureDeleted())
    {
      continue;
    }

    QList<CPoint> plist=lyr.featureCoordList();
    plist.append(plist[0]);

    edit->StartEditOperation();
    IFeaturePtr feature;
    if(featureClass->CreateFeature(&feature)!=S_OK)
    {
      cerr<<"error"<<featureClass->CreateFeature(&feature)<<endl;
    }

    if(feature)
    {
      IPointCollectionPtr pc(CLSID_Polygon);
      {
        IPointPtr p(CLSID_Point);
        foreach(CPoint pnt,plist)
        {
          p->PutCoords(pnt.x,pnt.y);
          pc->AddPoint(p);
        }
        //cerr<<"pts:"<<plist.count()<<endl;
      }

      IGeometryPtr geom(pc);
      if(feature->putref_Shape(geom)!=S_OK)
      {
        cerr<<"Error in shape"<<endl;
      }
      long oid;
      feature->get_OID(&oid);

      cerr<<count<<"("<<at*100/lyr.getMaxCode()<<"%): "<<lyr.featureAt()<<endl;
    }else
    {
      MSG(app.tr("Can't create a feature"));
      return 1;
    }

    HRESULT er=feature->Store();
    //cerr<<feature->Store()<<endl;
    if(er!=S_OK)
    {
      cerr<<"Not stored: "<<er<<endl;
    }
    edit->StopEditOperation();
    count++;
  }
  edit->StopEditing(VARIANT_TRUE);

  cerr<<"appended="<<count<<endl;


  //AoUninitialize();

  return 0;
}


More information about the Gdal-dev mailing list