public void Multils() { Random rnd = new Random(); LineString[] ls = new LineString[40]; GeoAPI.Geometries.ILineString[] lscheck = new GeoAPI.Geometries.ILineString[40]; GisSharpBlog.NetTopologySuite.Geometries.GeometryFactory gf = new GisSharpBlog.NetTopologySuite.Geometries.GeometryFactory(); for (int ii = 0; ii < 40; ii++) { Coordinate[] coord = new Coordinate[36]; GeoAPI.Geometries.ICoordinate[] coordcheck = new GeoAPI.Geometries.ICoordinate[36]; for (int i = 0; i < 36; i++) { coord[i] = new Coordinate((rnd.NextDouble() * 360) - 180, (rnd.NextDouble() * 180) - 90); double x = coord[i].X; double y = coord[i].Y; GisSharpBlog.NetTopologySuite.Geometries.Coordinate c = new GisSharpBlog.NetTopologySuite.Geometries.Coordinate(x, y); coordcheck[i] = c; } ls[ii] = new LineString(coord); lscheck[ii] = gf.CreateLineString(coordcheck); } MultiLineString mls = new MultiLineString(ls); GeoAPI.Geometries.IMultiLineString mlscheck = gf.CreateMultiLineString(lscheck); for (int ii = 0; ii < mls.Coordinates.Count; ii++) { Assert.AreEqual(mls.Coordinates[ii].X, mlscheck.Coordinates[ii].X); Assert.AreEqual(mls.Coordinates[ii].Y, mlscheck.Coordinates[ii].Y); } Assert.AreEqual(mls.NumGeometries, mlscheck.NumGeometries); }
internal FeatureSet CreateFeatureSet() { FeatureSet featureSet = new FeatureSet(FeatureType.Line); featureSet.Projection = KnownCoordinateSystems.Geographic.World.WGS1984; foreach (ReferentielMultiLineString mls in this.MultiLineStrings) { List<LineString> lineStrings = new List<LineString>(); foreach (ReferentielLineString ls in mls.LineStrings) { List<Coordinate> coordinates = new List<Coordinate>(); for (int i = 0; i < (ls.Segements.Count - 1); i++) { coordinates.Add(ls.Segements[i].CoordDeb); } coordinates.Add(ls.Segements.Last().CoordDeb); coordinates.Add(ls.Segements.Last().CoordFin); LineString fsLs = new LineString(coordinates); lineStrings.Add(fsLs); } MultiLineString fsMls = new MultiLineString(lineStrings); featureSet.AddFeature(fsMls); } return featureSet; }
private void MlsCS(object sender, EventArgs e) { Random rnd = new Random(); MultiLineString Mls = new MultiLineString(); LineString[] ls = new LineString[40]; for (int ii = 0; ii < 40; ii++) { Coordinate[] coord = new Coordinate[36]; for (int i = 0; i < 36; i++) { coord[i] = new Coordinate((rnd.NextDouble() * 360) - 180, (rnd.NextDouble() * 180) - 90); } ls[ii] = new LineString(coord); } Mls = new MultiLineString(ls); FeatureSet fs = new FeatureSet(Mls.FeatureType); fs.Features.Add(Mls); fs.SaveAs("C:\\Temp\\Mls.shp", true); }
/// <summary> /// Creates a <c>MultiLineString</c> using the given <c>LineStrings</c>; a null or empty /// array will create an empty MultiLineString. /// </summary> /// <param name="lineStrings">LineStrings, each of which may be empty but not null-</param> public virtual IMultiLineString CreateMultiLineString(IBasicLineString[] lineStrings) { if (lineStrings == null) { return new MultiLineString(); } int count = lineStrings.Length; LineString[] ls = new LineString[count]; for (int i = 0; i < count; i++) { ls[i] = new LineString(lineStrings[i]); } MultiLineString temp = new MultiLineString(ls); return temp; }
public static Geometry CreateGeometryFromWkt(string strData) { if (strData.StartsWith("LINESTRING")) { strData = strData.Substring(10); strData = strData.TrimStart().TrimEnd(); if (strData.StartsWith("(")) { strData = strData.Substring(1); } if (strData.EndsWith(")")) { strData = strData.Substring(0, strData.Length - 1); } strData = strData.TrimStart().TrimEnd(); String[] items = strData.Split(",".ToCharArray(), StringSplitOptions.RemoveEmptyEntries); List<Coordinate> coordinates = new List<Coordinate>(); foreach (String item in items) { String[] strCoord = item.Split(" ".ToCharArray(), StringSplitOptions.RemoveEmptyEntries); double x = 0; double y = 0; if (double.TryParse(strCoord[0], System.Globalization.NumberStyles.AllowDecimalPoint, System.Globalization.CultureInfo.InvariantCulture, out x) && double.TryParse(strCoord[1], System.Globalization.NumberStyles.AllowDecimalPoint, System.Globalization.CultureInfo.InvariantCulture, out y)) { Coordinate coordinate = new Coordinate(x, y); coordinates.Add(coordinate); } } LineString lineString = new LineString(coordinates); return lineString; } else if (strData.StartsWith("MULTILINESTRING")) { strData = strData.Substring(15); strData = strData.TrimStart().TrimEnd(); if (strData.StartsWith("(")) { strData = strData.Substring(1); } if (strData.EndsWith(")")) { strData = strData.Substring(0, strData.Length - 1); } strData = strData.TrimStart().TrimEnd(); bool parse = true; List<LineString> lineStrings = new List<LineString>(); while (parse) { int indexStart = strData.IndexOf("(") + 1; int indexEnd = strData.IndexOf(")"); if (indexStart != -1 && indexEnd != -1 && indexEnd > indexStart) { String strLine = strData.Substring(indexStart, indexEnd - indexStart); strLine = strLine.TrimStart().TrimEnd(); String[] items = strLine.Split(",".ToCharArray(), StringSplitOptions.RemoveEmptyEntries); List<Coordinate> coordinates = new List<Coordinate>(); foreach (String item in items) { String[] strCoord = item.Split(" ".ToCharArray(), StringSplitOptions.RemoveEmptyEntries); double x = 0; double y = 0; if (double.TryParse(strCoord[0], System.Globalization.NumberStyles.AllowDecimalPoint, System.Globalization.CultureInfo.InvariantCulture, out x) && double.TryParse(strCoord[1], System.Globalization.NumberStyles.AllowDecimalPoint, System.Globalization.CultureInfo.InvariantCulture, out y)) { Coordinate coordinate = new Coordinate(x, y); coordinates.Add(coordinate); } } LineString lineString = new LineString(coordinates); lineStrings.Add(lineString); strData = strData.Substring(0, indexEnd); } else parse = false; } MultiLineString multiLineString = new MultiLineString(lineStrings); return multiLineString; } return null; }
/// <summary> /// Exports the selected database to fieldAttributes filegeodatabase /// </summary> /// <param name="progressBar">A progress bar object</param> /// <param name="logFunction">A log function</param> /// <param name="mdbFile">The outputShapefileName of an addressing database</param> /// <param name="approvedOnly">1 to include only approved, 0 to include all</param> /// <returns>Feature set</returns> public static FeatureSet GetRoadFeatureSetFromAdmAdrMdb(ref ToolStripProgressBar progressBar, Action <string, bool> logFunction, string mdbFile, int approvedOnly = 0) { // Setup SQL string to select all or only some streets string sqlStatement; if (approvedOnly == 1) { sqlStatement = "SELECT ADRROADID, NAMEENGLISH, NAMEARABIC, NAMEPOPULARENGLISH, NAMEPOPULARARABIC, ROADTYPE, ADRDISTRICTID, APPROVED FROM ADM_ADRROAD WHERE APPROVED = 1"; } else { sqlStatement = "SELECT ADRROADID, NAMEENGLISH, NAMEARABIC, NAMEPOPULARENGLISH, NAMEPOPULARARABIC, ROADTYPE, ADRDISTRICTID, APPROVED FROM ADM_ADRROAD"; } logFunction(sqlStatement, true); // Setup fieldAttributes dictionary to hold road names var roadNames = new Dictionary <int, DataRow>(); // Connect to database and load names using (var database = new Database(mdbFile)) { var dataTable = database.Query(sqlStatement); progressBar.Value = 0; foreach (DataRow mRow in dataTable.Rows) { int mRoadId; if (int.TryParse(mRow["ADRROADID"].ToString(), out mRoadId)) { roadNames.Add(mRoadId, mRow); } } // Connect to database using OGR to load geometries var ogrDriver = Ogr.GetDriverByName("PGeo"); if (ogrDriver == null) { return(null); } logFunction("Loaded driver", true); var ogrDataSource = ogrDriver.Open(mdbFile, 0); if (ogrDataSource == null) { return(null); } logFunction("Loaded datasource", true); var roadSegmentLayer = ogrDataSource.GetLayerByName("ADRROADSEGMENT"); if (roadSegmentLayer == null) { logFunction("Could not load layer", true); return(null); } logFunction("Loaded layer", true); // Create simplified roads var simplifiedRoadsFeatureSet = new SimplifiedRoads(); int numberOfRoads = roadNames.Count(); progressBar.Value = 0; var roadCounter = 0; var segmentCounter = 0; foreach (var roadName in roadNames) { roadCounter++; int roadIdentifier = roadName.Key; DataRow roadData = roadName.Value; roadSegmentLayer.ResetReading(); roadSegmentLayer.SetAttributeFilter("ADRROADID=" + roadIdentifier); OSGeo.OGR.Feature roadSegment; var mWkbReader = new WkbReader(); while (null != (roadSegment = roadSegmentLayer.GetNextFeature())) { segmentCounter++; OSGeo.OGR.Geometry mGeometry = roadSegment.GetGeometryRef(); if (mGeometry != null) { mGeometry.FlattenTo2D(); byte[] mWkb = new Byte[mGeometry.WkbSize()]; mGeometry.ExportToWkb(mWkb); IMultiLineString multiLine; IGeometry roadGeometry = mWkbReader.Read(mWkb); if (roadGeometry.GetType() == typeof(DotSpatial.Topology.LineString)) { var mLineStrings = new List <LineString>(); mLineStrings.Add(roadGeometry as LineString); multiLine = new DotSpatial.Topology.MultiLineString(mLineStrings); } else { multiLine = roadGeometry as IMultiLineString; } int districtIdentifier, roadClass, roadApproved; int.TryParse(roadData["ADRDISTRICTID"].ToString(), out districtIdentifier); var nameArabic = roadData["NAMEARABIC"].ToString(); var nameLatin = roadData["NAMEENGLISH"].ToString(); var namePopularArabic = roadData["NAMEPOPULARARABIC"].ToString(); var namePopularLatin = roadData["NAMEPOPULARENGLISH"].ToString(); int.TryParse(roadData["ROADTYPE"].ToString(), out roadClass); int.TryParse(roadData["APPROVED"].ToString(), out roadApproved); simplifiedRoadsFeatureSet.AddNewRow( roadGeometry, districtIdentifier, roadIdentifier, nameArabic, nameLatin, namePopularArabic, namePopularLatin, roadClass, roadApproved); } else { logFunction("No geometry", true); } if (roadCounter % 100 == 0 || roadCounter == numberOfRoads) { progressBar.ProgressBar.Value = (int)Math.Round((double)(roadCounter * 100 / numberOfRoads)); Application.DoEvents(); } } } simplifiedRoadsFeatureSet.UpdateExtent(); logFunction("Done creating featureset", true); return(simplifiedRoadsFeatureSet); } }
/// <summary> /// Get Upstream flowslines from EPA WebServices /// </summary> /// <param name="uri">Query string</param> /// <returns>Returns an IFeatureSet including the </returns> private object[] GetStreamline(string uri) { //Declare a WebClient instance to get the Watershed Delineation response string WebClient streamline = new WebClient(); try { string response = streamline.DownloadString(uri); int start = response.IndexOf("("); int end = response.IndexOf(")"); response = response.Substring(start + 1, end - 1 - start); //Declare Json Elements JObject mainObj = new JObject(); JToken outputObj; JArray lineObj = new JArray(); JToken shapeObj = new JObject(); mainObj = JObject.Parse(response); outputObj = mainObj["output"]; lineObj = outputObj["flowlines_traversed"] as JArray; List<string> comid = new List<string>(); List<string> reachcode = new List<string>(); List<string> totdist = new List<string>(); //Setup projection information _defaultProjection = KnownCoordinateSystems.Projected.World.WebMercator; //Initialize feature parameters Feature linef = new Feature(); IFeatureSet linefs = new FeatureSet(FeatureType.Line); //for (int i = 0; i < lineObj.Count; i++) foreach (JObject flowObj in lineObj) { shapeObj = flowObj["shape"] as JObject; string id = flowObj["comid"].ToString(); string code = flowObj["reachcode"].ToString(); string dist = flowObj["totaldist"].ToString(); string stype = shapeObj["type"].ToString(); JArray coordArray = shapeObj["coordinates"] as JArray; //For coordinate information string lat; string lon; //For the case GeoJSON returns a MultiLineString if (stype.Trim().ToLower() == "multilinestring") { if (coordArray != null) { LineString[] lines = new LineString[coordArray.Count]; for (int j = 0; j < coordArray.Count; j++)//The second level branket { JArray linecoord = (JArray)coordArray[j]; IList<Coordinate> multicoords = new List<Coordinate>(); if (linecoord != null) { foreach (JArray latlongcoord in linecoord) //The first level branket { Coordinate coord = new Coordinate(); lon = latlongcoord[0].ToString(); lat = latlongcoord[1].ToString(); coord.X = Convert.ToDouble(lon); coord.Y = Convert.ToDouble(lat); multicoords.Add(coord); } lines[j] = new LineString(multicoords); } } //Save lines[] into a multiline IMultiLineString multilines = new MultiLineString(lines); linef = new Feature(multilines); } } //For the case GeoJSON returns a LineString if (stype.Trim().ToLower() == "linestring") { IList<Coordinate> coords = new List<Coordinate>(); foreach (JArray latlongcoord in coordArray) //The second level branket { Coordinate coord = new Coordinate(); lon = latlongcoord[0].ToString(); lat = latlongcoord[1].ToString(); coord.X = Convert.ToDouble(lon, CultureInfo.InvariantCulture); coord.Y = Convert.ToDouble(lat, CultureInfo.InvariantCulture); coords.Add(coord); } linef = new Feature(FeatureType.Line, coords); } linefs.Projection = WGS84; //Save features into a featureset if (linefs.Features.Count == 0) { linefs.Projection = WGS84; linefs = new FeatureSet(linef.FeatureType); linefs.AddFeature(linef); } else { linefs.AddFeature(linef); } //Save streamlines' information comid.Add(id); reachcode.Add(code); totdist.Add(dist); } //TODO: PK- use a StreamLine object with 4 properties instead - create this class in the Models folder object[] streamlines = new object[4]; streamlines[0] = linefs as object; streamlines[1] = comid as object; streamlines[2] = reachcode as object; streamlines[3] = totdist as object; return streamlines; } catch (Exception ex) { var resp = new HttpResponseMessage(HttpStatusCode.NotFound) { Content = new StringContent(ex.Message), ReasonPhrase = "Error finding upstream flow lines with EPA web service." }; throw new HttpResponseException(resp); } }