/// <summary> /// Loads a test network from geojson. /// </summary> private static IEnumerable <Coordinate> LoadTestPoints(string geoJson) { var geoJsonReader = new NetTopologySuite.IO.GeoJsonReader(); var features = geoJsonReader.Read <FeatureCollection>(geoJson); foreach (var feature in features.Features) { var point = feature.Geometry as Point; if (point == null) { continue; ; } yield return(new Coordinate((float)point.Coordinate.Y, (float)point.Coordinate.X)); } }
public List <Object> CalculateDataTable(string polyfile) { //Calling EPA WATERS: //https://ofmpub.epa.gov/waters10/SpatialAssignment.Service?pGeometry=POINT(-76.7498+37.5)&pLayer=NHDPLUS_CATCHMENT&pAssignmentHint=9894716&pReturnGeometry=TRUE //List<GeoAPI.Geometries.IGeometry> polys = new List<GeoAPI.Geometries.IGeometry>(); List <GeoAPI.Geometries.IGeometry> squares = new List <GeoAPI.Geometries.IGeometry>(); ArrayList polys = new ArrayList(); ArrayList polyfeats = new ArrayList(); List <Object> infoTable = new List <Object>(); double squareArea = 0;//0.015625; double gridArea = 0; double polygonArea = 0; string catchmentID = ""; ////////////// string gridfile = @"M:\DotSpatTopology\tests\NLDAS_Grid_Reference.shp";//""; string gridproj = @"M:\DotSpatTopology\tests\NLDAS_Grid_Reference.prj"; ////////////// Guid gid = Guid.NewGuid(); string directory = @"M:\\TransientStorage\\" + gid.ToString() + "\\"; //This block is for getting and setting shapefiles for NLDAS Grid /** * client.DownloadFile("https://ldas.gsfc.nasa.gov/nldas/gis/NLDAS_Grid_Reference.zip", @"M:\\TransientStorage\\NLDAS.zip"); * ZipFile.ExtractToDirectory(@"M:\\TransientStorage\\NLDAS.zip", @"M:\\TransientStorage\\NLDAS"); * unzippedLocation = (@"M:\\TransientStorage\\NLDAS"); * foreach (string file in Directory.GetFiles(unzippedLocation)) * { * if (Path.GetExtension(file).Equals(".shp")) * { * gridfile = file; * } * else if (Path.GetExtension(file).Equals(".prj")) * { * gridproj = file; * } * } * client.Dispose();**/ ShapefileDataReader reader2 = new ShapefileDataReader(gridfile, NetTopologySuite.Geometries.GeometryFactory.Default); while (reader2.Read()) { squares.Add(reader2.Geometry); gridArea += reader2.Geometry.Area; } reader2.Dispose(); //if (polyfile.StartsWith(@"{""type"": ""FeatureCollection""")) if (polyfile.StartsWith(@"{""type"":"))//.geojson { Boolean version1 = true; string[] featureParams = new string[3]; string jsonfile = polyfile; var readera = new NetTopologySuite.IO.GeoJsonReader(); NetTopologySuite.Features.FeatureCollection result = readera.Read <NetTopologySuite.Features.FeatureCollection>(jsonfile); if (result[0].Attributes.GetNames().Contains("HUC_8")) { version1 = false; featureParams[0] = "OBJECTID"; featureParams[1] = "HUC_8"; featureParams[2] = "HUC_12"; } else if (result[0].Attributes.GetNames().Contains("HUC8")) { version1 = true; featureParams[0] = "COMID"; featureParams[1] = "HUC8"; featureParams[2] = "HUC12"; } else { version1 = false; featureParams[0] = null; featureParams[1] = null; featureParams[2] = null; } List <Object> huc8Table = new List <Object>(); Dictionary <string, string> h8 = new Dictionary <string, string>(); h8.Add("HUC 8 ID: ", result[0].Attributes[featureParams[1]].ToString()); huc8Table.Add(h8); //huc8Table.Add(new KeyValuePair<string, string>("HUC 8 ID: ", result[0].Attributes[featureParams[1]].ToString())); for (int i = 0; i < result.Count; i++) { List <Object> huc12Table = new List <Object>(); if (version1) { huc12Table.Add(new KeyValuePair <string, string>("HUC 12 ID: ", null)); } else { Dictionary <string, string> h12 = new Dictionary <string, string>(); h12.Add("HUC 12 ID: ", result[i].Attributes["HUC_12"].ToString()); huc12Table.Add(h12); //huc12Table.Add(new KeyValuePair<string, string>("HUC 12 ID: ", result[i].Attributes["HUC_12"].ToString())); } catchmentID = result[i].Attributes[featureParams[0]].ToString(); Dictionary <string, string> cm = new Dictionary <string, string>(); cm.Add("Catchment ID: ", catchmentID); huc12Table.Add(cm); //huc12Table.Add(new KeyValuePair<string, string>("Catchment ID: ", catchmentID)); foreach (GeoAPI.Geometries.IGeometry s in squares) { double interArea = 0.0; squareArea = s.Area; if (result[i].Geometry.Intersects(s)) { GeoAPI.Geometries.IGeometry intersection = result[i].Geometry.Intersection(s); interArea += intersection.Area; double percent2 = (interArea / squareArea) * 100; Dictionary <string, string> catchTable = new Dictionary <string, string>(); //catchTable.Add("catchmentID", catchmentID); catchTable.Add("latitude", s.Centroid.X.ToString()); catchTable.Add("longitude", s.Centroid.Y.ToString()); catchTable.Add("cellArea", squareArea.ToString()); catchTable.Add("containedArea", interArea.ToString()); catchTable.Add("percentArea", percent2.ToString()); huc12Table.Add(catchTable); } } huc8Table.Add(huc12Table); } infoTable.Add(huc8Table); } else //Huc ID { catchmentID = polyfile; string ending = polyfile + ".zip"; WebClient client = new WebClient(); DirectoryInfo di = Directory.CreateDirectory(directory); client.DownloadFile("ftp://newftp.epa.gov/exposure/NHDV1/HUC12_Boundries/" + ending, directory + ending); string projfile = ""; string dataFile = ""; ZipFile.ExtractToDirectory(directory + ending, directory + polyfile); string unzippedLocation = (directory + polyfile + "\\" + polyfile); //+ "\\NHDPlus" + polyfile + "\\Drainage"); foreach (string file in Directory.GetFiles(unzippedLocation)) { if (Path.GetExtension(file).Equals(".shp")) { polyfile = file; } else if (Path.GetExtension(file).Equals(".prj")) { projfile = file; } else if (Path.GetExtension(file).Equals(".dbf")) { dataFile = file; } } //This block is for setting projection parameters of input shapefile and projecting it to NLDAS grid //Reprojecting of coordinates is not needed for NHDPlus V2 string line = System.IO.File.ReadAllText(projfile); //string line = @"PROJCS[""unnamed"",GEOGCS[""unnamed ellipse"",DATUM[""unknown"",SPHEROID[""unnamed"",6378137,0]],PRIMEM[""Greenwich"",0],UNIT[""degree"",0.0174532925199433]],PROJECTION[""Mercator_2SP""],PARAMETER[""latitude_of_origin"",0],PARAMETER[""standard_parallel_1"",0],PARAMETER[""central_meridian"",0],PARAMETER[""false_easting"",0],PARAMETER[""false_northing"",0],UNIT[""Meter"",1]]";//System.IO.File.ReadAllText(projfile); string[] projParams = { "PARAMETER", @"PARAMETER[""latitude_of_origin"",0]," };//@"PARAMETER[""false_easting"",0],", @"PARAMETER[""false_northing"",0],", @"PARAMETER[""central_meridian"",0],", @"PARAMETER[""standard_parallel_1"",0],", @"PARAMETER[""standard_parallel_2"",0],", @"PARAMETER[""latitude_Of_origin"",0]," }; int ptr = 0; foreach (string x in projParams) { if (line.Contains(x)) { ptr = line.IndexOf(x); } else if (!line.Contains(x) && !x.Equals("PARAMETER")) { line = line.Insert(ptr, x); } } string line2 = System.IO.File.ReadAllText(gridproj); IProjectedCoordinateSystem pcs = CoordinateSystemWktReader.Parse(line) as IProjectedCoordinateSystem; IGeographicCoordinateSystem gcs = GeographicCoordinateSystem.WGS84 as IGeographicCoordinateSystem; CoordinateTransformationFactory ctfac = new CoordinateTransformationFactory(); ICoordinateTransformation transformTo = ctfac.CreateFromCoordinateSystems(pcs, gcs); IMathTransform inverseTransformTo = transformTo.MathTransform; //Read geometries from both shapefiles and store in array lists ShapefileDataReader reader = new ShapefileDataReader(polyfile, NetTopologySuite.Geometries.GeometryFactory.Default); DbaseFileHeader header = reader.DbaseHeader; string huc8 = ""; while (reader.Read()) { //Reprojection not needed for NHDPLUSV2 CoordinateList cordlist = new CoordinateList(); foreach (Coordinate coord in reader.Geometry.Coordinates) { double[] newCoord = { coord.X, coord.Y }; newCoord = inverseTransformTo.Transform(newCoord); Coordinate newpt = new Coordinate(newCoord[0], newCoord[1]); cordlist.Add(newpt); } Coordinate[] listofpts = cordlist.ToCoordinateArray(); IGeometryFactory geoFactory = new NetTopologySuite.Geometries.GeometryFactory(); NetTopologySuite.Geometries.LinearRing linear = (NetTopologySuite.Geometries.LinearRing) new GeometryFactory().CreateLinearRing(listofpts); Feature feature = new Feature(); AttributesTable attributesTable = new AttributesTable(); string[] keys = new string[header.NumFields]; IGeometry geometry = (Geometry)reader.Geometry; for (int i = 0; i < header.NumFields; i++) { DbaseFieldDescriptor fldDescriptor = header.Fields[i]; keys[i] = fldDescriptor.Name; attributesTable.AddAttribute(fldDescriptor.Name, reader.GetValue(i)); } feature.Geometry = geometry; feature.Attributes = attributesTable; polyfeats.Add(feature.Attributes); huc8 = attributesTable.GetValues()[5].ToString(); Polygon projPoly = new Polygon(linear, null, geoFactory); polys.Add(projPoly); polygonArea += projPoly.Area; } reader.Dispose(); List <Object> huc8Table = new List <Object>(); Dictionary <string, string> h8 = new Dictionary <string, string>(); h8.Add("HUC 8 ID: ", catchmentID); huc8Table.Add(h8); int j = 0; foreach (Polygon p in polys) { List <Object> huc12Table = new List <Object>(); Dictionary <string, string> cm = new Dictionary <string, string>(); // AttributesTable tab = (AttributesTable)polyfeats[j]; object[] valuesl = tab.GetValues(); cm.Add("HUC 12 ID: ", valuesl[17].ToString()); huc12Table.Add(cm); //huc12Table.Add(new KeyValuePair<string, string>("HUC 12 ID: ", null)); catchmentID = null;//result[i].Attributes["OBJECTID"].ToString(); Dictionary <string, string> cm2 = new Dictionary <string, string>(); cm2.Add("Catchment ID: ", catchmentID); huc12Table.Add(cm2); //huc12Table.Add(new KeyValuePair<string, string>("Catchment ID: ", catchmentID)); foreach (GeoAPI.Geometries.IGeometry s in squares) { double interArea = 0.0; squareArea = s.Area; if (p.Intersects(s)) { GeoAPI.Geometries.IGeometry intersection = p.Intersection(s); interArea += intersection.Area; double percent2 = (interArea / squareArea) * 100; Dictionary <string, string> catchTable = new Dictionary <string, string>(); //catchTable.Add("catchmentID", catchmentID); catchTable.Add("latitude", s.Centroid.X.ToString()); catchTable.Add("longitude", s.Centroid.Y.ToString()); catchTable.Add("cellArea", squareArea.ToString()); catchTable.Add("containedArea", interArea.ToString()); catchTable.Add("percentArea", percent2.ToString()); huc12Table.Add(catchTable); } } huc8Table.Add(huc12Table); j++; } infoTable.Add(huc8Table); } //System.IO.DirectoryInfo del = new DirectoryInfo(directory); /* * foreach (FileInfo file in del.GetFiles()) * { * file.Delete(); * } * foreach (DirectoryInfo dir in del.GetDirectories()) * { * dir.Delete(true); * }*/ //del.Delete(true); return(infoTable); }
public List <Object> CalculateDataTable(string polyfile) { //List<GeoAPI.Geometries.IGeometry> polys = new List<GeoAPI.Geometries.IGeometry>(); List <GeoAPI.Geometries.IGeometry> squares = new List <GeoAPI.Geometries.IGeometry>(); ArrayList polys = new ArrayList(); List <Object> infoTable = new List <Object>(); double squareArea = 0;//0.015625; double gridArea = 0; double polygonArea = 0; string catchmentID = ""; ////////////// string gridfile = @"M:\DotSpatTopology\tests\NLDAS_Grid_Reference.shp";//""; string gridproj = @"M:\DotSpatTopology\tests\NLDAS_Grid_Reference.prj"; ////////////// Guid gid = Guid.NewGuid(); string directory = @"M:\\TransientStorage\\" + gid.ToString() + "\\"; //This block is for getting and setting shapefiles for NLDAS Grid /** * client.DownloadFile("https://ldas.gsfc.nasa.gov/nldas/gis/NLDAS_Grid_Reference.zip", @"M:\\TransientStorage\\NLDAS.zip"); * ZipFile.ExtractToDirectory(@"M:\\TransientStorage\\NLDAS.zip", @"M:\\TransientStorage\\NLDAS"); * unzippedLocation = (@"M:\\TransientStorage\\NLDAS"); * foreach (string file in Directory.GetFiles(unzippedLocation)) * { * if (Path.GetExtension(file).Equals(".shp")) * { * gridfile = file; * } * else if (Path.GetExtension(file).Equals(".prj")) * { * gridproj = file; * } * } * client.Dispose();**/ ShapefileDataReader reader2 = new ShapefileDataReader(gridfile, NetTopologySuite.Geometries.GeometryFactory.Default); while (reader2.Read()) { squares.Add(reader2.Geometry); gridArea += reader2.Geometry.Area; } reader2.Dispose(); //if (polyfile.StartsWith(@"{""type"": ""FeatureCollection""")) if (polyfile.StartsWith(@"{""type"":"))//.geojson { Boolean version1 = true; string[] featureParams = new string[3]; string jsonfile = polyfile; var readera = new NetTopologySuite.IO.GeoJsonReader(); NetTopologySuite.Features.FeatureCollection result = readera.Read <NetTopologySuite.Features.FeatureCollection>(jsonfile); if (result[0].Attributes.GetNames().Contains("HUC_8")) { version1 = false; featureParams[0] = "OBJECTID"; featureParams[1] = "HUC_8"; featureParams[2] = "HUC_12"; } else if (result[0].Attributes.GetNames().Contains("HUC8")) { version1 = true; featureParams[0] = "COMID"; featureParams[1] = "HUC8"; featureParams[2] = "HUC12"; } else { version1 = false; featureParams[0] = null; featureParams[1] = null; featureParams[2] = null; } List <Object> huc8Table = new List <Object>(); huc8Table.Add(new KeyValuePair <string, string>("HUC 8 ID: ", result[0].Attributes[featureParams[1]].ToString())); for (int i = 0; i < result.Count; i++) { List <Object> huc12Table = new List <Object>(); if (version1) { huc12Table.Add(new KeyValuePair <string, string>("HUC 12 ID: ", null)); } else { huc12Table.Add(new KeyValuePair <string, string>("HUC 12 ID: ", result[i].Attributes["HUC_12"].ToString())); } catchmentID = result[i].Attributes[featureParams[0]].ToString(); huc12Table.Add(new KeyValuePair <string, string>("Catchment ID: ", catchmentID)); foreach (GeoAPI.Geometries.IGeometry s in squares) { double interArea = 0.0; squareArea = s.Area; if (result[i].Geometry.Intersects(s)) { GeoAPI.Geometries.IGeometry intersection = result[i].Geometry.Intersection(s); interArea += intersection.Area; double percent2 = (interArea / squareArea) * 100; Dictionary <string, string> catchTable = new Dictionary <string, string>(); //catchTable.Add("catchmentID", catchmentID); catchTable.Add("latitude", s.Centroid.X.ToString()); catchTable.Add("longitude", s.Centroid.Y.ToString()); catchTable.Add("cellArea", squareArea.ToString()); catchTable.Add("containedArea", interArea.ToString()); catchTable.Add("percentArea", percent2.ToString()); huc12Table.Add(catchTable); } } huc8Table.Add(huc12Table); } infoTable.Add(huc8Table); } else //Huc ID { catchmentID = polyfile; string ending = polyfile + ".zip"; WebClient client = new WebClient(); DirectoryInfo di = Directory.CreateDirectory(directory); client.DownloadFile("ftp://newftp.epa.gov/exposure/NHDV1/HUC12_Boundries/" + ending, directory + ending); string projfile = ""; string dataFile = ""; ZipFile.ExtractToDirectory(directory + ending, directory + polyfile); string unzippedLocation = (directory + polyfile + "\\" + polyfile); //+ "\\NHDPlus" + polyfile + "\\Drainage"); foreach (string file in Directory.GetFiles(unzippedLocation)) { if (Path.GetExtension(file).Equals(".shp")) { polyfile = file; } else if (Path.GetExtension(file).Equals(".prj")) { projfile = file; } else if (Path.GetExtension(file).Equals(".dbf")) { dataFile = file; } } //This block is for setting projection parameters of input shapefile and projecting it to NLDAS grid //Reprojecting of coordinates is not needed for NHDPlus V2 string line = System.IO.File.ReadAllText(projfile); string[] projParams = { "PARAMETER", @"PARAMETER[""latitude_Of_origin"",0]," };//@"PARAMETER[""false_easting"",0],", @"PARAMETER[""false_northing"",0],", @"PARAMETER[""central_meridian"",0],", @"PARAMETER[""standard_parallel_1"",0],", @"PARAMETER[""standard_parallel_2"",0],", @"PARAMETER[""latitude_Of_origin"",0]," }; int ptr = 0; foreach (string x in projParams) { if (line.Contains(x)) { ptr = line.IndexOf(x); } else if (!line.Contains(x) && !x.Equals("PARAMETER")) { line = line.Insert(ptr, x); } } string line2 = System.IO.File.ReadAllText(gridproj); IProjectedCoordinateSystem pcs = CoordinateSystemWktReader.Parse(line) as IProjectedCoordinateSystem; IGeographicCoordinateSystem gcs = GeographicCoordinateSystem.WGS84 as IGeographicCoordinateSystem; CoordinateTransformationFactory ctfac = new CoordinateTransformationFactory(); ICoordinateTransformation transformTo = ctfac.CreateFromCoordinateSystems(pcs, gcs); IMathTransform inverseTransformTo = transformTo.MathTransform; //Read geometries from both shapefiles and store in array lists //As well as calculate shapefile areas ahead of time ShapefileDataReader reader = new ShapefileDataReader(polyfile, NetTopologySuite.Geometries.GeometryFactory.Default); while (reader.Read()) { //Reprojection not needed for NHDPLUSV2 CoordinateList cordlist = new CoordinateList(); foreach (Coordinate coord in reader.Geometry.Coordinates) { double[] newCoord = { coord.X, coord.Y }; newCoord = inverseTransformTo.Transform(newCoord); Coordinate newpt = new Coordinate(newCoord[0], newCoord[1]); cordlist.Add(newpt); } Coordinate[] listofpts = cordlist.ToCoordinateArray(); IGeometryFactory geoFactory = new NetTopologySuite.Geometries.GeometryFactory(); NetTopologySuite.Geometries.LinearRing linear = (NetTopologySuite.Geometries.LinearRing) new GeometryFactory().CreateLinearRing(listofpts); Polygon projPoly = new Polygon(linear, null, geoFactory); polys.Add(projPoly); polygonArea += projPoly.Area; } reader.Dispose(); List <Object> huc8Table = new List <Object>(); huc8Table.Add(new KeyValuePair <string, string>("HUC 8 ID: ", catchmentID)); foreach (Polygon p in polys) { List <Object> huc12Table = new List <Object>(); huc12Table.Add(new KeyValuePair <string, string>("HUC 12 ID: ", null)); catchmentID = null;//result[i].Attributes["OBJECTID"].ToString(); huc12Table.Add(new KeyValuePair <string, string>("Catchment ID: ", catchmentID)); foreach (GeoAPI.Geometries.IGeometry s in squares) { double interArea = 0.0; squareArea = s.Area; if (p.Intersects(s)) { GeoAPI.Geometries.IGeometry intersection = p.Intersection(s); interArea += intersection.Area; double percent2 = (interArea / squareArea) * 100; Dictionary <string, string> catchTable = new Dictionary <string, string>(); //catchTable.Add("catchmentID", catchmentID); catchTable.Add("latitude", s.Centroid.X.ToString()); catchTable.Add("longitude", s.Centroid.Y.ToString()); catchTable.Add("cellArea", squareArea.ToString()); catchTable.Add("containedArea", interArea.ToString()); catchTable.Add("percentArea", percent2.ToString()); huc12Table.Add(catchTable); } } huc8Table.Add(huc12Table); } infoTable.Add(huc8Table); } //System.IO.DirectoryInfo del = new DirectoryInfo(directory); /* * foreach (FileInfo file in del.GetFiles()) * { * file.Delete(); * } * foreach (DirectoryInfo dir in del.GetDirectories()) * { * dir.Delete(true); * }*/ //del.Delete(true); ///// //infoTable.Add(new List<Object>() { elapsedTime, elapsedTime, elapsedTime, elapsedTime }); ////// return(infoTable); }