public List<Feature> GetShapeFeaturesToExport(string tableName, bool spatialFilter)
{
using (var conn = DbClient.GetConnection())
{
Dictionary<string, DataRow> shapeDict = GetShapeRowsByLOGRECNO(conn);
var variablesDT = DataClient.GetMagicTable(conn, DbClient, string.Format("select * from \"{0}\" ", tableName));
if ((variablesDT == null) || (variablesDT.Rows.Count == 0))
{
_log.Warn("Nothing to export, data table is empty");
_log.Error("Nothing to export, data table is empty");
return null;
}
bool shouldReproject = (!string.IsNullOrEmpty(this.OutputProjectionFilename));
ICoordinateTransformation reprojector = null;
ICoordinateSystem destCRS = null;
if (!string.IsNullOrEmpty(this.OutputProjectionFilename))
{
destCRS = Utilities.GetCoordinateSystemByWKTFile(this.OutputProjectionFilename);
reprojector = Utilities.BuildTransformationObject(GeographicCoordinateSystem.WGS84, destCRS);
}
//TODO:
bool shouldAddMissingShapes = this.IncludeEmptyGridCells;
HashSet<string> allShapeIDs = new HashSet<string>(shapeDict.Keys);
//Build hashset, remove by shapeid as shapes are added,
//go through and add missing shapes if 'shouldAddMissingShapes' is set...
bool variablesHaveGeoID = variablesDT.Columns.Contains("GEOID");
List<IGeometry> filteringGeoms = null;
if (!string.IsNullOrEmpty(this.ExportFilterFilename))
{
filteringGeoms = (spatialFilter) ? GetFilteringGeometries(this.ExportFilterFilename, destCRS) : null;
//Die if we're supposed to have a filter but don't
if (spatialFilter && filteringGeoms == null)
{
return null;
}
}
GisSharpBlog.NetTopologySuite.IO.WKBReader binReader = new WKBReader(
ShapefileHelper.GetGeomFactory());
var features = new List<Feature>(variablesDT.Rows.Count);
foreach (DataRow row in variablesDT.Rows)
{
var id = row["LOGRECNO"] as string;
if (!shapeDict.ContainsKey(id))
continue;
allShapeIDs.Remove(id);
AttributesTable t = new AttributesTable();
foreach (DataColumn col in variablesDT.Columns)
{
//produces more sane results.
t.AddAttribute(
Utilities.EnsureMaxLength(col.ColumnName, 10),
Utilities.GetAsType(col.DataType, row[col.Ordinal], null)
);
}
byte[] geomBytes = (byte[])shapeDict[id]["Geometry"];
IGeometry geom = binReader.Read(geomBytes);
if (geom == null)
{
_log.WarnFormat("Geometry for LRN {0} was empty!", id);
continue;
}
if (shouldReproject)
{
geom = Utilities.ReprojectGeometry(geom, reprojector);
}
if (spatialFilter)
{
if (!IsIncluded(geom, filteringGeoms))
{
continue;
}
}
if (!variablesHaveGeoID)
{
t.AddAttribute("GEOID", shapeDict[id]["GEOID"]);
}
if (this.AddStrippedGEOIDcolumn)
{
t.AddAttribute("GEOID_STRP", (t["GEOID"] as string).Replace(Settings.GeoidPrefix, ""));
}
if (this.AddGeometryAttributesToOutput)
{
t.AddAttribute("AREA", geom.Area);
t.AddAttribute("PERIMETER", geom.Length);
var centroid = geom.Centroid;
t.AddAttribute("CENTROID_X", centroid.X);
t.AddAttribute("CENTROID_Y", centroid.Y);
}
features.Add(new Feature(geom, t));
}
if (shouldAddMissingShapes)
{
_log.DebugFormat("Adding {0} missing shapes", allShapeIDs.Count);
foreach (string id in allShapeIDs)
{
byte[] geomBytes = (byte[])shapeDict[id]["Geometry"];
IGeometry geom = binReader.Read(geomBytes);
if (geom == null)
{
_log.WarnFormat("Geometry for LRN {0} was empty!", id);
continue;
}
if (shouldReproject)
{
geom = Utilities.ReprojectGeometry(geom, reprojector);
}
if (spatialFilter)
{
if (!IsIncluded(geom, filteringGeoms))
{
continue;
}
}
AttributesTable t = new AttributesTable();
foreach (DataColumn col in variablesDT.Columns)
{
//produces more sane results.
t.AddAttribute(Utilities.EnsureMaxLength(col.ColumnName, 10), null);
}
if (!variablesHaveGeoID)
{
t.AddAttribute("GEOID", shapeDict[id]["GEOID"]);
}
if (this.AddStrippedGEOIDcolumn)
{
t.AddAttribute("GEOID_STRP", (t["GEOID"] as string).Replace(Settings.GeoidPrefix, ""));
}
if (this.AddGeometryAttributesToOutput)
{
t.AddAttribute("AREA", geom.Area);
t.AddAttribute("PERIMETER", geom.Length);
var centroid = geom.Centroid;
t.AddAttribute("CENTROID_X", centroid.X);
t.AddAttribute("CENTROID_Y", centroid.Y);
}
t["LOGRECNO"] = id;
features.Add(new Feature(geom, t));
}
}
return features;
}
}