public int QueryValues(IRowBuffer row) { try { if (m_currentOGRFeature == null) { return(-1); } // loop and only set values in the rowbuffer // that were requested from the fieldMap int count = m_esriQueryFieldMap.GetLength(0); for (int i = 0; i < count; i++) { int esriFieldIndex = (int)m_esriQueryFieldMap.GetValue(i); // have to skip 1) objectid and 2) geometry field or we will get an ESRI // exception. Also have to skip anything that the query map is asking to ignore if (esriFieldIndex == -1 || esriFieldIndex == m_pDataset.get_OIDFieldIndex(0) || esriFieldIndex == m_pDataset.get_ShapeFieldIndex(0)) { continue; } try { IField valField = m_pDataset.get_Fields(0).get_Field(i); object val = m_pDataset.get_mapped_value(m_currentOGRFeature, i); row.set_Value(i, val); } catch (Exception ex) { // skip values that fail to be set but continue doing it anyway System.Diagnostics.Debug.WriteLine(ex.Message); } } return(m_currentOGRFeature.GetFID()); } catch (Exception ex) { System.Diagnostics.Debug.WriteLine(ex.Message); return(-1); } }
/// <summary> /// Spatial filter using OGR /// </summary> /// <param name="featureSet">FeatureSet</param> /// <param name="geometry">Spatial geometry</param> /// <returns>Indexs of selected features</returns> public static List <int> OgrSpatialFilter(IFeatureSet featureSet, IGeometry geometry) { List <int> result = new List <int>(); OSGeo.OGR.Layer layer = GIS.GDAL.VectorConverter.DS2OrgLayer(featureSet); OSGeo.OGR.Geometry geo = GIS.GDAL.VectorConverter.DS2OgrGeometry(geometry, featureSet); layer.SetSpatialFilter(geo); OSGeo.OGR.Feature feature = layer.GetNextFeature(); while (feature != null) { result.Add(Convert.ToInt32(feature.GetFID())); feature = layer.GetNextFeature(); } return(result); }
public int QueryValues(IRowBuffer row) { try { if (m_currentOGRFeature == null) { return(-1); } // Loop and only set values in the rowbuffer // that were requested by ArcGIS. // // Ignore the ArcObjects documentation because it says I should use the field map // and that is straight out incorrect. Only copy values if the fieldmap has any value // besides -1. Othewise, ignore whatever map it is asking to copy to and simply copy // to the right field. IFields pFields = m_pDataset.get_Fields(0); int fieldCount = pFields.FieldCount; for (int i = 0; i < fieldCount; i++) { int esriFieldIndex = (int)m_esriQueryFieldMap.GetValue(i); if (esriFieldIndex == -1 || i == m_pDataset.get_OIDFieldIndex(0) || i == m_pDataset.get_ShapeFieldIndex(0)) { continue; } IField pField = pFields.get_Field(i); object val = null; // DANGER - POTENTIAL BUG - Workaround. For some reason, in ArcGIS 10.1SP1 I am // getting fields that should not be editable passed in to map into!!!?!? I am // skipping those here. In theory, since we are the DataSource provider, we should be able to // use a lower level set value that skips polymorphic behavior and hence allows // the write to happen even in non-editable fields. Something analogous to // ITableWrite::WriteRow, but for Rows. We don't have that, so I skip those values for those // rows. if (!pFields.get_Field(i).Editable) { continue; } try { val = m_pDataset.get_mapped_value(m_currentOGRFeature, i); row.set_Value(i, val); } catch (Exception ex) { // skip values that fail to be set but continue doing it anyway string msg = String.Format("OGRFID:[{0}] esriFieldName:[{1}] esriFieldIndex:[{2}] esriValue:[{3}] Exception:[{4}]", m_currentOGRFeature.GetFID(), pField.Name, esriFieldIndex, val != null ? val.ToString() : "<not set>", ex.Message); System.Diagnostics.Debug.WriteLine(msg); } } return(m_currentOGRFeature.GetFID()); } catch (Exception ex) { System.Diagnostics.Debug.WriteLine(ex.Message); return(-1); } }
/// <summary> /// Query by geometry /// </summary> /// <param name="geo">input geometry</param> /// <param name="layer">target layer</param> /// <returns></returns> public List <int> QueryByGeometry(OSGeo.OGR.Geometry geo, OSGeo.OGR.Layer layer, SpatialRel sr) { List <int> result = new List <int>(); try { OSGeo.OGR.Feature oFeature = null; while ((oFeature = layer.GetNextFeature()) != null) { OSGeo.OGR.Geometry oGeometry = oFeature.GetGeometryRef(); switch (sr) { case SpatialQuery.SpatialRel.Undefined: break; case SpatialQuery.SpatialRel.Intersects: if (geo.Intersect(oGeometry)) { result.Add((int)oFeature.GetFID()); } break; case SpatialQuery.SpatialRel.Contains: if (oGeometry.Contains(geo)) { result.Add((int)oFeature.GetFID()); } break; case SpatialQuery.SpatialRel.Crosses: if (geo.Crosses(oGeometry)) { result.Add((int)oFeature.GetFID()); } break; case SpatialQuery.SpatialRel.Overlaps: if (geo.Overlaps(oGeometry)) { result.Add((int)oFeature.GetFID()); } break; case SpatialQuery.SpatialRel.Touches: if (geo.Touches(oGeometry)) { result.Add((int)oFeature.GetFID()); } break; case SpatialQuery.SpatialRel.WithIn: if (oGeometry.Within(geo)) { result.Add((int)oFeature.GetFID()); } break; } } } catch { throw; } return(result); }