Skip to content

Commit

Permalink
Lots of misc cleanup
Browse files Browse the repository at this point in the history
* Environment.Exit() calls replaced with ApplicationExceptions
* Fix minor bug in Shapefilehelper
* Change locations of config files
  • Loading branch information
Bennet Huber committed Mar 1, 2014
1 parent 3495962 commit 18205bf
Show file tree
Hide file tree
Showing 9 changed files with 38 additions and 138 deletions.
22 changes: 0 additions & 22 deletions csharp/Azavea.NijPredictivePolicing.AcsImporter/ImportJob.cs
Expand Up @@ -412,14 +412,7 @@ public bool ExecuteJob()
manager.AddStrippedGEOIDcolumn = (!string.IsNullOrEmpty(this.AddStrippedGEOIDcolumn));
manager.AddGeometryAttributesToOutput = (!string.IsNullOrEmpty(this.AddGeometryAttributesToOutput));
manager.OutputFolder = FileUtilities.CleanPath(OutputFolder);


//if (!string.IsNullOrEmpty(this.WorkingFolder))
//{
// manager.WorkingPath = FileUtilities.SafePathEnsure(this.WorkingFolder);
//}
manager.IncludeEmptyGridCells = (!string.IsNullOrEmpty(this.IncludeEmptyGridCells));
//manager.SRID = Utilities.GetAs<int>(this.ExportFilterSRID, -1);


if (FileUtilities.SafePathEnsure(OutputFolder) != OutputFolder)
Expand Down Expand Up @@ -617,30 +610,15 @@ public void SaveJobFile(string filename)
val = val + " #" + Utilities.GetAs<BoundaryLevels>(val, BoundaryLevels.census_blockgroups);
}



//sb.Append("#").Append(nl);
sb.Append("# ").Append(arg.Description).Append(nl);
//sb.Append("#").Append(nl);
sb.Append("-" + arg.Flag).Append(" ").Append(val).Append(nl);
sb.Append(nl);



//object defval = (prop.PropertyType == typeof(AcsState)) ? (object)AcsState.None : null;
//prop.GetValue(this, Utilities.GetAsType(prop.PropertyType, contents, defval), null);

//break;
}

File.WriteAllText(filename, sb.ToString());
}






