/// <summary> /// Retrieves all features within the given BoundingBox. /// </summary> /// <param name="bounds">Bounds of the region to search.</param> /// <param name="fcs">FeatureDataSet to fill data into</param> public override void ExecuteIntersectionQuery(Envelope bounds, IFeatureCollectionSet fcs, CancellationToken?cancellationToken = null) { if (Table.Rows.Count == 0) { return; } string statement = XColumn + " > " + bounds.MinX.ToString(Map.NumberFormatEnUs) + " AND " + XColumn + " < " + bounds.MaxX.ToString(Map.NumberFormatEnUs) + " AND " + YColumn + " > " + bounds.MinY.ToString(Map.NumberFormatEnUs) + " AND " + YColumn + " < " + bounds.MaxY.ToString(Map.NumberFormatEnUs); var rows = Table.Select(statement); var fdt = new FeatureDataTable(Table); foreach (DataColumn col in Table.Columns) { fdt.Columns.Add(col.ColumnName, col.DataType, col.Expression); } foreach (var dr in rows) { fdt.ImportRow(dr); var fdr = (FeatureDataRow)fdt.Rows[fdt.Rows.Count - 1]; fdr.Geometry = Factory.CreatePoint(new Coordinate((double)dr[XColumn], (double)dr[YColumn])); } fcs.Add(fdt); }
public void ExecuteIntersectionQuery(IGeometry geom, IFeatureCollectionSet ds, CancellationToken?cancellationToken = null) { var fdt = (FeatureDataTable)_schemaTable.Copy(); /* // NOTE WHY IS THIS, No other provider behaves like that? * if (ds.Tables.Count > 0) * { * fdt = ds.Tables[0]; * } * else * { * fdt = new FeatureDataTable(); * }*/ var pGeom = NetTopologySuite.Geometries.Prepared.PreparedGeometryFactory.Prepare(geom); fdt.BeginLoadData(); foreach (var feature in _geometrys) { feature.Value.Where(pGeom.Intersects).ToList() .ForEach(v => { var newRow = (FeatureDataRow)fdt.LoadDataRow(GetAssetProperties(feature.Key), true); newRow.Geometry = v; } ); } fdt.EndLoadData(); ds.Add(fdt); }
protected override void OnExecuteIntersectionQuery(IGeometry geom, IFeatureCollectionSet ds, CancellationToken?cancellationToken = null) { var fds = new FeatureDataSet(); ExecuteIntersectionQuery(geom.EnvelopeInternal, fds); //index of last added feature data table var index = fds.Tables.Count - 1; if (index <= 0) { return; } var res = (FeatureDataTable)CloneTableStructure(fds.Tables[index].Clone()); res.BeginLoadData(); var fdt = fds.Tables[index]; foreach (FeatureDataRow row in fdt.Rows) { if (PreparedGeometry.Intersects(row.Geometry)) { var fdr = (FeatureDataRow)res.LoadDataRow(row.ItemArray, true); fdr.Geometry = row.Geometry; } } res.EndLoadData(); ds.Add(res); fds.Dispose(); }
/// <summary> /// Returns the features that intersects with 'geom' /// </summary> /// <param name="geom"></param> /// <param name="fcs">FeatureDataSet to fill data into</param> /// <param name="cancellationToken">A cancellation token</param> protected override void OnExecuteIntersectionQuery(Geometry geom, IFeatureCollectionSet fcs, CancellationToken?cancellationToken = null) { using (SqlConnection conn = new SqlConnection(ConnectionString)) { string strGeom = SpatialObjectType + "::STGeomFromText('" + geom.AsText() + "', #SRID#)"; strGeom = strGeom.Replace("#SRID#", SRID > 0 ? SRID.ToString(NumberFormatInfo.InvariantInfo) : "0"); strGeom = GeometryColumn + ".STIntersects(" + strGeom + ") = 1"; string strSQL = "SELECT g.* FROM " + Table + " g " + BuildTableHints() + " WHERE "; if (!String.IsNullOrEmpty(DefinitionQuery)) { strSQL += DefinitionQuery + " AND "; } strSQL += strGeom; string extraOptions = GetExtraOptions(); if (!string.IsNullOrEmpty(extraOptions)) { strSQL += " " + extraOptions; } var ds = (System.Data.DataSet) new FeatureDataSet(); using (SqlDataAdapter adapter = new SqlDataAdapter(strSQL, conn)) { conn.Open(); adapter.Fill(ds); conn.Close(); if (ds.Tables.Count > 0) { FeatureDataTable fdt = new FeatureDataTable(ds.Tables[0]); foreach (System.Data.DataColumn col in ds.Tables[0].Columns) { if (col.ColumnName != GeometryColumn) { fdt.Columns.Add(col.ColumnName, col.DataType, col.Expression); } } var geometryReader = CreateReader(); foreach (System.Data.DataRow dr in ds.Tables[0].Rows) { FeatureDataRow fdr = fdt.NewRow(); foreach (System.Data.DataColumn col in ds.Tables[0].Columns) { if (col.ColumnName != GeometryColumn) { fdr[col.ColumnName] = dr[col]; } } fdr.Geometry = geometryReader.Read(dr[GeometryColumn]); fdt.AddRow(fdr); } fcs.Add(fdt); } } } }
/// <summary> /// Returns all features with the view box /// </summary> /// <param name="bbox">view box</param> /// <param name="fcs">FeatureDataSet to fill data into</param> /// <param name="cancellationToken">A cancellation token</param> public override void ExecuteIntersectionQuery(BoundingBox bbox, IFeatureCollectionSet fcs, CancellationToken?cancellationToken = null) { using (SqlConnection conn = new SqlConnection(ConnectionString)) { //Get bounding box string string strBbox = GetBoxFilterStr(bbox); string strSQL = String.Format( "SELECT g.* FROM {0} g {1} WHERE ", Table, BuildTableHints()); if (!String.IsNullOrEmpty(DefinitionQuery)) { strSQL += DefinitionQuery + " AND "; } strSQL += strBbox; string extraOptions = GetExtraOptions(); if (!string.IsNullOrEmpty(extraOptions)) { strSQL += " " + extraOptions; } using (SqlDataAdapter adapter = new SqlDataAdapter(strSQL, conn)) { conn.Open(); System.Data.DataSet ds2 = new System.Data.DataSet(); adapter.Fill(ds2); conn.Close(); if (ds2.Tables.Count > 0) { FeatureDataTable fdt = new FeatureDataTable(ds2.Tables[0]); foreach (System.Data.DataColumn col in ds2.Tables[0].Columns) { if (col.ColumnName != GeometryColumn) { fdt.Columns.Add(col.ColumnName, col.DataType, col.Expression); } } var geometryReader = CreateReader(); foreach (System.Data.DataRow dr in ds2.Tables[0].Rows) { FeatureDataRow fdr = fdt.NewRow(); foreach (System.Data.DataColumn col in ds2.Tables[0].Columns) { if (col.ColumnName != GeometryColumn) { fdr[col.ColumnName] = dr[col]; } } fdr.Geometry = geometryReader.Read(dr[GeometryColumn]); fdt.AddRow(fdr); } fcs.Add(fdt); } } } }
public override void ExecuteIntersectionQuery(Envelope box, IFeatureCollectionSet fcs, CancellationToken?ct = null) { using (var conn = SpatiaLiteConnection(ConnectionString)) { string cols = "*"; //If using rowid as oid, we need to explicitly request it! if (String.Compare(ObjectIdColumn, "rowid", StringComparison.OrdinalIgnoreCase) == 0) { cols = "rowid,*"; } var strSql = "SELECT " + cols + ", AsBinary(" + GeometryColumn + ") AS sharpmap_tempgeometry "; strSql += "FROM " + Table + " WHERE "; strSql += GetBoxClause(box); if (!String.IsNullOrEmpty(_definitionQuery)) { strSql += " AND " + DefinitionQuery; } using (var adapter = new SQLiteDataAdapter(strSql, conn)) { var ds2 = new DataSet(); adapter.Fill(ds2); conn.Close(); if (ds2.Tables.Count > 0) { var fdt = new FeatureDataTable(ds2.Tables[0]); foreach (DataColumn col in ds2.Tables[0].Columns) { if (col.ColumnName != GeometryColumn && col.ColumnName != "sharpmap_tempgeometry" && !col.ColumnName.StartsWith("Envelope_")) { fdt.Columns.Add(col.ColumnName, col.DataType, col.Expression); } } foreach (DataRow dr in ds2.Tables[0].Rows) { var fdr = fdt.NewRow(); foreach (DataColumn col in ds2.Tables[0].Columns) { if (col.ColumnName != GeometryColumn && col.ColumnName != "sharpmap_tempgeometry" && !col.ColumnName.StartsWith("Envelope_")) { fdr[col.ColumnName] = dr[col]; } } if (dr["sharpmap_tempgeometry"] != DBNull.Value) { fdr.Geometry = GeometryFromWKB.Parse((byte[])dr["sharpmap_tempgeometry"], Factory); } fdt.AddRow(fdr); } fcs.Add(fdt); } } } }
/// <summary> /// Returns the features that intersects with 'geom' /// </summary> /// <param name="geom"></param> /// <param name="fcs">FeatureCollectionSet to fill data into</param> protected override void OnExecuteIntersectionQuery(IGeometry geom, IFeatureCollectionSet fcs, CancellationToken?cancellationToken = null) { using (var conn = new OracleConnection(ConnectionString)) { var strGeom = "MDSYS.SDO_GEOMETRY('" + geom.AsText() + "', #SRID#)"; strGeom = strGeom.Replace("#SRID#", SRID > 0 ? SRID.ToString(Map.NumberFormatEnUs) : "NULL"); strGeom = "SDO_RELATE(g." + GeometryColumn + ", " + strGeom + ", 'mask=ANYINTERACT querytype=WINDOW') = 'TRUE'"; var strSQL = "SELECT g.* , g." + GeometryColumn + ").Get_WKB() As sharpmap_tempgeometry FROM " + Table + " g WHERE "; if (!String.IsNullOrEmpty(_definitionQuery)) { strSQL += DefinitionQuery + " AND "; } strSQL += strGeom; var ds = new FeatureDataSet(); using (var adapter = new OracleDataAdapter(strSQL, conn)) { conn.Open(); adapter.Fill(ds); conn.Close(); if (ds.Tables.Count <= 0) { return; } var fdt = new FeatureDataTable(ds.Tables[0]); foreach (DataColumn col in ds.Tables[0].Columns) { if (col.ColumnName != GeometryColumn && col.ColumnName != "sharpmap_tempgeometry") { fdt.Columns.Add(col.ColumnName, col.DataType, col.Expression); } } foreach (DataRow dr in ds.Tables[0].Rows) { var fdr = fdt.NewRow(); foreach (DataColumn col in ds.Tables[0].Columns) { if (col.ColumnName != GeometryColumn && col.ColumnName != "sharpmap_tempgeometry") { fdr[col.ColumnName] = dr[col]; } } fdr.Geometry = GeometryFromWKB.Parse((byte[])dr["sharpmap_tempgeometry"], Factory); fdt.AddRow(fdr); } fcs.Add(fdt); } } }
/// <summary> /// Returns all features with the view box /// </summary> /// <param name="bbox">view box</param> /// <param name="fcs">FeatureCollectionSet to fill data into</param> public override void ExecuteIntersectionQuery(Envelope bbox, IFeatureCollectionSet fcs, CancellationToken?cancellationToken = null) { using (var conn = new OracleConnection(ConnectionString)) { //Get bounding box string var strBbox = GetBoxFilterStr(bbox); var strSql = "SELECT * "; strSql += "FROM " + Table + " g WHERE "; if (!String.IsNullOrEmpty(_definitionQuery)) { strSql += DefinitionQuery + " AND "; } strSql += strBbox; using (var adapter = new OracleDataAdapter(strSql, conn)) { conn.Open(); var ds2 = new DataSet(); adapter.Fill(ds2); conn.Close(); if (ds2.Tables.Count > 0) { var fdt = new FeatureDataTable(ds2.Tables[0]); foreach (DataColumn col in ds2.Tables[0].Columns) { if (string.Compare(col.ColumnName, GeometryColumn, CultureInfo.InvariantCulture, CompareOptions.OrdinalIgnoreCase) != 0) { fdt.Columns.Add(col.ColumnName, col.DataType, col.Expression); } } foreach (DataRow dr in ds2.Tables[0].Rows) { FeatureDataRow fdr = fdt.NewRow(); foreach (DataColumn col in ds2.Tables[0].Columns) { if (string.Compare(col.ColumnName, GeometryColumn, CultureInfo.InvariantCulture, CompareOptions.OrdinalIgnoreCase) != 0) { fdr[col.ColumnName] = dr[col]; } } var sdoGeom = dr[GeometryColumn] as SdoGeometry; if (sdoGeom != null) { fdr.Geometry = sdoGeom.AsGeometry(); } fdt.AddRow(fdr); } fcs.Add(fdt); } } } }
/// <summary> /// Throws an NotSupportedException. Attribute data is not supported by this datasource /// </summary> /// <param name="view"></param> /// <param name="ds">FeatureDataSet to fill data into</param> public void ExecuteIntersectionQuery(Envelope view, IFeatureCollectionSet ds, CancellationToken?cancellationToken = null) { IFeatureCollection fdt; lock (_featuresLock) fdt = _features.Clone(); fdt.AddRange(EnumerateFeatures(view)); ds.Add(fdt); }
/// <summary> /// Returns the data associated with all the geometries that are intersected by 'geom' /// </summary> /// <param name="bbox">Geometry to intersect with</param> /// <param name="ds">FeatureDataSet to fill data into</param> public override void ExecuteIntersectionQuery(BoundingBox bbox, IFeatureCollectionSet fcs, CancellationToken?ct = null) { _ogrLayer.SetSpatialFilterRect(bbox.MinX, bbox.MinY, bbox.MaxX, bbox.MaxY); var fds = new FeatureDataSet(); ExecuteIntersectionQuery(fds); foreach (var fd in fds) { fcs.Add(fd); } }
/// <summary> /// Throws an NotSupportedException. Attribute data is not supported by this datasource /// </summary> /// <param name="geom"></param> /// <param name="ds">FeatureDataSet to fill data into</param> public void ExecuteIntersectionQuery(IGeometry geom, IFeatureCollectionSet ds, CancellationToken?cancellationToken = null) { IFeatureCollection fdt; lock (_featuresLock) fdt = _features.Clone(); var pg = new NetTopologySuite.Geometries.Prepared.PreparedGeometryFactory().Create(geom); fdt.AddRange(EnumerateFeatures(pg)); ds.Add(fdt); }
public override void ExecuteIntersectionQuery(Envelope box, IFeatureCollectionSet fcs, CancellationToken?cancellationToken = null) { using (var conn = new SQLiteConnection(ConnectionID)) { string strSQL = "SELECT *, " + GeometryColumn + " AS sharpmap_tempgeometry "; strSQL += "FROM " + Table + " WHERE "; strSQL += GetBoxClause(box); if (!String.IsNullOrEmpty(_definitionQuery)) { strSQL += " AND " + DefinitionQuery; } using (SQLiteDataAdapter adapter = new SQLiteDataAdapter(strSQL, conn)) { conn.Open(); DataSet ds2 = new DataSet(); adapter.Fill(ds2); conn.Close(); if (ds2.Tables.Count > 0) { FeatureDataTable fdt = new FeatureDataTable(ds2.Tables[0]); foreach (DataColumn col in ds2.Tables[0].Columns) { if (col.ColumnName != GeometryColumn && col.ColumnName != "sharpmap_tempgeometry" && !col.ColumnName.StartsWith("Envelope_")) { fdt.Columns.Add(col.ColumnName, col.DataType, col.Expression); } } foreach (DataRow dr in ds2.Tables[0].Rows) { FeatureDataRow fdr = fdt.NewRow(); foreach (DataColumn col in ds2.Tables[0].Columns) { if (col.ColumnName != GeometryColumn && col.ColumnName != "sharpmap_tempgeometry" && !col.ColumnName.StartsWith("Envelope_")) { fdr[col.ColumnName] = dr[col]; } } if (dr["sharpmap_tempgeometry"] != DBNull.Value) { fdr.Geometry = GeometryFromWKT.Parse((string)dr["sharpmap_tempgeometry"]); } fdt.AddRow(fdr); } fcs.Add(fdt); } } } }
/// <summary> /// Returns the data associated with all the geometries that are intersected by 'geom' /// </summary> /// <param name="geom">Geometry to intersect with</param> /// <param name="ds">FeatureDataSet to fill data into</param> protected override void OnExecuteIntersectionQuery(Geometry geom, IFeatureCollectionSet fcs, CancellationToken?ct = null) { using (var ogrGeometry = OgrGeometry.CreateFromWkb(GeometryToWKB.Write(geom))) { _ogrLayer.SetSpatialFilter(ogrGeometry); var fds = new FeatureDataSet(); ExecuteIntersectionQuery(fds); foreach (var fd in fds) { fcs.Add(fd); } } }
/// <summary> /// Returns all features with the view box /// </summary> /// <param name="bbox">view box</param> /// <param name="ds">FeatureDataSet to fill data into</param> public override void ExecuteIntersectionQuery(Envelope bbox, IFeatureCollectionSet ds, CancellationToken?cancellationToken = null) { using (var conn = new OracleConnection(ConnectionString)) { //Get bounding box string var strBbox = GetBoxFilterStr(bbox); var strSQL = "SELECT g.*, g." + GeometryColumn + ".Get_WKB() AS sharpmap_tempgeometry "; strSQL += "FROM " + Table + " g WHERE "; if (!String.IsNullOrEmpty(_definitionQuery)) { strSQL += DefinitionQuery + " AND "; } strSQL += strBbox; using (var adapter = new OracleDataAdapter(strSQL, conn)) { conn.Open(); var ds2 = new DataSet(); adapter.Fill(ds2); conn.Close(); if (ds2.Tables.Count > 0) { var fdt = new FeatureDataTable(ds2.Tables[0]); foreach (DataColumn col in ds2.Tables[0].Columns) { if (col.ColumnName != GeometryColumn && col.ColumnName != "sharpmap_tempgeometry") { fdt.Columns.Add(col.ColumnName, col.DataType, col.Expression); } } foreach (DataRow dr in ds2.Tables[0].Rows) { FeatureDataRow fdr = fdt.NewRow(); foreach (DataColumn col in ds2.Tables[0].Columns) { if (col.ColumnName != GeometryColumn && col.ColumnName != "sharpmap_tempgeometry") { fdr[col.ColumnName] = dr[col]; } } fdr.Geometry = GeometryFromWKB.Parse((byte[])dr["sharpmap_tempgeometry"], Factory); fdt.AddRow(fdr); } ds.Add(fdt); } } } }
/// <summary> /// Method to perform the intersection query against the data source /// </summary> /// <param name="geom">The geometry to use as filter</param> /// <param name="ds">The feature data set to store the results in</param> protected override void OnExecuteIntersectionQuery(IGeometry geom, IFeatureCollectionSet ds, CancellationToken? cancellationToken = null) { ExecuteIntersectionQuery(geom.EnvelopeInternal, ds); //index of last added feature data table var index = ds.Count - 1; if (index < 0) return; var fds = ds[index].Clone(); fds.AddRange(FilterFeatures(ds[index])); ds.Remove(ds[index]); ds.Add(fds); }
/// <summary> /// /// </summary> /// <param name="envelope"></param> /// <param name="ds"></param> public override void ExecuteIntersectionQuery(BoundingBox envelope, IFeatureCollectionSet ds, CancellationToken?ct = null) { // Identifies all the features within the given BoundingBox var dataTable = CreateFeatureDataTable(); dataTable.BeginLoadData(); foreach (Feature feature in _features) { if (envelope.Intersects(feature.Geometry.EnvelopeInternal)) { CreateNewRow(dataTable, feature); } } dataTable.EndLoadData(); ds.Add(dataTable); }
/// <summary> /// Method to perform the intersection query against the data source /// </summary> /// <param name="geom">The geometry to use as filter</param> /// <param name="ds">The feature data set to store the results in</param> protected override void OnExecuteIntersectionQuery(IGeometry geom, IFeatureCollectionSet ds, CancellationToken?cancellationToken = null) { ExecuteIntersectionQuery(geom.EnvelopeInternal, ds); //index of last added feature data table var index = ds.Count - 1; if (index < 0) { return; } var fds = ds[index].Clone(); fds.AddRange(FilterFeatures(ds[index])); ds.Remove(ds[index]); ds.Add(fds); }
/// <summary> /// Returns all features with the view box /// </summary> /// <param name="bbox">view box</param> /// <param name="ds">FeatureDataSet to fill data into</param> public override void ExecuteIntersectionQuery(Envelope bbox, IFeatureCollectionSet ds, CancellationToken? cancellationToken = null) { //List<Geometries.Geometry> features = new List<GeoAPI.Geometries.IGeometry>(); using (var conn = new OleDbConnection(ConnectionString)) { conn.Open(); var strSQL = "SELECT * FROM " + Table + " WHERE "; //If a definition query has been specified, add this as a filter on the query strSQL += GetDefinitionQueryConstraint(true); //Limit to the points within the boundingbox strSQL += GetSpatialConstraint(bbox); using (var cmd = new OleDbCommand(strSQL, conn)) { using (var reader = cmd.ExecuteReader()) { if (reader == null) throw new InvalidOperationException(); //Set up result table var fdt = new FeatureDataTable(); fdt.TableName = Table; for (var c = 0; c < reader.FieldCount; c++) { var fieldType = reader.GetFieldType(c); if (fieldType == null) throw new Exception("Failed to retrieve field type for column: " + c); fdt.Columns.Add(reader.GetName(c), fieldType); } var pkColumn = fdt.Columns[ObjectIdColumn]; fdt.PrimaryKey = new [] { pkColumn }; var dataTransfer = new object[reader.FieldCount]; //Get factory and precision model var factory = Factory; var pm = factory.PrecisionModel; fdt.BeginLoadData(); while (reader.Read()) { var count = reader.GetValues(dataTransfer); System.Diagnostics.Debug.Assert(count == dataTransfer.Length); var fdr = (FeatureDataRow) fdt.LoadDataRow(dataTransfer, true); var c = new Coordinate(Convert.ToDouble(fdr[XColumn]), Convert.ToDouble(fdr[YColumn])); pm.MakePrecise(c); fdr.Geometry = Factory.CreatePoint(c); } fdt.EndLoadData(); ds.Add(fdt); } } } }
/// <summary> /// Returns all features with the view box /// </summary> /// <param name="bbox">view box</param> /// <param name="fcs">FeatureCollectionSet to fill data into</param> public override void ExecuteIntersectionQuery(Envelope bbox, IFeatureCollectionSet fcs, CancellationToken? cancellationToken=null) { using (var conn = new OracleConnection(ConnectionString)) { //Get bounding box string var strBbox = GetBoxFilterStr(bbox); var strSql = "SELECT * "; strSql += "FROM " + Table + " g WHERE "; if (!String.IsNullOrEmpty(_definitionQuery)) strSql += DefinitionQuery + " AND "; strSql += strBbox; using (var adapter = new OracleDataAdapter(strSql, conn)) { conn.Open(); var ds2 = new DataSet(); adapter.Fill(ds2); conn.Close(); if (ds2.Tables.Count > 0) { var fdt = new FeatureDataTable(ds2.Tables[0]); foreach (DataColumn col in ds2.Tables[0].Columns) if (string.Compare(col.ColumnName, GeometryColumn, CultureInfo.InvariantCulture, CompareOptions.OrdinalIgnoreCase) != 0) fdt.Columns.Add(col.ColumnName, col.DataType, col.Expression); foreach (DataRow dr in ds2.Tables[0].Rows) { FeatureDataRow fdr = fdt.NewRow(); foreach (DataColumn col in ds2.Tables[0].Columns) if (string.Compare(col.ColumnName, GeometryColumn, CultureInfo.InvariantCulture, CompareOptions.OrdinalIgnoreCase) != 0) fdr[col.ColumnName] = dr[col]; var sdoGeom = dr[GeometryColumn] as SdoGeometry; if (sdoGeom != null) fdr.Geometry = sdoGeom.AsGeometry(); fdt.AddRow(fdr); } fcs.Add(fdt); } } } }
/// <summary> /// Returns the data associated with all the geometries that are intersected by 'geom' /// </summary> /// <param name="box">Geometry to intersect with</param> /// <param name="ds">FeatureDataSet to fill data into</param> public void ExecuteIntersectionQuery(Envelope box, IFeatureCollectionSet ds, CancellationToken? cancellationToken = null) { if (_labelInfo == null) return; ds.Add(_labelInfo); // Destroy internal reference _labelInfo = null; }
/// <summary> /// Returns all features with the view box /// </summary> /// <param name="bbox">view box</param> /// <param name="ds">FeatureDataSet to fill data into</param> public override void ExecuteIntersectionQuery(Envelope bbox, IFeatureCollectionSet ds, CancellationToken?cancellationToken = null) { //List<Geometry> features = new List<Geometry>(); using (var conn = new SqlConnection(ConnectionString)) { //Get bounding box string string strBbox = GetBoxFilterStr(bbox); //string strSQL = "SELECT g.*, g." + GeometryColumn + ".STAsBinary() AS sharpmap_tempgeometry "; string strSQL = String.Format( "SELECT g.*, g.{0}{1}.STAsBinary() AS sharpmap_tempgeometry FROM {2} g {3} WHERE ", GeometryColumn, MakeValidString, Table, BuildTableHints()); if (!String.IsNullOrEmpty(_definitionQuery)) { strSQL += DefinitionQuery + " AND "; } strSQL += strBbox; string extraOptions = GetExtraOptions(); if (!string.IsNullOrEmpty(extraOptions)) { strSQL += " " + extraOptions; } using (SqlDataAdapter adapter = new SqlDataAdapter(strSQL, conn)) { conn.Open(); System.Data.DataSet ds2 = new System.Data.DataSet(); adapter.Fill(ds2); conn.Close(); if (ds2.Tables.Count > 0) { FeatureDataTable fdt = new FeatureDataTable(ds2.Tables[0]); foreach (System.Data.DataColumn col in ds2.Tables[0].Columns) { if (col.ColumnName != GeometryColumn && col.ColumnName != "sharpmap_tempgeometry") { fdt.Columns.Add(col.ColumnName, col.DataType, col.Expression); } } foreach (System.Data.DataRow dr in ds2.Tables[0].Rows) { FeatureDataRow fdr = fdt.NewRow(); foreach (System.Data.DataColumn col in ds2.Tables[0].Columns) { if (col.ColumnName != GeometryColumn && col.ColumnName != "sharpmap_tempgeometry") { fdr[col.ColumnName] = dr[col]; } } fdr.Geometry = Converters.WellKnownBinary.GeometryFromWKB.Parse((byte[])dr["sharpmap_tempgeometry"], Factory); fdt.AddRow(fdr); } ds.Add(fdt); } } } }
/// <summary> /// Returns the data associated with all the geometries that are intersected by 'geom' /// </summary> /// <param name="geom">Geometry to intersect with</param> /// <param name="ds">FeatureDataSet to fill data into</param> protected override void OnExecuteIntersectionQuery(Geometry geom, IFeatureCollectionSet fcs, CancellationToken? ct = null) { using (var ogrGeometry = OgrGeometry.CreateFromWkb(GeometryToWKB.Write(geom))) { _ogrLayer.SetSpatialFilter(ogrGeometry); var fds = new FeatureDataSet(); ExecuteIntersectionQuery(fds); foreach (var fd in fds) fcs.Add(fd); } }
private void ExecuteIntersectionQuery(Coordinate pt, IFeatureCollectionSet ds) { if (CoordinateTransformation != null) { #if !DotSpatialProjections if (ReverseCoordinateTransformation != null) { pt = GeometryTransform.TransformCoordinate(pt, ReverseCoordinateTransformation.MathTransform); } else { CoordinateTransformation.MathTransform.Invert(); pt = GeometryTransform.TransformCoordinate(pt, CoordinateTransformation.MathTransform); CoordinateTransformation.MathTransform.Invert(); } #else pt = GeometryTransform.TransformCoordinate(pt, CoordinateTransformation.Target, CoordinateTransformation.Source); #endif } //Setup resulting Table var dt = new FeatureDataTable(); dt.Columns.Add("Ordinate X", typeof(Double)); dt.Columns.Add("Ordinate Y", typeof(Double)); for (int i = 1; i <= Bands; i++) dt.Columns.Add(string.Format("Value Band {0}", i), typeof(Double)); //Get location on raster var buffer = new double[1]; var bandMap = new int[Bands]; for (int i = 1; i <= Bands; i++) bandMap[i - 1] = i; var geoTransform = new GeoTransform(_gdalDataset); var imgPt = geoTransform.GroundToImage(pt); Int32 x = Convert.ToInt32(imgPt.X); Int32 y = Convert.ToInt32(imgPt.Y); //Test if raster ordinates are within bounds if (x < 0) return; if (y < 0) return; if (x >= _imageSize.Width) return; if (y >= _imageSize.Height) return; //Create new row, add ordinates and location geometry FeatureDataRow dr = dt.NewRow(); dr.Geometry = Factory.CreatePoint(pt); dr[0] = pt.X; dr[1] = pt.Y; //Add data from raster for (int i = 1; i <= Bands; i++) { Band band = _gdalDataset.GetRasterBand(i); //DataType dtype = band.DataType; CPLErr res = band.ReadRaster(x, y, 1, 1, buffer, 1, 1, 0, 0); if (res == CPLErr.CE_None) { dr[1 + i] = buffer[0]; } else { dr[1 + i] = Double.NaN; } } //Add new row to table dt.Rows.Add(dr); //Add table to dataset ds.Add(dt); }
/// <summary> /// Returns all features with the view box /// </summary> /// <param name="bbox">view box</param> /// <param name="ds">FeatureDataSet to fill data into</param> public override void ExecuteIntersectionQuery(Envelope bbox, IFeatureCollectionSet ds, CancellationToken? cancellationToken = null) { using (var conn = new OracleConnection(ConnectionString)) { //Get bounding box string var strBbox = GetBoxFilterStr(bbox); var strSQL = "SELECT g.*, g." + GeometryColumn + ".Get_WKB() AS sharpmap_tempgeometry "; strSQL += "FROM " + Table + " g WHERE "; if (!String.IsNullOrEmpty(_definitionQuery)) strSQL += DefinitionQuery + " AND "; strSQL += strBbox; using (var adapter = new OracleDataAdapter(strSQL, conn)) { conn.Open(); var ds2 = new DataSet(); adapter.Fill(ds2); conn.Close(); if (ds2.Tables.Count > 0) { var fdt = new FeatureDataTable(ds2.Tables[0]); foreach (DataColumn col in ds2.Tables[0].Columns) if (col.ColumnName != GeometryColumn && col.ColumnName != "sharpmap_tempgeometry") fdt.Columns.Add(col.ColumnName, col.DataType, col.Expression); foreach (DataRow dr in ds2.Tables[0].Rows) { FeatureDataRow fdr = fdt.NewRow(); foreach (DataColumn col in ds2.Tables[0].Columns) if (col.ColumnName != GeometryColumn && col.ColumnName != "sharpmap_tempgeometry") fdr[col.ColumnName] = dr[col]; fdr.Geometry = GeometryFromWKB.Parse((byte[]) dr["sharpmap_tempgeometry"], Factory); fdt.AddRow(fdr); } ds.Add(fdt); } } } }
/// <summary> /// Returns the features that intersects with 'geom' /// </summary> /// <param name="geom"></param> /// <param name="ds">FeatureDataSet to fill data into</param> protected override void OnExecuteIntersectionQuery(IGeometry geom, IFeatureCollectionSet fcs, CancellationToken?ct = null) { GetNonSpatialColumns(); using (var conn = new NpgsqlConnection(ConnectionString)) { conn.Open(); var strSql = "SELECT " + _columns + ", \"" + GeometryColumn + "\" AS \"_smtmp_\" "; strSql += "FROM " + Table + " WHERE "; // Attribute constraint if (!String.IsNullOrEmpty(_definitionQuery)) { strSql += DefinitionQuery + " AND "; } // Spatial constraint if (_supportSTIntersects) { strSql += "ST_Intersects(@PGeo ,\"" + GeometryColumn + "\")"; } else { strSql += "\"" + GeometryColumn + "\" && @PGeo AND " + _prefixFunction + "distance(\"" + GeometryColumn + "\", @PGeo)<0"; } /* * string strGeom = _prefixFunction + "GeomFromText('" + geom.AsText() + "')"; * if (SRID > 0) * strGeom = _prefixFunction + "setSRID(" + strGeom + "," + SRID + ")"; * * string strSQL = "SELECT * , " + _prefixFunction + "AsBinary(\"" + GeometryColumn + "\") As sharpmap_tempgeometry FROM " + * QualifiedTable + " WHERE "; * * if (!String.IsNullOrEmpty(_definitionQuery)) * strSQL += DefinitionQuery + " AND "; * * strSQL += "\"" + GeometryColumn + "\" && " + strGeom + " AND " + _prefixFunction + "distance(\"" + GeometryColumn + "\", " + * strGeom + ")<0"; * */ if (Logger.IsDebugEnabled) { Logger.Debug(string.Format("{0}\n{1}", "OnExecuteIntersectionQuery: executing sql:", strSql)); } using (var cmd = new NpgsqlCommand(strSql, conn)) { geom.SRID = SRID; cmd.Parameters.AddWithValue("PGeo", new PostGisWriter().Write(geom)); using (var reader = cmd.ExecuteReader()) { var geomIndex = reader.FieldCount - 1; var fdt = CreateTableFromReader(reader, geomIndex); var dataTransfer = new object[geomIndex]; var geoReader = new PostGisReader(Factory.CoordinateSequenceFactory, Factory.PrecisionModel); fdt.BeginLoadData(); while (reader.Read()) { IGeometry g = null; if (!reader.IsDBNull(geomIndex)) { g = geoReader.Read((byte[])reader.GetValue(geomIndex)); } //No geometry, no feature! if (g == null) { continue; } //Get all the attribute data var count = reader.GetValues(dataTransfer); System.Diagnostics.Debug.Assert(count == dataTransfer.Length); var fdr = (FeatureDataRow)fdt.LoadDataRow(dataTransfer, true); fdr.Geometry = g; } fdt.EndLoadData(); fcs.Add(fdt); } } } }
/// <summary> /// Returns the data associated with all the geometries that are intersected by 'geom' /// </summary> /// <param name="spatialWhere">Geometry to intersect with</param> /// <param name="fds">FeatureDataSet to fill data into</param> protected virtual void ExecuteIntersectionQueryInternal(object spatialWhere, IFeatureCollectionSet fds, CancellationToken?cancellationToken = null) { var fdt = CreateNewTable(true); fdt.BeginLoadData(); using (var cn = CreateOpenDbConnection()) { using (var cmd = cn.CreateCommand()) { string from = null; var spatialWhereString = string.Empty; var env = spatialWhere as Envelope; if (env != null) { from = GetFrom(env, cmd); spatialWhereString = GetSpatialWhere(env, cmd); } else { var geom = spatialWhere as IGeometry; if (geom != null) { from = GetFrom(geom, cmd); spatialWhereString = GetSpatialWhere(geom, cmd); } } cmd.CommandText = FeatureColumns.GetSelectClause(from) #pragma warning disable 612,618 + (string.IsNullOrEmpty(DefinitionQuery) #pragma warning restore 612,618 ? FeatureColumns.GetWhereClause(spatialWhereString) : (" WHERE " + _definitionQuery + (string.IsNullOrEmpty(spatialWhereString) ? "" : " AND " + spatialWhereString))) + FeatureColumns.GetGroupByClause() + FeatureColumns.GetOrderByClause(); var numColumns = fdt.Columns.Count; var geomIndex = numColumns; Logger.Debug(t => t("Executing query:\n{0}", PrintCommand(cmd))); using (var dr = cmd.ExecuteReader()) { while (dr.Read()) { var data = new object[numColumns + 1]; if (dr.GetValues(data) > 0) { var loadData = new object[geomIndex]; Array.Copy(data, 0, loadData, 0, geomIndex); var row = (FeatureDataRow)fdt.LoadDataRow(loadData, true); row.Geometry = GeometryFromWKB.Parse((byte[])data[geomIndex], Factory); } } } } } fdt.EndLoadData(); fds.Add(fdt); }
protected override void OnExecuteIntersectionQuery(IGeometry geom, IFeatureCollectionSet ds, CancellationToken? cancellationToken = null) { var fds = new FeatureDataSet(); ExecuteIntersectionQuery(geom.EnvelopeInternal, fds); //index of last added feature data table var index = fds.Tables.Count - 1; if (index <= 0) return; var res = (FeatureDataTable)CloneTableStructure(fds.Tables[index].Clone()); res.BeginLoadData(); var fdt = fds.Tables[index]; foreach (FeatureDataRow row in fdt.Rows) { if (PreparedGeometry.Intersects(row.Geometry)) { var fdr = (FeatureDataRow)res.LoadDataRow(row.ItemArray, true); fdr.Geometry = row.Geometry; } } res.EndLoadData(); ds.Add(res); fds.Dispose(); }
public override void ExecuteIntersectionQuery(Envelope box, IFeatureCollectionSet fcs, CancellationToken? cancellationToken = null) { using (var conn = new SQLiteConnection(ConnectionID)) { string strSQL = "SELECT *, " + GeometryColumn + " AS sharpmap_tempgeometry "; strSQL += "FROM " + Table + " WHERE "; strSQL += GetBoxClause(box); if (!String.IsNullOrEmpty(_definitionQuery)) strSQL += " AND " + DefinitionQuery; using (SQLiteDataAdapter adapter = new SQLiteDataAdapter(strSQL, conn)) { conn.Open(); DataSet ds2 = new DataSet(); adapter.Fill(ds2); conn.Close(); if (ds2.Tables.Count > 0) { FeatureDataTable fdt = new FeatureDataTable(ds2.Tables[0]); foreach (DataColumn col in ds2.Tables[0].Columns) if (col.ColumnName != GeometryColumn && col.ColumnName != "sharpmap_tempgeometry" && !col.ColumnName.StartsWith("Envelope_")) fdt.Columns.Add(col.ColumnName, col.DataType, col.Expression); foreach (DataRow dr in ds2.Tables[0].Rows) { FeatureDataRow fdr = fdt.NewRow(); foreach (DataColumn col in ds2.Tables[0].Columns) if (col.ColumnName != GeometryColumn && col.ColumnName != "sharpmap_tempgeometry" && !col.ColumnName.StartsWith("Envelope_")) fdr[col.ColumnName] = dr[col]; if (dr["sharpmap_tempgeometry"] != DBNull.Value) fdr.Geometry = GeometryFromWKT.Parse((string) dr["sharpmap_tempgeometry"]); fdt.AddRow(fdr); } fcs.Add(fdt); } } } }
/// <summary> /// Retrieves all features within the given BoundingBox. /// </summary> /// <param name="bounds">Bounds of the region to search.</param> /// <param name="fcs">FeatureDataSet to fill data into</param> public override void ExecuteIntersectionQuery(Envelope bounds, IFeatureCollectionSet fcs, CancellationToken? cancellationToken = null) { if (Table.Rows.Count == 0) { return; } string statement = XColumn + " > " + bounds.MinX.ToString(Map.NumberFormatEnUs) + " AND " + XColumn + " < " + bounds.MaxX.ToString(Map.NumberFormatEnUs) + " AND " + YColumn + " > " + bounds.MinY.ToString(Map.NumberFormatEnUs) + " AND " + YColumn + " < " + bounds.MaxY.ToString(Map.NumberFormatEnUs); var rows = Table.Select(statement); var fdt = new FeatureDataTable(Table); foreach (DataColumn col in Table.Columns) { fdt.Columns.Add(col.ColumnName, col.DataType, col.Expression); } foreach (var dr in rows) { fdt.ImportRow(dr); var fdr = (FeatureDataRow)fdt.Rows[fdt.Rows.Count - 1]; fdr.Geometry = Factory.CreatePoint(new Coordinate((double) dr[XColumn], (double) dr[YColumn])); } fcs.Add(fdt); }
public override void ExecuteIntersectionQuery(Envelope box, IFeatureCollectionSet fcs, CancellationToken?cancellationToken = null) { GetNonSpatialColumns(); using (var conn = GetConnection(ConnectionString)) { var strSql = "SELECT " + _columns + ", \"" + GeometryColumn + "\" AS \"_smtmp_\" "; strSql += "FROM " + Table + " WHERE "; // Attribute constraint if (!String.IsNullOrEmpty(_definitionQuery)) { strSql += DefinitionQuery + " AND "; } // Spatial constraint strSql += GetBoxClause(box); using (var cmd = new SQLiteCommand(strSql, conn)) { using (var reader = cmd.ExecuteReader()) { var geomIndex = reader.FieldCount - 1; var fdt = CreateTableFromReader(reader, geomIndex); var dataTransfer = new object[geomIndex]; var geoReader = new GaiaGeoReader(Factory.CoordinateSequenceFactory, Factory.PrecisionModel, _ordinates); fdt.BeginLoadData(); while (reader.Read()) { IGeometry g = null; if (!reader.IsDBNull(geomIndex)) { g = geoReader.Read((byte[])reader.GetValue(geomIndex)); } //No geometry, no feature! if (g == null) { continue; } //If not using RTree index we need to filter in code if (_spatiaLiteIndex != SpatiaLiteIndex.RTree && !box.Intersects(g.EnvelopeInternal)) { continue; } //Get all the attribute data var count = reader.GetValues(dataTransfer); System.Diagnostics.Debug.Assert(count == dataTransfer.Length); var fdr = (FeatureDataRow)fdt.LoadDataRow(dataTransfer, true); fdr.Geometry = g; } reader.Close(); fdt.EndLoadData(); fcs.Add(fdt); } } } }
/// <summary> /// /// </summary> /// <param name="envelope"></param> /// <param name="ds"></param> public override void ExecuteIntersectionQuery(BoundingBox envelope, IFeatureCollectionSet ds, CancellationToken? ct = null) { // Identifies all the features within the given BoundingBox var dataTable = CreateFeatureDataTable(); dataTable.BeginLoadData(); foreach (Feature feature in _features) { if (envelope.Intersects(feature.Geometry.EnvelopeInternal)) CreateNewRow(dataTable, feature); } dataTable.EndLoadData(); ds.Add(dataTable); }
/// <summary> /// Returns the features that intersects with 'geom' /// </summary> /// <param name="geom"></param> /// <param name="ds">FeatureDataSet to fill data into</param> protected override void OnExecuteIntersectionQuery(IGeometry geom, IFeatureCollectionSet ds, CancellationToken?cancellationToken = null) { //List<Geometry> features = new List<Geometry>(); using (var conn = new SqlConnection(ConnectionString)) { //TODO: Convert to SQL Server string strGeom = _spatialObject + "::STGeomFromText('" + geom.AsText() + "', #SRID#)"; strGeom = strGeom.Replace("#SRID#", SRID > 0 ? SRID.ToString() : "0"); strGeom = GeometryColumn + ".STIntersects(" + strGeom + ") = 1"; string strSQL = "SELECT g.* , g." + GeometryColumn + ".STAsBinary() As sharpmap_tempgeometry FROM " + Table + " g " + BuildTableHints() + " WHERE "; if (!String.IsNullOrEmpty(_definitionQuery)) { strSQL += DefinitionQuery + " AND "; } strSQL += strGeom; string extraOptions = GetExtraOptions(); if (!string.IsNullOrEmpty(extraOptions)) { strSQL += " " + extraOptions; } var fds = new FeatureDataSet(); using (SqlDataAdapter adapter = new SqlDataAdapter(strSQL, conn)) { conn.Open(); adapter.Fill(fds); conn.Close(); if (fds.Tables.Count > 0) { var fdt = new FeatureDataTable(fds.Tables[0]); foreach (System.Data.DataColumn col in fds.Tables[0].Columns) { if (col.ColumnName != GeometryColumn && col.ColumnName != "sharpmap_tempgeometry") { fdt.Columns.Add(col.ColumnName, col.DataType, col.Expression); } } fdt.BeginLoadData(); foreach (System.Data.DataRow dr in fds.Tables[0].Rows) { FeatureDataRow fdr = fdt.NewRow(); foreach (System.Data.DataColumn col in fds.Tables[0].Columns) { if (col.ColumnName != GeometryColumn && col.ColumnName != "sharpmap_tempgeometry") { fdr[col.ColumnName] = dr[col]; } } fdr.Geometry = Converters.WellKnownBinary.GeometryFromWKB.Parse((byte[])dr["sharpmap_tempgeometry"], Factory); fdt.AddRow(fdr); } fdt.EndLoadData(); ds.Add(fdt); } } } }
/// <summary> /// Returns all features with the view box /// </summary> /// <param name="bbox">view box</param> /// <param name="ds">FeatureDataSet to fill data into</param> public override void ExecuteIntersectionQuery(Envelope bbox, IFeatureCollectionSet ds, CancellationToken?cancellationToken = null) { //List<Geometries.Geometry> features = new List<GeoAPI.Geometries.IGeometry>(); using (var conn = new OleDbConnection(ConnectionString)) { conn.Open(); var strSQL = "SELECT * FROM " + Table + " WHERE "; //If a definition query has been specified, add this as a filter on the query strSQL += GetDefinitionQueryConstraint(true); //Limit to the points within the boundingbox strSQL += GetSpatialConstraint(bbox); using (var cmd = new OleDbCommand(strSQL, conn)) { using (var reader = cmd.ExecuteReader()) { if (reader == null) { throw new InvalidOperationException(); } //Set up result table var fdt = new FeatureDataTable(); fdt.TableName = Table; for (var c = 0; c < reader.FieldCount; c++) { var fieldType = reader.GetFieldType(c); if (fieldType == null) { throw new Exception("Failed to retrieve field type for column: " + c); } fdt.Columns.Add(reader.GetName(c), fieldType); } var pkColumn = fdt.Columns[ObjectIdColumn]; fdt.PrimaryKey = new [] { pkColumn }; var dataTransfer = new object[reader.FieldCount]; //Get factory and precision model var factory = Factory; var pm = factory.PrecisionModel; fdt.BeginLoadData(); while (reader.Read()) { var count = reader.GetValues(dataTransfer); System.Diagnostics.Debug.Assert(count == dataTransfer.Length); var fdr = (FeatureDataRow)fdt.LoadDataRow(dataTransfer, true); var c = new Coordinate(Convert.ToDouble(fdr[XColumn]), Convert.ToDouble(fdr[YColumn])); pm.MakePrecise(c); fdr.Geometry = Factory.CreatePoint(c); } fdt.EndLoadData(); ds.Add(fdt); } } } }
/// <summary> /// Returns the data associated with all the geometries that are intersected by 'geom' /// </summary> /// <param name="bbox">Geometry to intersect with</param> /// <param name="ds">FeatureDataSet to fill data into</param> public override void ExecuteIntersectionQuery(BoundingBox bbox, IFeatureCollectionSet fcs, CancellationToken? ct = null) { _ogrLayer.SetSpatialFilterRect(bbox.MinX, bbox.MinY, bbox.MaxX, bbox.MaxY); var fds = new FeatureDataSet(); ExecuteIntersectionQuery(fds); foreach (var fd in fds) fcs.Add(fd); }
/// <summary> /// Returns all features with the view box /// </summary> /// <param name="bbox">view box</param> /// <param name="fcs">FeatureDataSet to fill data into</param> public override void ExecuteIntersectionQuery(Envelope bbox, IFeatureCollectionSet fcs, CancellationToken?ct = null) { GetNonSpatialColumns(); //List<Geometry> features = new List<Geometry>(); using (var conn = new NpgsqlConnection(ConnectionString)) { conn.Open(); var strSql = "SELECT " + _columns + ", \"" + GeometryColumn + "\"" + _geometryCast + "::bytea AS \"_smtmp_\" "; strSql += "FROM " + QualifiedTable + " WHERE "; // Attribute constraint if (!String.IsNullOrEmpty(_definitionQuery)) { strSql += DefinitionQuery + " AND "; } // Spatial constraint if (_supportSTIntersects) { strSql += "ST_Intersects(\"" + GeometryColumn + "\"," + GetBoxClause(bbox) + ")"; } else { strSql += "\"" + GeometryColumn + "\" && " + GetBoxClause(bbox); } if (Logger.IsDebugEnabled) { Logger.Debug(string.Format("{0}\n{1}\n", "ExecuteIntersectionQuery: executing sql:", strSql)); } using (var cmd = new NpgsqlCommand(strSql, conn)) { using (var reader = cmd.ExecuteReader()) { var geomIndex = reader.FieldCount - 1; var fdt = CreateTableFromReader(reader, geomIndex); var dataTransfer = new object[geomIndex]; var geoReader = new PostGisReader(Factory.CoordinateSequenceFactory, Factory.PrecisionModel); fdt.BeginLoadData(); while (reader.Read()) { IGeometry g = null; if (!reader.IsDBNull(geomIndex)) { g = geoReader.Read((byte[])reader.GetValue(geomIndex)); } //No geometry, no feature! if (g == null) { continue; } //Get all the attribute data var count = reader.GetValues(dataTransfer); System.Diagnostics.Debug.Assert(count == dataTransfer.Length); var fdr = (FeatureDataRow)fdt.LoadDataRow(dataTransfer, true); fdr.Geometry = g; } fdt.EndLoadData(); fcs.Add(fdt); } } } }
/// <summary> /// Returns the data associated with all the geometries that are intersected by 'geom' /// </summary> /// <param name="spatialWhere">Geometry to intersect with</param> /// <param name="fds">FeatureDataSet to fill data into</param> protected virtual void ExecuteIntersectionQueryInternal(object spatialWhere, IFeatureCollectionSet fds, CancellationToken? cancellationToken = null) { var fdt = CreateNewTable(true); fdt.BeginLoadData(); using (var cn = CreateOpenDbConnection()) { using (var cmd = cn.CreateCommand()) { string from = null; var spatialWhereString = string.Empty; var env = spatialWhere as Envelope; if (env != null) { from = GetFrom(env, cmd); spatialWhereString = GetSpatialWhere(env, cmd); } else { var geom = spatialWhere as IGeometry; if (geom != null) { from = GetFrom(geom, cmd); spatialWhereString = GetSpatialWhere(geom, cmd); } } cmd.CommandText = FeatureColumns.GetSelectClause(from) #pragma warning disable 612,618 + (string.IsNullOrEmpty(DefinitionQuery) #pragma warning restore 612,618 ? FeatureColumns.GetWhereClause(spatialWhereString) : (" WHERE " + _definitionQuery + (string.IsNullOrEmpty(spatialWhereString) ? "" : " AND " + spatialWhereString))) + FeatureColumns.GetGroupByClause() + FeatureColumns.GetOrderByClause(); var numColumns = fdt.Columns.Count; var geomIndex = numColumns; Logger.Debug(t => t("Executing query:\n{0}", PrintCommand(cmd))); using (var dr = cmd.ExecuteReader()) { while (dr.Read()) { var data = new object[numColumns+1]; if (dr.GetValues(data) > 0) { var loadData = new object[geomIndex]; Array.Copy(data, 0, loadData, 0, geomIndex); var row = (FeatureDataRow)fdt.LoadDataRow(loadData, true); row.Geometry = GeometryFromWKB.Parse((byte[])data[geomIndex], Factory); } } } } } fdt.EndLoadData(); fds.Add(fdt); }
/// <summary> /// Returns the features that intersects with 'geom' /// </summary> /// <param name="geom"></param> /// <param name="fcs">FeatureCollectionSet to fill data into</param> protected override void OnExecuteIntersectionQuery(IGeometry geom, IFeatureCollectionSet fcs, CancellationToken? cancellationToken=null) { using (var conn = new OracleConnection(ConnectionString)) { var strGeom = "MDSYS.SDO_GEOMETRY('" + geom.AsText() + "', #SRID#)"; strGeom = strGeom.Replace("#SRID#", SRID > 0 ? SRID.ToString(Map.NumberFormatEnUs) : "NULL"); strGeom = "SDO_RELATE(g." + GeometryColumn + ", " + strGeom + ", 'mask=ANYINTERACT querytype=WINDOW') = 'TRUE'"; var strSQL = "SELECT g.* , g." + GeometryColumn + ").Get_WKB() As sharpmap_tempgeometry FROM " + Table + " g WHERE "; if (!String.IsNullOrEmpty(_definitionQuery)) strSQL += DefinitionQuery + " AND "; strSQL += strGeom; var ds = new FeatureDataSet(); using (var adapter = new OracleDataAdapter(strSQL, conn)) { conn.Open(); adapter.Fill(ds); conn.Close(); if (ds.Tables.Count <= 0) { return; } var fdt = new FeatureDataTable(ds.Tables[0]); foreach (DataColumn col in ds.Tables[0].Columns) { if (col.ColumnName != GeometryColumn && col.ColumnName != "sharpmap_tempgeometry") { fdt.Columns.Add(col.ColumnName, col.DataType, col.Expression); } } foreach (DataRow dr in ds.Tables[0].Rows) { var fdr = fdt.NewRow(); foreach (DataColumn col in ds.Tables[0].Columns) if (col.ColumnName != GeometryColumn && col.ColumnName != "sharpmap_tempgeometry") fdr[col.ColumnName] = dr[col]; fdr.Geometry = GeometryFromWKB.Parse((byte[]) dr["sharpmap_tempgeometry"], Factory); fdt.AddRow(fdr); } fcs.Add(fdt); } } }
/// <summary> /// Returns all features with the view box /// </summary> /// <param name="bbox">view box</param> /// <param name="fcs">FeatureDataSet to fill data into</param> public override void ExecuteIntersectionQuery(Envelope bbox, IFeatureCollectionSet fcs, CancellationToken? ct = null) { GetNonSpatialColumns(); //List<Geometry> features = new List<Geometry>(); using (var conn = new NpgsqlConnection(ConnectionString)) { conn.Open(); var strSql = "SELECT " + _columns + ", \"" + GeometryColumn + "\"" + _geometryCast + "::bytea AS \"_smtmp_\" "; strSql += "FROM " + QualifiedTable + " WHERE "; // Attribute constraint if (!String.IsNullOrEmpty(_definitionQuery)) strSql += DefinitionQuery + " AND "; // Spatial constraint if (_supportSTIntersects) { strSql += "ST_Intersects(\"" + GeometryColumn + "\"," + GetBoxClause(bbox) + ")"; } else { strSql += "\"" + GeometryColumn + "\" && " + GetBoxClause(bbox); } if (Logger.IsDebugEnabled) Logger.Debug(string.Format("{0}\n{1}\n", "ExecuteIntersectionQuery: executing sql:", strSql)); using (var cmd = new NpgsqlCommand(strSql, conn)) { using (var reader = cmd.ExecuteReader()) { var geomIndex = reader.FieldCount - 1; var fdt = CreateTableFromReader(reader, geomIndex); var dataTransfer = new object[geomIndex]; var geoReader = new PostGisReader(Factory.CoordinateSequenceFactory, Factory.PrecisionModel); fdt.BeginLoadData(); while (reader.Read()) { IGeometry g = null; if (!reader.IsDBNull(geomIndex)) g = geoReader.Read((byte[])reader.GetValue(geomIndex)); //No geometry, no feature! if (g == null) continue; //Get all the attribute data var count = reader.GetValues(dataTransfer); System.Diagnostics.Debug.Assert(count == dataTransfer.Length); var fdr = (FeatureDataRow)fdt.LoadDataRow(dataTransfer, true); fdr.Geometry = g; } fdt.EndLoadData(); fcs.Add(fdt); } } } }
public override void ExecuteIntersectionQuery(Envelope box, IFeatureCollectionSet fcs, CancellationToken? cancellationToken = null) { GetNonSpatialColumns(); using (var conn = GetConnection(ConnectionString)) { var strSql = "SELECT " + _columns + ", \"" + GeometryColumn + "\" AS \"_smtmp_\" "; strSql += "FROM " + Table + " WHERE "; // Attribute constraint if (!String.IsNullOrEmpty(_definitionQuery)) strSql += DefinitionQuery + " AND "; // Spatial constraint strSql += GetBoxClause(box); using (var cmd = new SQLiteCommand(strSql, conn)) { using (var reader = cmd.ExecuteReader()) { var geomIndex = reader.FieldCount - 1; var fdt = CreateTableFromReader(reader, geomIndex); var dataTransfer = new object[geomIndex]; var geoReader = new GaiaGeoReader(Factory.CoordinateSequenceFactory, Factory.PrecisionModel, _ordinates); fdt.BeginLoadData(); while (reader.Read()) { IGeometry g = null; if (!reader.IsDBNull(geomIndex)) g = geoReader.Read((byte[])reader.GetValue(geomIndex)); //No geometry, no feature! if (g == null) continue; //If not using RTree index we need to filter in code if (_spatiaLiteIndex != SpatiaLiteIndex.RTree && !box.Intersects(g.EnvelopeInternal)) continue; //Get all the attribute data var count = reader.GetValues(dataTransfer); System.Diagnostics.Debug.Assert(count == dataTransfer.Length); var fdr = (FeatureDataRow)fdt.LoadDataRow(dataTransfer, true); fdr.Geometry = g; } reader.Close(); fdt.EndLoadData(); fcs.Add(fdt); } } } }
/// <summary> /// Returns the features that intersects with 'geom' /// </summary> /// <param name="geom"></param> /// <param name="ds">FeatureDataSet to fill data into</param> protected override void OnExecuteIntersectionQuery(IGeometry geom, IFeatureCollectionSet ds, CancellationToken? cancellationToken=null) { //List<Geometry> features = new List<Geometry>(); using (var conn = new SqlConnection(ConnectionString)) { //TODO: Convert to SQL Server string strGeom = _spatialObject + "::STGeomFromText('" + geom.AsText() + "', #SRID#)"; strGeom = strGeom.Replace("#SRID#", SRID > 0 ? SRID.ToString() : "0"); strGeom = GeometryColumn + ".STIntersects(" + strGeom + ") = 1"; string strSQL = "SELECT g.* , g." + GeometryColumn + ".STAsBinary() As sharpmap_tempgeometry FROM " + Table + " g " + BuildTableHints() + " WHERE "; if (!String.IsNullOrEmpty(_definitionQuery)) strSQL += DefinitionQuery + " AND "; strSQL += strGeom; string extraOptions = GetExtraOptions(); if (!string.IsNullOrEmpty(extraOptions)) strSQL += " " + extraOptions; var fds = new FeatureDataSet(); using (SqlDataAdapter adapter = new SqlDataAdapter(strSQL, conn)) { conn.Open(); adapter.Fill(fds); conn.Close(); if (fds.Tables.Count > 0) { var fdt = new FeatureDataTable(fds.Tables[0]); foreach (System.Data.DataColumn col in fds.Tables[0].Columns) if (col.ColumnName != GeometryColumn && col.ColumnName != "sharpmap_tempgeometry") fdt.Columns.Add(col.ColumnName, col.DataType, col.Expression); fdt.BeginLoadData(); foreach (System.Data.DataRow dr in fds.Tables[0].Rows) { FeatureDataRow fdr = fdt.NewRow(); foreach (System.Data.DataColumn col in fds.Tables[0].Columns) if (col.ColumnName != GeometryColumn && col.ColumnName != "sharpmap_tempgeometry") fdr[col.ColumnName] = dr[col]; fdr.Geometry = Converters.WellKnownBinary.GeometryFromWKB.Parse((byte[])dr["sharpmap_tempgeometry"], Factory); fdt.AddRow(fdr); } fdt.EndLoadData(); ds.Add(fdt); } } } }
public void ExecuteIntersectionQuery(IGeometry geom, IFeatureCollectionSet ds, CancellationToken? cancellationToken = null) { var fdt = (FeatureDataTable)_schemaTable.Copy(); /* // NOTE WHY IS THIS, No other provider behaves like that? if (ds.Tables.Count > 0) { fdt = ds.Tables[0]; } else { fdt = new FeatureDataTable(); }*/ var pGeom = NetTopologySuite.Geometries.Prepared.PreparedGeometryFactory.Prepare(geom); fdt.BeginLoadData(); foreach (var feature in _geometrys) { feature.Value.Where(pGeom.Intersects).ToList() .ForEach(v => { var newRow = (FeatureDataRow) fdt.LoadDataRow(GetAssetProperties(feature.Key), true); newRow.Geometry = v; } ); } fdt.EndLoadData(); ds.Add(fdt); }
/// <summary> /// Returns all features with the view box /// </summary> /// <param name="bbox">view box</param> /// <param name="ds">FeatureDataSet to fill data into</param> public override void ExecuteIntersectionQuery(Envelope bbox, IFeatureCollectionSet ds, CancellationToken? cancellationToken = null) { //List<Geometry> features = new List<Geometry>(); using (var conn = new SqlConnection(ConnectionString)) { //Get bounding box string string strBbox = GetBoxFilterStr(bbox); //string strSQL = "SELECT g.*, g." + GeometryColumn + ".STAsBinary() AS sharpmap_tempgeometry "; string strSQL = String.Format( "SELECT g.*, g.{0}{1}.STAsBinary() AS sharpmap_tempgeometry FROM {2} g {3} WHERE ", GeometryColumn, MakeValidString, Table, BuildTableHints()); if (!String.IsNullOrEmpty(_definitionQuery)) strSQL += DefinitionQuery + " AND "; strSQL += strBbox; string extraOptions = GetExtraOptions(); if (!string.IsNullOrEmpty(extraOptions)) strSQL += " " + extraOptions; using (SqlDataAdapter adapter = new SqlDataAdapter(strSQL, conn)) { conn.Open(); System.Data.DataSet ds2 = new System.Data.DataSet(); adapter.Fill(ds2); conn.Close(); if (ds2.Tables.Count > 0) { FeatureDataTable fdt = new FeatureDataTable(ds2.Tables[0]); foreach (System.Data.DataColumn col in ds2.Tables[0].Columns) if (col.ColumnName != GeometryColumn && col.ColumnName != "sharpmap_tempgeometry") fdt.Columns.Add(col.ColumnName,col.DataType,col.Expression); foreach (System.Data.DataRow dr in ds2.Tables[0].Rows) { FeatureDataRow fdr = fdt.NewRow(); foreach(System.Data.DataColumn col in ds2.Tables[0].Columns) if (col.ColumnName != GeometryColumn && col.ColumnName != "sharpmap_tempgeometry") fdr[col.ColumnName] = dr[col]; fdr.Geometry = Converters.WellKnownBinary.GeometryFromWKB.Parse((byte[])dr["sharpmap_tempgeometry"], Factory); fdt.AddRow(fdr); } ds.Add(fdt); } } } }
public override void ExecuteIntersectionQuery(Envelope box, IFeatureCollectionSet fcs, CancellationToken? ct= null) { using (var conn = SpatiaLiteConnection(ConnectionString)) { string cols = "*"; //If using rowid as oid, we need to explicitly request it! if (String.Compare(ObjectIdColumn, "rowid", StringComparison.OrdinalIgnoreCase) == 0) cols = "rowid,*"; var strSql = "SELECT " + cols + ", AsBinary(" + GeometryColumn + ") AS sharpmap_tempgeometry "; strSql += "FROM " + Table + " WHERE "; strSql += GetBoxClause(box); if (!String.IsNullOrEmpty(_definitionQuery)) strSql += " AND " + DefinitionQuery; using (var adapter = new SQLiteDataAdapter(strSql, conn)) { var ds2 = new DataSet(); adapter.Fill(ds2); conn.Close(); if (ds2.Tables.Count > 0) { var fdt = new FeatureDataTable(ds2.Tables[0]); foreach (DataColumn col in ds2.Tables[0].Columns) if (col.ColumnName != GeometryColumn && col.ColumnName != "sharpmap_tempgeometry" && !col.ColumnName.StartsWith("Envelope_")) fdt.Columns.Add(col.ColumnName, col.DataType, col.Expression); foreach (DataRow dr in ds2.Tables[0].Rows) { var fdr = fdt.NewRow(); foreach (DataColumn col in ds2.Tables[0].Columns) if (col.ColumnName != GeometryColumn && col.ColumnName != "sharpmap_tempgeometry" && !col.ColumnName.StartsWith("Envelope_")) fdr[col.ColumnName] = dr[col]; if (dr["sharpmap_tempgeometry"] != DBNull.Value) fdr.Geometry = GeometryFromWKB.Parse((byte[]) dr["sharpmap_tempgeometry"], Factory); fdt.AddRow(fdr); } fcs.Add(fdt); } } } }
/// <summary> /// Returns the features that intersects with 'geom' /// </summary> /// <param name="geom"></param> /// <param name="ds">FeatureDataSet to fill data into</param> protected override void OnExecuteIntersectionQuery(IGeometry geom, IFeatureCollectionSet fcs, CancellationToken? ct = null) { GetNonSpatialColumns(); using (var conn = new NpgsqlConnection(ConnectionString)) { conn.Open(); var strSql = "SELECT " + _columns + ", \"" + GeometryColumn + "\" AS \"_smtmp_\" "; strSql += "FROM " + Table + " WHERE "; // Attribute constraint if (!String.IsNullOrEmpty(_definitionQuery)) strSql += DefinitionQuery + " AND "; // Spatial constraint if (_supportSTIntersects) { strSql += "ST_Intersects(@PGeo ,\"" + GeometryColumn + "\")"; } else { strSql += "\"" + GeometryColumn + "\" && @PGeo AND " + _prefixFunction + "distance(\"" + GeometryColumn + "\", @PGeo)<0"; } /* string strGeom = _prefixFunction + "GeomFromText('" + geom.AsText() + "')"; if (SRID > 0) strGeom = _prefixFunction + "setSRID(" + strGeom + "," + SRID + ")"; string strSQL = "SELECT * , " + _prefixFunction + "AsBinary(\"" + GeometryColumn + "\") As sharpmap_tempgeometry FROM " + QualifiedTable + " WHERE "; if (!String.IsNullOrEmpty(_definitionQuery)) strSQL += DefinitionQuery + " AND "; strSQL += "\"" + GeometryColumn + "\" && " + strGeom + " AND " + _prefixFunction + "distance(\"" + GeometryColumn + "\", " + strGeom + ")<0"; */ if (Logger.IsDebugEnabled) Logger.Debug(string.Format("{0}\n{1}", "OnExecuteIntersectionQuery: executing sql:", strSql)); using (var cmd = new NpgsqlCommand(strSql, conn)) { geom.SRID = SRID; cmd.Parameters.AddWithValue("PGeo", new PostGisWriter().Write(geom)); using (var reader = cmd.ExecuteReader()) { var geomIndex = reader.FieldCount - 1; var fdt = CreateTableFromReader(reader, geomIndex); var dataTransfer = new object[geomIndex]; var geoReader = new PostGisReader(Factory.CoordinateSequenceFactory, Factory.PrecisionModel); fdt.BeginLoadData(); while (reader.Read()) { IGeometry g = null; if (!reader.IsDBNull(geomIndex)) g = geoReader.Read((byte[])reader.GetValue(geomIndex)); //No geometry, no feature! if (g == null) continue; //Get all the attribute data var count = reader.GetValues(dataTransfer); System.Diagnostics.Debug.Assert(count == dataTransfer.Length); var fdr = (FeatureDataRow)fdt.LoadDataRow(dataTransfer, true); fdr.Geometry = g; } fdt.EndLoadData(); fcs.Add(fdt); } } } }