public List<IGeometry> GetFilteringGeometries(string filename, ICoordinateSystem outputCrs)
{
if ((string.IsNullOrEmpty(filename)) || (!File.Exists(filename)))
{
_log.ErrorFormat("GetFilteringGeometries failed, shapefile is empty or does not exist {0} ", filename);
return null;
}
//inputCrs should be valid if LoadShapefile returns true
ICoordinateSystem inputCrs;
if (!ShapefileHelper.LoadShapefile(filename, Path.GetFileNameWithoutExtension(filename), DbClient, out inputCrs))
{
_log.Error("Could not load filtering geometries!");
return null;
}
string tableName = Path.GetFileNameWithoutExtension(filename);
string sqlcmd = string.Format("SELECT AsBinary(Geometry) AS Geometry FROM \"{0}\" ", tableName);
var wholeShapeTable = DataClient.GetMagicTable(DbClient.GetConnection(), DbClient, sqlcmd);
List<IGeometry> results = new List<IGeometry>(wholeShapeTable.Rows.Count);
ICoordinateTransformation reprojector = null;
if (outputCrs != null)
{
reprojector = Utilities.BuildTransformationObject(inputCrs, outputCrs);
}
GisSharpBlog.NetTopologySuite.IO.WKBReader binReader = new WKBReader(
new GeometryFactory());
foreach (DataRow row in wholeShapeTable.Rows)
{
byte[] geomBytes = (byte[])row["Geometry"];
IGeometry geom = binReader.Read(geomBytes);
if (geom == null || geom.Dimension != Dimensions.Surface)
{
continue;
}
if (reprojector != null)
{
geom = Utilities.ReprojectGeometry(geom, reprojector);
}
results.Add(geom);
}
//Cleanup so we don't store lots of crap in database
sqlcmd = string.Format("DROP TABLE \"{0}\" ", tableName);
var droptable = DbClient.GetCommand(sqlcmd);
droptable.ExecuteNonQuery();
if (results.Count == 0)
{
_log.Error("Could not use shapefile to create filter because no two dimensional geometries were found");
return null;
}
return results;
}