public bool WorkOffline { get; set; }
}
}
9 changes: 7 additions & 2 deletions csharp/Azavea.NijPredictivePolicing.AcsImporter/Main.cs
Expand Up @@ -35,6 +35,9 @@ namespace Azavea.NijPredictivePolicing.ACSAlchemist
public class Program
{
private static ILog _log = null;

public const string DefaultConfigPath = "configs/AcsAlchemist.json.config";

protected static void Init()
{
try
Expand Down Expand Up @@ -99,10 +102,12 @@ public static void ShowCopyrightAndLicense()
/// </summary>
protected static void LoadConfigFile()
{
Settings.ConfigFile = new Config(Path.Combine(Settings.ApplicationPath, "AcsAlchemist.json.config"));
string configName = Path.Combine(Settings.ApplicationPath, DefaultConfigPath);
Settings.ConfigFile = new Config(configName);
if (Settings.ConfigFile.IsEmpty())
{
Settings.RestoreDefaults();
_log.FatalFormat("Config file {0} missing or empty! Cannot continue, exiting", DefaultConfigPath);
Environment.Exit((int)Constants.ExitCodes.BadConfig);
}

//search for our 'per-year' config files that explain which paths to check, file naming, etc.
Expand Down
Expand Up @@ -147,16 +147,14 @@ public List<string> AvailableProjections



#region Initialization Boilerplate


private static ILog _log = null;

public const string DefaultConfigPath = "configs/AcsAlchemist.json.config";



#region Initialization Boilerplate


private static ILog _log = null;
internal void InitLogging(IAppender appenderObj)
{
if (_log != null)
Expand Down Expand Up @@ -193,14 +191,9 @@ protected void ShowWelcomeScreen()
_log.Debug("Welcome to ACS Alchemist");
_log.Debug("");


_log.Debug(@"This project was supported by Award No. 2010-DE-BX-K004, awarded by the National Institute of Justice, Office of Justice Programs, U.S. Department of Justice. The opinions, findings, and conclusions or recommendations expressed in this software are those of the author(s) and do not necessarily reflect those of the Department of Justice or Temple University. The software was developed by Azavea in connection with the National Institute of Justice grant awarded to Jerry Ratcliffe and Ralph Taylor of Temple University's Center for Security and Crime Science. The source code is released under a GPLv3 license and is available at: https://github.com/azavea/acs-alchemist");

_log.Debug("");

//_log.Debug(@"This project was supported by Award No. 2010-DE-BX-K004, awarded by the National Institute of Justice, Office of Justice Programs, U.S. Department of Justice. The opinions, findings, and conclusions or recommendations expressed in this software are those of the author(s) and do not necessarily reflect those of the Department of Justice. The source code is released under a GPLv3 license and is available at: https://github.com/azavea/acs-alchemist");

//_log.Debug("");
}

protected void ShowCopyrightAndLicense()
Expand All @@ -220,10 +213,12 @@ protected void ShowCopyrightAndLicense()
/// </summary>
protected void LoadConfigFile()
{
Settings.ConfigFile = new Config(Path.Combine(Settings.ApplicationPath, "AcsAlchemist.json.config"));
string configName = Path.Combine(Settings.ApplicationPath, DefaultConfigPath);
Settings.ConfigFile = new Config(configName);
if (Settings.ConfigFile.IsEmpty())
{
Settings.RestoreDefaults();
_log.FatalFormat("Config file {0} missing or empty! Cannot continue, exiting", DefaultConfigPath);
Environment.Exit((int)Constants.ExitCodes.BadConfig);
}

Settings.LoadYearConfigs();
Expand All @@ -235,27 +230,15 @@ protected void LoadConfigFile()
/// </summary>
internal void Initialize()
{
//InitLogging();

LoadConfigFile();
ShowWelcomeScreen();
ShowCopyrightAndLicense();

//this.JobInstance = new ImportJob();
NewDefaultJobInstance();
}


#endregion Initialization Boilerplate









/// <summary>
/// Performs some sanity checks on our variables file
/// (does it exist, can I read it, does it have at least one variable, etc.)
Expand Down
Expand Up @@ -39,7 +39,7 @@ public enum AcsState
Colorado = 08,
Connecticut = 09,
Delaware = 10,
DistrictofColumbia = 11,
//DistrictofColumbia = 11, // This appears to have been removed from ACS downloads at some point
Florida = 12,
Georgia = 13,
Hawaii = 15,
Expand Down Expand Up @@ -86,8 +86,6 @@ public enum AcsState

PuertoRico = 72,
None = -1


}


Expand All @@ -106,17 +104,6 @@ public static string StateToCensusName(AcsState state)
return state.ToString();
}

///// <summary>
///// Shows how to use the settings
///// </summary>
///// <param name="state"></param>
///// <returns></returns>
//public static string StateToCurrentBlockGroupFilename(AcsState state)
//{
// return string.Concat(state.ToString(), Settings.BlockGroupsDataTableSuffix);
//}


/// <summary>
/// Get's a pretty name for a state
/// </summary>
Expand Down
Expand Up @@ -78,7 +78,6 @@ public static bool LoadShapefile(string filename, string tableName, IDataClient
return false;
}

//string workingDirectory = FileUtilities.SafePathEnsure(TempPath, Path.GetFileNameWithoutExtension(filename));
using (DbConnection conn = DbClient.GetConnection())
{
if (!ShapefileHelper.ImportShapefile(conn, DbClient, filename, tableName, (int)CRS.AuthorityCode))
Expand Down Expand Up @@ -213,7 +212,7 @@ public static bool MakeOutputProjFile(string sourceProjectionFilename, string de

if (File.Exists(sourceProjectionFilename))
{
File.Copy(sourceProjectionFilename, prjFileName, true); //File.WriteAllText(prjFileName, File.ReadAllText(wktProjFilename));
File.Copy(sourceProjectionFilename, prjFileName, true);
}
else
{
Expand Down Expand Up @@ -244,18 +243,6 @@ public static GeometryFactory GetGeomFactory()
return new GeometryFactory(new PrecisionModel(), 4269);
}

///// <summary>
///// Helper function for cre
///// </summary>
///// <param name="shapefilename"></param>
///// <param name="acsState"></param>
///// <returns></returns>
//public static System.Data.DataTable GetTable(string shapefilename, AcsState acsState)
//{
// return Shapefile.CreateDataTable(shapefilename, acsState.ToString(), ShapefileHelper.GetGeomFactory());
//}


/// <summary>
/// Consumes an ADO.net datatable and correctly initializes a Shapefile header object
/// </summary>
Expand Down Expand Up @@ -295,8 +282,6 @@ public static void AddColumn(DbaseFileHeader header, string columnName, Type t)
}
else if (t == typeof(DateTime))
{
// D stores only the date
//retVal.AddColumn(shapefileColumnName, 'D', 8, 0);
header.AddColumn(columnName, 'C', 22, 0);
}
else if (t == typeof(float) || t == typeof(double) || t == typeof(decimal))
Expand Down Expand Up @@ -374,15 +359,19 @@ public static string GetRemoteShapefileURL(BoundaryLevels level, string stateFip
/// <returns></returns>
public static bool IsForbiddenShapefileName(string name)
{
string[] levels = new string[] {
if (string.IsNullOrEmpty(name))
{
return true;
}
var levels = (new List<string> {
Settings.ShapeFileBlockGroupFilename,
Settings.ShapeFileTractFilename,
Settings.ShapeFileCountySubdivisionsFilename,
Settings.ShapeFileVotingFilename,
Settings.ShapeFileThreeDigitZipsFilename,
Settings.ShapeFileFiveDigitZipsFilename,
Settings.ShapeFileCountiesFilename
};
}).Where(s => !string.IsNullOrEmpty(s));

StringBuilder newRegex = new StringBuilder(512);
foreach (string level in levels)
Expand Down
Expand Up @@ -164,7 +164,6 @@ public AcsDataManager(AcsState aState, string workingFolder, string year)
if (!string.IsNullOrEmpty(workingFolder))
{
//override where we're storing temporary files
//Settings.AppDataDirectory = FileUtilities.SafePathEnsure(workingFolder, Settings.RequestedYear);
Settings.AppDataDirectory = FileUtilities.SafePathEnsure(workingFolder);
}
_log.InfoFormat("Working directory is {0} ", workingFolder);
Expand Down Expand Up @@ -415,12 +414,6 @@ public bool DownloadAndImportShapefile(string desiredUrl, string destFilepath, s
var client = DbClient;
using (var conn = client.GetConnection())
{
//if (DataClient.HasTable(conn, client, tablename))
//{
// _log.DebugFormat("{0} table already exists, skipping...", tablename);
// return true;
//}
//else

if (DataClient.HasTable(conn, client, tablename))
{
Expand Down Expand Up @@ -1186,13 +1179,6 @@ public GetGeometryRowKey GetGeometryRowKeyGenerator()

foreach (DataRow row in wholeShapeTable.Rows)
{
//string county = Utilities.GetAs<string>(row["COUNTY"], "-1");
//string tract = Utilities.GetAs<string>(row["TRACT"], "-1");
//string blkgroup = Utilities.GetAs<string>(row["BLKGROUP"], "-1");
//if (tract.Trim().Length != 6)
// tract += "00";
//string key = string.Format("{0}_{1}_{2}", county, tract, blkgroup);

string key = keyDelegate(row);

if (geomKeys.ContainsKey(key))
Expand Down Expand Up @@ -1270,8 +1256,6 @@ public bool IsIncluded(IGeometry geom, List<IGeometry> filteringGeoms)
var fg = filt.GetGeometryN(g);
if (fg.Intersects(geom))
{
//double commonArea = fg.Intersection(geom).Area;
//if(commonArea > 0.01 * fg.Area || commonArea > 0.01 * geom.Area)
return true;
}
}
Expand Down Expand Up @@ -1306,12 +1290,6 @@ public List<Feature> GetShapeFeaturesToExport(string tableName, bool spatialFilt
{
destCRS = Utilities.GetCoordinateSystemByWKTFile(this.OutputProjectionFilename);
reprojector = Utilities.BuildTransformationObject(GeographicCoordinateSystem.WGS84, destCRS);

//Reproject everything in this file to the requested projection...
//exportFeatures = Utilities.ReprojectFeaturesTo(exportFeatures, this.OutputProjectionFilename);

//THESE MUST BE PROVIDED ALREADY PROJECTED!
//filteringGeoms = Utilities.ReprojectFeaturesTo(filteringGeoms, this.OutputProjectionFilename);
}

//TODO:
Expand All @@ -1333,7 +1311,6 @@ public List<Feature> GetShapeFeaturesToExport(string tableName, bool spatialFilt
}
}



GisSharpBlog.NetTopologySuite.IO.WKBReader binReader = new WKBReader(
ShapefileHelper.GetGeomFactory());
Expand Down Expand Up @@ -1484,8 +1461,10 @@ public bool ExportShapefile(string tableName)
DbaseFileHeader header = null;
using (var conn = DbClient.GetConnection())
{
//Dictionary<string, DataRow> shapeDict = GetShapeRowsByLOGRECNO(conn);
var variablesDT = DataClient.GetMagicTable(conn, DbClient, string.Format("SELECT * FROM \"{0}\" where 0 = 1 ", tableName));
var variablesDT = DataClient.GetMagicTable(
conn,
DbClient,
string.Format("SELECT * FROM \"{0}\" where 0 = 1 ", tableName));
header = ShapefileHelper.SetupHeader(variablesDT);
}

Expand Down Expand Up @@ -1538,9 +1517,10 @@ public bool ExportShapefile(string tableName)
}
catch (FileNotFoundException notFound)
{
_log.Error("A needed file couldn't be found: " + notFound.FileName);
string msg = "A needed file couldn't be found: " + notFound.FileName;
_log.Error(msg);
_log.Fatal("The export cannot continue. Exiting...");
Environment.Exit(-1);
throw new ApplicationException(msg);
}
catch (Exception ex)
{
Expand All @@ -1550,8 +1530,6 @@ public bool ExportShapefile(string tableName)
}




/// <summary>
/// Retrieves the contents of the specified table, and writes them to a shapefile
/// </summary>
Expand Down Expand Up @@ -1618,9 +1596,6 @@ public bool ExportGriddedShapefile(string tableName)


var features = new List<Feature>(exportFeatures.Count);
//var cellStepPoint = Utilities.GetCellFeetForProjection(GridCellWidthFeet, GridCellHeightFeet);
//double cellWidth = cellStepPoint.X;
//double cellHeight = cellStepPoint.Y;

double cellWidth = GridCellWidth;
double cellHeight = GridCellHeight;
Expand Down Expand Up @@ -1780,9 +1755,10 @@ public bool ExportGriddedShapefile(string tableName)
}
catch (FileNotFoundException notFound)
{
_log.Error("A needed file couldn't be found: " + notFound.FileName);
string msg = "A needed file couldn't be found: " + notFound.FileName;
_log.Error(msg);
_log.Fatal("The export cannot continue. Exiting...");
Environment.Exit(-1);
throw new ApplicationException(msg);
}
catch (Exception ex)
{
Expand Down Expand Up @@ -1817,13 +1793,6 @@ public Envelope GetGridEnvelope()
}









public void Dispose()
{
if (DbClient != null)
Expand Down Expand Up @@ -1874,8 +1843,6 @@ public void Cancel()
_cancelled = true;
}



public bool WorkOffline { get; set; }
}
}

0 comments on commit 18205bf

Please sign in to comment.