void CFields::RebuildFieldIndexMap() const { m_mapFieldIndex.clear(); for(uint32 i = 0; i < m_vecFields.size(); ++i) { IFieldPtr field = m_vecFields[i]; if(!field.get()) continue; m_mapFieldIndex.insert(TMapFieldIndex::value_type(field->getName(), (int)i)); } }
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; }
STDMETHODIMP CSimplePointDatasetHelper::get_Fields(long ClassIndex, IFields **FieldSet) { if (! FieldSet) return E_POINTER; HRESULT hr; // First get the standard fields for an feature class IObjectClassDescriptionPtr ipOCDesc; hr = ipOCDesc.CreateInstance(CLSID_FeatureClassDescription); if (FAILED(hr)) return hr; IFieldsPtr ipFields; hr = ipOCDesc->get_RequiredFields(&ipFields); if (FAILED(hr)) return hr; IFieldPtr ipField; // We will have: a shape field name of "shape" // an UnknownCoordinateSystem // Just need to change the geometry type to Point long i; long lNumFields; esriFieldType eFieldType; hr = ipFields->get_FieldCount(&lNumFields); if (FAILED(hr)) return hr; for (i=0; i < lNumFields; i++) { hr = ipFields->get_Field(i, &ipField); if (FAILED(hr)) return hr; hr = ipField->get_Type(&eFieldType); if (eFieldType == esriFieldTypeGeometry) { IGeometryDefPtr ipGeomDef; hr = ipField->get_GeometryDef(&ipGeomDef); if (FAILED(hr)) return hr; IGeometryDefEditPtr ipGeomDefEdit = ipGeomDef; if (ipGeomDefEdit == NULL) return E_FAIL; hr = ipGeomDefEdit->put_GeometryType(esriGeometryPoint); if (FAILED(hr)) return hr; } } // Add the extra text field IFieldEditPtr ipFieldEdit; hr = ipFieldEdit.CreateInstance(CLSID_Field); if (FAILED(hr)) return hr; hr = ipFieldEdit->put_Name(L"Column1"); if (FAILED(hr)) return hr; hr = ipFieldEdit->put_Type(esriFieldTypeString); if (FAILED(hr)) return hr; hr = ipFieldEdit->put_Length(1); if (FAILED(hr)) return hr; IFieldsEditPtr ipFieldsEdit = ipFields; if (ipFieldsEdit == NULL) return E_FAIL; ipField = ipFieldEdit; if (ipField == NULL) return E_FAIL; hr = ipFieldsEdit->AddField(ipField); if (FAILED(hr)) return hr; *FieldSet = ipFields.Detach(); // pass ownership of object to client return S_OK; }
bool AOLayer::Initialize(ITable* pTable) { HRESULT hr; m_ipTable = pTable; CComBSTR temp; IDatasetPtr ipDataset = m_ipTable; if (FAILED(hr = ipDataset->get_Name(&temp))) return false; m_pFeatureDefn = new OGRFeatureDefn(CW2A(temp)); //Should I "new" an OGR smart pointer - sample says so, but it doesn't seem right //as long as we use the same compiler & settings in both the ogr build and this //driver, we should be OK m_pFeatureDefn->Reference(); IFeatureClassPtr ipFC = m_ipTable; VARIANT_BOOL hasOID = VARIANT_FALSE; ipFC->get_HasOID(&hasOID); if (hasOID == VARIANT_TRUE) { ipFC->get_OIDFieldName(&temp); m_strOIDFieldName = CW2A(temp); } if (FAILED(hr = ipFC->get_ShapeFieldName(&temp))) return AOErr(hr, "No shape field found!"); m_strShapeFieldName = CW2A(temp); IFieldsPtr ipFields; if (FAILED(hr = ipFC->get_Fields(&ipFields))) return AOErr(hr, "Fields not found!"); long shapeIndex = -1; if (FAILED(hr = ipFields->FindField(temp, &shapeIndex))) return AOErr(hr, "Shape field not found!"); IFieldPtr ipShapeField; if (FAILED(hr = ipFields->get_Field(shapeIndex, &ipShapeField))) return false; // Use GeometryDef to set OGR shapetype and Spatial Reference information // IGeometryDefPtr ipGeoDef; if (FAILED(hr = ipShapeField->get_GeometryDef(&ipGeoDef))) return false; OGRwkbGeometryType ogrGeoType; if (!AOToOGRGeometry(ipGeoDef, &ogrGeoType)) return false; m_pFeatureDefn->SetGeomType(ogrGeoType); if (wkbFlatten(ogrGeoType) == wkbMultiLineString || wkbFlatten(ogrGeoType) == wkbMultiPoint) m_forceMulti = true; // Mapping of Spatial Reference will be passive about errors // (it is possible we won't be able to map some ESRI-specific projections) esriGeometry::ISpatialReferencePtr ipSR = NULL; if (FAILED(hr = ipGeoDef->get_SpatialReference(&ipSR))) { AOErr(hr, "Failed Fetching ESRI spatial reference"); } else { if (!AOToOGRSpatialReference(ipSR, &m_pSRS)) { //report error, but be passive about it CPLError( CE_Warning, CPLE_AppDefined, "Failed Mapping ESRI Spatial Reference"); } } // Map fields // return AOToOGRFields(ipFields, m_pFeatureDefn, m_OGRFieldToESRIField); }
int64 SQLiteInsertCursor::InsertRow(IRow* pRow) { if(!m_bInit) { init(); m_bInit = true; } if(!m_bValidCursor) return -1; IFeature *pFeature = NULL; //IFieldSetPtr pFieldSet = pRow->GetFieldSet(); //if(!pFieldSet.get() || pFieldSet->GetCount() == 0) // pFieldSet = m_pFieldSet; { //for (int i = 0; i < pFieldSet->GetCount(); ++i) for (int i = 0, sz = (int)m_vecTypes.size(); i < sz; ++i) { /*const CommonLib::CString& sName = pFieldSet->Get(i); TFieldsInfo::const_iterator c_it = m_mapFieldInfo.find(sName); if(c_it == m_mapFieldInfo.end()) { //TO DO Error return -1; }*/ if(m_vecTypes[i] == dtGeometry) { pFeature = (IFeature *)pRow; if(!pFeature) { //TO DO Error return -1; } CommonLib::IGeoShapePtr pShape = pFeature->GetShape(); if(pShape.get()) { //pShape->compress(&m_WriteShapeStream, &m_comp_params); if (!pShape->IsSuccinct()) pShape->InnerEncode(&m_WriteShapeStream, &m_comp_params); m_pStmt->ColumnBindBlob(i + 1, pShape->buffer(), pShape->size()); m_WriteShapeStream.seek(0, CommonLib::soFromBegin); } } else { m_pStmt->ColumnBind(i + 1, m_vecTypes[i], pRow->GetValue(i)); } if(m_pStmt->IsError()) { //TO DO error m_bValidCursor = false; return -1; } } m_pStmt->StepNext(); if(m_pStmt->IsError()) { //TO DO error m_bValidCursor = false; return -1; } m_pStmt->reset(); } int64 nLastRowID = m_pDB->GetLastInsertRowID(); if(pFeature) { IFieldPtr pOIDField = pRow->GetSourceFields()->GetField(m_pTable->GetOIDFieldName()); int64 nOID = -1; if(m_pTable->HasOIDField()) nOID = pRow->GetOID(); if(nOID == -1) nOID = nLastRowID; CommonLib::IGeoShapePtr pShape = pFeature->GetShape(); CommonLib::bbox bb; if (m_ShapeType == CommonLib::shape_type_general_multipoint) { if (!InsertMultiPointSpatialIndex(pShape, nOID, pOIDField->GetType())) return -1; } else { if (m_ShapeType == CommonLib::shape_type_general_polygon || m_ShapeType == CommonLib::shape_type_general_polyline) { bb = pShape->getBB(); } else if (m_ShapeType == CommonLib::shape_type_general_point) { auto *pXY = pShape->getPoints(); bb.xMax = pXY->x; bb.xMin = pXY->x; bb.yMax = pXY->y; bb.yMin = pXY->y; } if (!InsertSpatialIndex(bb, nOID, pOIDField->GetType())) return -1; } } pRow->SetRowID(nLastRowID); return nLastRowID; }
void SQLiteInsertCursor::init() { CommonLib::CString sSQL = L"INSERT INTO " + m_pTable->GetDatasetName() + L" ( "; CommonLib::CString sValues = " VALUES ( "; if(!m_pFieldSet.get()) { m_pFieldSet = new CFieldSet(); for (int i = 0, sz = m_pSourceFields->GetFieldCount(); i < sz; ++i) { IFieldPtr pField = m_pSourceFields->GetField(i); m_pFieldSet->Add(pField->GetName()); } } for (int i = 0; i < m_pFieldSet->GetCount(); ++i) { const CommonLib::CString& sFieldName = m_pFieldSet->Get(i); IFieldPtr pField = m_pSourceFields->GetField(sFieldName); if(!pField.get()) { //TO DO error m_bValidCursor = false; } //m_mapFieldInfo.insert(std::make_pair(sFieldName, SFieldInfo(i, pField->GetType()))); m_vecTypes.push_back(pField->GetType()); if(i != 0) { sSQL += L", "; sValues += L", "; } sSQL += m_pFieldSet->Get(i); sValues += "?"; } sValues += L" )"; sSQL += L" )"; sSQL += sValues; m_pStmt = m_pDB->prepare_query(sSQL); if(!m_pStmt.get()) { //TO DO error m_bValidCursor = false; return; } if(m_pTable->GetDatasetType() == dtFeatureClass) { CSQLiteFeatureClass *pFC = dynamic_cast<CSQLiteFeatureClass*>(m_pTable.get()); assert(pFC); const CommonLib::CString& sRTreeIndex = pFC->GetRTReeIndexName(); CommonLib::CString sSQL = L"INSERT INTO " + sRTreeIndex + L"(feature_id, minX, maxX, minY, maxY)" + " VALUES (?, ?, ?, ?, ?)"; m_pStmtSpatial = m_pDB->prepare_query(sSQL); if(!m_pStmtSpatial.get()) { m_bValidCursor = false; } GisGeometry::IEnvelopePtr pEnvelope = pFC->GetExtent(); m_comp_params = pEnvelope->GetCompressParams(); m_ShapeType = CommonLib::CGeoShape::GetGeneralType(pFC->GetGeometryType()); } }