public IEnumerable <IFeature> GetFeatures(IEnvelope bbox, string filter, double scale) { //are we cached? Polygon p = null; foreach (IEnvelope area in m_cachedareas.Query(bbox)) { if (p == null) { p = ConvertEnvelopeToPolygon(area); } else { p.Union(ConvertEnvelopeToPolygon(area)); } } if (p != null && p.Covers(ConvertEnvelopeToPolygon(bbox))) { //load from cache LinkedList <IFeature> ret = new LinkedList <IFeature>(); m_objects.Query(bbox, new FeatureLinkedListVisitor(ret)); return(ret); } OpenConnection(); //load from DB IDbCommand cmd = m_conn.CreateCommand(); cmd.CommandText = "SELECT " + m_PrimaryColumn + ", " + m_WKBColumn + " FROM " + m_tablename + " WHERE " + m_X1Column + " >= " + bbox.MinX.ToString(System.Globalization.CultureInfo.InvariantCulture) + " AND " + m_X1Column + " <= " + bbox.MaxX.ToString(System.Globalization.CultureInfo.InvariantCulture) + " AND " + m_Y1Column + " >= " + bbox.MinY.ToString(System.Globalization.CultureInfo.InvariantCulture) + " AND " + m_Y1Column + " <= " + bbox.MaxY.ToString(System.Globalization.CultureInfo.InvariantCulture) + " OR " + m_X2Column + " >= " + bbox.MinX.ToString(System.Globalization.CultureInfo.InvariantCulture) + " AND " + m_X2Column + " <= " + bbox.MaxX.ToString(System.Globalization.CultureInfo.InvariantCulture) + " AND " + m_Y1Column + " >= " + bbox.MinY.ToString(System.Globalization.CultureInfo.InvariantCulture) + " AND " + m_Y1Column + " <= " + bbox.MaxY.ToString(System.Globalization.CultureInfo.InvariantCulture) + " OR " + m_X2Column + " >= " + bbox.MinX.ToString(System.Globalization.CultureInfo.InvariantCulture) + " AND " + m_X2Column + " <= " + bbox.MaxX.ToString(System.Globalization.CultureInfo.InvariantCulture) + " AND " + m_Y2Column + " >= " + bbox.MinY.ToString(System.Globalization.CultureInfo.InvariantCulture) + " AND " + m_Y2Column + " <= " + bbox.MaxY.ToString(System.Globalization.CultureInfo.InvariantCulture) + " OR " + m_X1Column + " >= " + bbox.MinX.ToString(System.Globalization.CultureInfo.InvariantCulture) + " AND " + m_X1Column + " <= " + bbox.MaxX.ToString(System.Globalization.CultureInfo.InvariantCulture) + " AND " + m_Y2Column + " >= " + bbox.MinY.ToString(System.Globalization.CultureInfo.InvariantCulture) + " AND " + m_Y2Column + " <= " + bbox.MaxY.ToString(System.Globalization.CultureInfo.InvariantCulture); IDataReader dr = null; LinkedList <IFeature> fromdb = new LinkedList <IFeature>(); try { dr = cmd.ExecuteReader(); Topology.IO.WKBReader wkbreader = new Topology.IO.WKBReader(); int nmap = dr.GetOrdinal(m_WKBColumn); int npk = dr.GetOrdinal(m_PrimaryColumn); while (dr.Read()) { IGeometry g = wkbreader.Read((byte[])dr.GetValue(nmap)); int pk = (int)dr.GetValue(npk); SimpleFeature f = new SimpleFeature(g, m_areastyle, pk); if (!m_PrimaryKeyIndex.ContainsKey(pk)) { fromdb.AddLast(f); m_PrimaryKeyIndex.Add(pk, f); m_objects.Insert(g.EnvelopeInternal, f); } else { fromdb.AddLast(m_PrimaryKeyIndex[pk]); } } } catch (Exception ex) { throw new Exception("Couldn't load features\nError: " + ex.Message); } finally { if (dr != null) { dr.Close(); } } //add to cache m_cachedareas.Insert(bbox, bbox); return(fromdb); }
public IEnumerable<IFeature> GetFeatures(IEnvelope bbox, string filter, double scale) { //are we cached? Polygon p = null; foreach (IEnvelope area in m_cachedareas.Query(bbox)) { if (p == null) p = ConvertEnvelopeToPolygon(area); else p.Union(ConvertEnvelopeToPolygon(area)); } if(p != null && p.Covers(ConvertEnvelopeToPolygon(bbox))) { //load from cache LinkedList<IFeature> ret = new LinkedList<IFeature>(); m_objects.Query(bbox, new FeatureLinkedListVisitor(ret)); return ret; } OpenConnection(); //load from DB IDbCommand cmd = m_conn.CreateCommand(); cmd.CommandText = "SELECT " + m_PrimaryColumn + ", " + m_WKBColumn + " FROM " + m_tablename + " WHERE " + m_X1Column + " >= " + bbox.MinX.ToString(System.Globalization.CultureInfo.InvariantCulture) + " AND " + m_X1Column + " <= " + bbox.MaxX.ToString(System.Globalization.CultureInfo.InvariantCulture) + " AND " + m_Y1Column + " >= " + bbox.MinY.ToString(System.Globalization.CultureInfo.InvariantCulture) + " AND " + m_Y1Column + " <= " + bbox.MaxY.ToString(System.Globalization.CultureInfo.InvariantCulture) + " OR " + m_X2Column + " >= " + bbox.MinX.ToString(System.Globalization.CultureInfo.InvariantCulture) + " AND " + m_X2Column + " <= " + bbox.MaxX.ToString(System.Globalization.CultureInfo.InvariantCulture) + " AND " + m_Y1Column + " >= " + bbox.MinY.ToString(System.Globalization.CultureInfo.InvariantCulture) + " AND " + m_Y1Column + " <= " + bbox.MaxY.ToString(System.Globalization.CultureInfo.InvariantCulture) + " OR " + m_X2Column + " >= " + bbox.MinX.ToString(System.Globalization.CultureInfo.InvariantCulture) + " AND " + m_X2Column + " <= " + bbox.MaxX.ToString(System.Globalization.CultureInfo.InvariantCulture) + " AND " + m_Y2Column + " >= " + bbox.MinY.ToString(System.Globalization.CultureInfo.InvariantCulture) + " AND " + m_Y2Column + " <= " + bbox.MaxY.ToString(System.Globalization.CultureInfo.InvariantCulture) + " OR " + m_X1Column + " >= " + bbox.MinX.ToString(System.Globalization.CultureInfo.InvariantCulture) + " AND " + m_X1Column + " <= " + bbox.MaxX.ToString(System.Globalization.CultureInfo.InvariantCulture) + " AND " + m_Y2Column + " >= " + bbox.MinY.ToString(System.Globalization.CultureInfo.InvariantCulture) + " AND " + m_Y2Column + " <= " + bbox.MaxY.ToString(System.Globalization.CultureInfo.InvariantCulture); IDataReader dr = null; LinkedList<IFeature> fromdb = new LinkedList<IFeature>(); try { dr = cmd.ExecuteReader(); Topology.IO.WKBReader wkbreader = new Topology.IO.WKBReader(); int nmap = dr.GetOrdinal(m_WKBColumn); int npk = dr.GetOrdinal(m_PrimaryColumn); while (dr.Read()) { IGeometry g = wkbreader.Read((byte[])dr.GetValue(nmap)); int pk = (int)dr.GetValue(npk); SimpleFeature f = new SimpleFeature(g, m_areastyle, pk); if (!m_PrimaryKeyIndex.ContainsKey(pk)) { fromdb.AddLast(f); m_PrimaryKeyIndex.Add(pk, f); m_objects.Insert(g.EnvelopeInternal, f); } else fromdb.AddLast(m_PrimaryKeyIndex[pk]); } } catch (Exception ex) { throw new Exception("Couldn't load features\nError: " + ex.Message); } finally { if (dr != null) dr.Close(); } //add to cache m_cachedareas.Insert(bbox, bbox); return fromdb; }