示例#1
0
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;
}