1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463
|
/******************************************************************************
*
* Project: OpenGIS Simple Features Reference Implementation
* Purpose: Different utility functions used in ArcObjects OGR driver.
* Author: Ragi Yaser Burhum, ragi@burhum.com
*
******************************************************************************
* Copyright (c) 2009, Ragi Yaser Burhum
*
* Permission is hereby granted, free of charge, to any person obtaining a
* copy of this software and associated documentation files (the "Software"),
* to deal in the Software without restriction, including without limitation
* the rights to use, copy, modify, merge, publish, distribute, sublicense,
* and/or sell copies of the Software, and to permit persons to whom the
* Software is furnished to do so, subject to the following conditions:
*
* The above copyright notice and this permission notice shall be included
* in all copies or substantial portions of the Software.
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS
* OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
* THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
* FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
* DEALINGS IN THE SOFTWARE.
****************************************************************************/
#include "aoutils.h"
bool AOErr(HRESULT hr, std::string desc)
{
IErrorInfoPtr ipErrorInfo = NULL;
::GetErrorInfo(NULL, &ipErrorInfo);
if (ipErrorInfo)
{
CComBSTR comErrDesc;
ipErrorInfo->GetDescription(&comErrDesc);
CW2A errMsg(comErrDesc);
CPLError( CE_Failure, CPLE_AppDefined, "AO Error: %s HRESULT:%d COM_ERROR:%s", desc.c_str(), hr, errMsg );
::SetErrorInfo(NULL, NULL);
}
else
{
CPLError( CE_Failure, CPLE_AppDefined, "AO Error: %s HRESULT:%d", desc.c_str(), hr);
}
return false;
}
bool AOToOGRGeometry(IGeometryDef* pGeoDef, OGRwkbGeometryType* pOut)
{
esriGeometry::esriGeometryType geo;
VARIANT_BOOL hasZ;
pGeoDef->get_GeometryType(&geo);
pGeoDef->get_HasZ(&hasZ);
switch (geo)
{
case esriGeometry::esriGeometryPoint: *pOut = hasZ == VARIANT_TRUE? wkbPoint25D : wkbPoint; break;
case esriGeometry::esriGeometryMultipoint: *pOut = hasZ == VARIANT_TRUE? wkbMultiPoint25D : wkbMultiPoint; break;
case esriGeometry::esriGeometryLine: *pOut = hasZ == VARIANT_TRUE? wkbLineString25D : wkbLineString; break;
case esriGeometry::esriGeometryPolyline: *pOut = hasZ == VARIANT_TRUE? wkbMultiLineString25D : wkbMultiLineString; break;
case esriGeometry::esriGeometryPolygon: *pOut = hasZ == VARIANT_TRUE? wkbMultiPolygon25D : wkbMultiPolygon; break;// no mapping to single polygon
default:
{
CPLError( CE_Failure, CPLE_AppDefined, "Cannot map esriGeometryType(%d) to OGRwkbGeometryType", geo);
return false;
}
}
return true;
}
bool AOToOGRFields(IFields* pFields, OGRFeatureDefn* pOGRFeatureDef, std::vector<long> & ogrToESRIFieldMapping)
{
HRESULT hr;
long fieldCount;
if (FAILED(hr = pFields->get_FieldCount(&fieldCount)))
return false;
ogrToESRIFieldMapping.clear();
for (long i = 0; i < fieldCount; ++i)
{
IFieldPtr ipField;
if (FAILED(hr = pFields->get_Field(i, &ipField)))
return AOErr(hr, "Error getting field");
CComBSTR name;
if (FAILED(hr = ipField->get_Name(&name)))
return AOErr(hr, "Could not get field name");
esriFieldType fieldType;
if (FAILED(hr = ipField->get_Type(&fieldType)))
return AOErr(hr, "Error getting field type");
//skip these
if (fieldType == esriFieldTypeOID || fieldType == esriFieldTypeGeometry)
continue;
OGRFieldType ogrType;
if (!AOToOGRFieldType(fieldType, &ogrType))
{
// field cannot be mapped, skipping it
CPLError( CE_Warning, CPLE_AppDefined, "Skipping field %s", CW2A(name) );
continue;
}
OGRFieldDefn fieldTemplate( CW2A(name), ogrType);
pOGRFeatureDef->AddFieldDefn( &fieldTemplate );
ogrToESRIFieldMapping.push_back(i);
}
CPLAssert(ogrToESRIFieldMapping.size() == pOGRFeatureDef->GetFieldCount());
return true;
}
// We could make this function far more robust by doing automatic coertion of types,
// and/or skipping fields we do not know. But our purposes this works fine
bool AOToOGRFieldType(esriFieldType aoType, OGRFieldType* pOut)
{
/*
ESRI types
esriFieldTypeSmallInteger = 0,
esriFieldTypeInteger = 1,
esriFieldTypeSingle = 2,
esriFieldTypeDouble = 3,
esriFieldTypeString = 4,
esriFieldTypeDate = 5,
esriFieldTypeOID = 6,
esriFieldTypeGeometry = 7,
esriFieldTypeBlob = 8,
esriFieldTypeRaster = 9,
esriFieldTypeGUID = 10,
esriFieldTypeGlobalID = 11,
esriFieldTypeXML = 12
*/
//OGR Types
// Desc Name AO->OGR Mapped By Us?
/** Simple 32bit integer */// OFTInteger = 0, YES
/** List of 32bit integers */// OFTIntegerList = 1, NO
/** Double Precision floating point */// OFTReal = 2, YES
/** List of doubles */// OFTRealList = 3, NO
/** String of ASCII chars */// OFTString = 4, YES
/** Array of strings */// OFTStringList = 5, NO
/** deprecated */// OFTWideString = 6, NO
/** deprecated */// OFTWideStringList = 7, NO
/** Raw Binary data */// OFTBinary = 8, YES
/** Date */// OFTDate = 9, NO
/** Time */// OFTTime = 10, NO
/** Date and Time */// OFTDateTime = 11 YES
switch (aoType)
{
case esriFieldTypeSmallInteger:
case esriFieldTypeInteger:
{
*pOut = OFTInteger;
return true;
}
case esriFieldTypeSingle:
case esriFieldTypeDouble:
{
*pOut = OFTReal;
return true;
}
case esriFieldTypeGUID:
case esriFieldTypeGlobalID:
case esriFieldTypeXML:
case esriFieldTypeString:
{
*pOut = OFTString;
return true;
}
case esriFieldTypeDate:
{
*pOut = OFTDateTime;
return true;
}
case esriFieldTypeBlob:
{
*pOut = OFTBinary;
return true;
}
default:
{
/* Intentionally fail at these
esriFieldTypeOID
esriFieldTypeGeometry
esriFieldTypeRaster
*/
return false;
}
}
}
bool AOGeometryToOGRGeometry(bool forceMulti, esriGeometry::IGeometry* pInAOGeo, OGRSpatialReference* pOGRSR, unsigned char* & pInOutWorkingBuffer, long & inOutBufferSize, OGRGeometry** ppOutGeometry)
{
HRESULT hr;
esriGeometry::IWkbPtr ipWkb = pInAOGeo;
long reqSize = 0;
if (FAILED(hr = ipWkb->get_WkbSize(&reqSize)))
{
AOErr(hr, "Error getting Wkb buffer size");
return false;
}
if (reqSize > inOutBufferSize)
{
// resize working buffer
delete [] pInOutWorkingBuffer;
pInOutWorkingBuffer = new unsigned char[reqSize];
inOutBufferSize = reqSize;
}
if (FAILED(hr = ipWkb->ExportToWkb(&reqSize, pInOutWorkingBuffer)))
{
AOErr(hr, "Error exporting to WKB buffer");
return false;
}
OGRGeometry* pOGRGeometry = NULL;
OGRErr eErr = OGRGeometryFactory::createFromWkb(pInOutWorkingBuffer, pOGRSR, &pOGRGeometry, reqSize);
if (eErr != OGRERR_NONE)
{
CPLError( CE_Failure, CPLE_AppDefined, "Failed attempting to import ArcGIS WKB Geometry. OGRGeometryFactory err:%d", eErr);
return false;
}
// force geometries to multi if requested
// If it is a polygon, force to MultiPolygon since we always produce multipolygons
if (wkbFlatten(pOGRGeometry->getGeometryType()) == wkbPolygon)
{
pOGRGeometry = OGRGeometryFactory::forceToMultiPolygon(pOGRGeometry);
}
else if (forceMulti)
{
if (wkbFlatten(pOGRGeometry->getGeometryType()) == wkbLineString)
{
pOGRGeometry = OGRGeometryFactory::forceToMultiLineString(pOGRGeometry);
}
else if (wkbFlatten(pOGRGeometry->getGeometryType()) == wkbPoint)
{
pOGRGeometry = OGRGeometryFactory::forceToMultiPoint(pOGRGeometry);
}
}
*ppOutGeometry = pOGRGeometry;
return true;
}
bool AOToOGRSpatialReference(esriGeometry::ISpatialReference* pSR, OGRSpatialReference** ppSR)
{
HRESULT hr;
if (pSR == NULL)
{
CPLError( CE_Warning, CPLE_AppDefined, "ESRI Spatial Reference is NULL");
return false;
}
esriGeometry::IESRISpatialReferenceGEN2Ptr ipSRGen = pSR;
if (ipSRGen == NULL)
{
CPLError( CE_Warning, CPLE_AppDefined, "ESRI Spatial Reference is Unknown");
return false;
}
long bufferSize = 0;
if (FAILED(hr = ipSRGen->get_ESRISpatialReferenceSize(&bufferSize)) || bufferSize == 0)
return false; //should never happen
BSTR buffer = ::SysAllocStringLen(NULL,bufferSize);
if (FAILED(hr = ipSRGen->ExportToESRISpatialReference2(&buffer, &bufferSize)))
{
::SysFreeString(buffer);
return AOErr(hr, "Failed to export ESRI string");
}
CW2A strESRIWKT(buffer);
::SysFreeString(buffer);
if (strlen(strESRIWKT) <= 0)
{
CPLError( CE_Warning, CPLE_AppDefined, "ESRI Spatial Reference is NULL");
return false;
}
*ppSR = new OGRSpatialReference(strESRIWKT);
OGRErr result = (*ppSR)->morphFromESRI();
if (result == OGRERR_NONE)
{
return true;
}
else
{
delete *ppSR;
*ppSR = NULL;
CPLError( CE_Failure, CPLE_AppDefined, "Failed morhping from ESRI Geometry: %s", strESRIWKT);
return false;
}
}
bool OGRGeometryToAOGeometry(OGRGeometry* pOGRGeom, esriGeometry::IGeometry** ppGeometry)
{
HRESULT hr;
*ppGeometry = NULL;
GByte* pWKB = NULL;
long wkbSize = pOGRGeom->WkbSize();
pWKB = (GByte *) CPLMalloc(wkbSize);
if( pOGRGeom->exportToWkb( wkbNDR, pWKB ) != OGRERR_NONE )
{
CPLFree (pWKB);
CPLError( CE_Failure, CPLE_AppDefined, "Could not export OGR geometry to WKB");
return false;
}
long bytesRead;
esriGeometry::IGeometryFactoryPtr ipGeomFact(esriGeometry::CLSID_GeometryEnvironment);
hr = ipGeomFact->CreateGeometryFromWkb(&bytesRead, pWKB, ppGeometry);
CPLFree (pWKB);
if (FAILED(hr))
{
return AOErr(hr, "Failed translating OGR geometry to ESRI Geometry");
}
return true;
}
// Attempt to checkout a license from the top down
bool InitializeDriver(esriLicenseExtensionCode license)
{
IAoInitializePtr ipInit(CLSID_AoInitialize);
if (license == 0)
{
// Try to init as engine, then engineGeoDB, then ArcView,
// then ArcEditor, then ArcInfo
if (!InitAttemptWithoutExtension(esriLicenseProductCodeEngine))
if (!InitAttemptWithoutExtension(esriLicenseProductCodeArcView))
if (!InitAttemptWithoutExtension(esriLicenseProductCodeArcEditor))
if (!InitAttemptWithoutExtension(esriLicenseProductCodeArcInfo))
{
// No appropriate license is available
CPLError( CE_Failure, CPLE_AppDefined, "ArcGIS License checkout failed.");
return false;
}
return true;
}
// Try to init as engine, then engineGeoDB, then ArcView,
// then ArcEditor, then ArcInfo
if (!InitAttemptWithExtension(esriLicenseProductCodeEngine,license))
if (!InitAttemptWithExtension(esriLicenseProductCodeArcView, license))
if (!InitAttemptWithExtension(esriLicenseProductCodeArcEditor, license))
if (!InitAttemptWithExtension(esriLicenseProductCodeArcInfo, license))
{
// No appropriate license is available
CPLError( CE_Failure, CPLE_AppDefined, "ArcGIS License checkout failed.");
return false;
}
return true;
}
// Attempt to initialize without an extension
bool InitAttemptWithoutExtension(esriLicenseProductCode product)
{
IAoInitializePtr ipInit(CLSID_AoInitialize);
esriLicenseStatus status = esriLicenseFailure;
ipInit->Initialize(product, &status);
return (status == esriLicenseCheckedOut);
}
// Attempt to initialize with an extension
bool InitAttemptWithExtension(esriLicenseProductCode product,
esriLicenseExtensionCode extension)
{
IAoInitializePtr ipInit(CLSID_AoInitialize);
esriLicenseStatus licenseStatus = esriLicenseFailure;
ipInit->IsExtensionCodeAvailable(product, extension, &licenseStatus);
if (licenseStatus == esriLicenseAvailable)
{
ipInit->Initialize(product, &licenseStatus);
if (licenseStatus == esriLicenseCheckedOut)
ipInit->CheckOutExtension(extension, &licenseStatus);
}
return (licenseStatus == esriLicenseCheckedOut);
}
// Shutdown the driver and check-in the license if needed.
HRESULT ShutdownDriver(esriLicenseExtensionCode license)
{
HRESULT hr;
// Scope ipInit so released before AoUninitialize call
{
IAoInitializePtr ipInit(CLSID_AoInitialize);
esriLicenseStatus status;
if (license != NULL)
{
hr = ipInit->CheckInExtension(license, &status);
if (FAILED(hr) || status != esriLicenseCheckedIn)
CPLError( CE_Failure, CPLE_AppDefined, "License checkin failed.");
}
hr = ipInit->Shutdown();
}
return hr;
}
int GetInitedProductCode()
{
HRESULT hr;
IAoInitializePtr ipAO(CLSID_AoInitialize);
esriLicenseProductCode code;
if (FAILED(hr = ipAO->InitializedProduct(&code)))
return -1;
return static_cast<int>(code);
}
|