public void USGS_CitiesAndTowns() { ShapeFileReader reader = new ShapeFileReader(pointFile); reader.LoadFile(); string json = reader.FeaturesAsJson(); File.WriteAllText("point.json", reader.FeatureAsJson(42)); Assert.AreEqual(6062745, json.Length); Assert.AreEqual(38187, reader.Features.Length); Assert.AreEqual(157, reader.FeatureAsJson(0).Length); Assert.AreEqual(159, reader.FeatureAsJson(38186).Length); VectorPoint[] features = reader.Features as VectorPoint[]; Assert.AreEqual(-100.06096779999996, features[0].Coordinates[0]); Assert.AreEqual(48.813056899065479, features[0].Coordinates[1]); Assert.AreEqual(-101.22071379999994, features[42].Coordinates[0]); Assert.AreEqual(48.513074399064209, features[42].Coordinates[1]); Assert.AreEqual(-149.22967529296875, features[38186].Coordinates[0]); Assert.AreEqual(61.541870116397909, features[38186].Coordinates[1]); Assert.AreEqual(5, reader.Features[0].Properties.Count); Assert.AreEqual("dunseith", reader.Features[0].Properties["nam"]); Assert.AreEqual("al020", reader.Features[0].Properties["f_code"]); Assert.AreEqual("773", reader.Features[0].Properties["pop"]); Assert.AreEqual("2010", reader.Features[0].Properties["ypc"]); Assert.AreEqual("usa", reader.Features[0].Properties["soc"]); Assert.AreEqual(reader.Features[42].Properties.Count, 5); Assert.AreEqual("glenburn", reader.Features[42].Properties["nam"]); Assert.AreEqual("al020", reader.Features[42].Properties["f_code"]); Assert.AreEqual("380", reader.Features[42].Properties["pop"]); Assert.AreEqual("2010", reader.Features[42].Properties["ypc"]); Assert.AreEqual("usa", reader.Features[42].Properties["soc"]); Assert.AreEqual(reader.Features[38186].Properties.Count, 5); Assert.AreEqual("matanuska", reader.Features[38186].Properties["nam"]); Assert.AreEqual("al020", reader.Features[38186].Properties["f_code"]); Assert.AreEqual("-999", reader.Features[38186].Properties["pop"]); Assert.AreEqual("2010", reader.Features[38186].Properties["ypc"]); Assert.AreEqual("usa", reader.Features[38186].Properties["soc"]); }
public void Tiger_US_PlaceBoundaries() { ShapeFileReader reader = new ShapeFileReader(polyFile); reader.LoadFile(); string json = reader.FeaturesAsJson(); File.WriteAllText("polygon.json", reader.FeatureAsJson(42)); Assert.AreEqual(25301992, json.Length); Assert.AreEqual(1516, reader.Features.Length); Assert.AreEqual(4780, reader.FeatureAsJson(0).Length); Assert.AreEqual(13137, reader.FeatureAsJson(1515).Length); VectorShape[] features = reader.Features as VectorShape[]; Assert.AreEqual(-118.456008, features[0].Coordinates[0][0].Value[0]); Assert.AreEqual(34.284903, features[0].Coordinates[0][0].Value[1]); Assert.AreEqual(-118.30807, features[42].Coordinates[0][0].Value[0]); Assert.AreEqual(34.161224, features[42].Coordinates[0][0].Value[1]); Assert.AreEqual(-122.060783, features[1515].Coordinates[0][0].Value[0]); Assert.AreEqual(37.05574, features[1515].Coordinates[0][0].Value[1]); Assert.AreEqual(16, reader.Features[0].Properties.Count); Assert.AreEqual("06", reader.Features[42].Properties["statefp"]); Assert.AreEqual("30000", reader.Features[42].Properties["placefp"]); Assert.AreEqual("02410597", reader.Features[42].Properties["placens"]); Assert.AreEqual("0630000", reader.Features[42].Properties["geoid"]); Assert.AreEqual("glendale", reader.Features[42].Properties["name"]); Assert.AreEqual("glendale city", reader.Features[42].Properties["namelsad"]); Assert.AreEqual("25", reader.Features[42].Properties["lsad"]); Assert.AreEqual("c1", reader.Features[42].Properties["classfp"]); Assert.AreEqual("y", reader.Features[42].Properties["pcicbsa"]); Assert.AreEqual("n", reader.Features[42].Properties["pcinecta"]); Assert.AreEqual("g4110", reader.Features[42].Properties["mtfcc"]); Assert.AreEqual("a", reader.Features[42].Properties["funcstat"]); Assert.AreEqual("78848571", reader.Features[42].Properties["aland"]); Assert.AreEqual("337673", reader.Features[42].Properties["awater"]); Assert.AreEqual("+34.1813929", reader.Features[42].Properties["intptlat"]); Assert.AreEqual("-118.2458301", reader.Features[42].Properties["intptlon"]); }
public void Tiger_CA_SanFran_Roads() { ShapeFileReader reader = new ShapeFileReader(lineFile); reader.LoadFile(); string json = reader.FeaturesAsJson(); File.WriteAllText("line.json", reader.FeatureAsJson(42)); Assert.AreEqual(2166881, json.Length); Assert.AreEqual(4589, reader.Features.Length); Assert.AreEqual(283, reader.FeatureAsJson(0).Length); Assert.AreEqual(649, reader.FeatureAsJson(4588).Length); VectorShape[] features = reader.Features as VectorShape[]; Assert.AreEqual(-122.480706, features[0].Coordinates[0][0].Value[0]); Assert.AreEqual(37.792316, features[0].Coordinates[0][0].Value[1]); Assert.AreEqual(-122.469023, features[42].Coordinates[0][0].Value[0]); Assert.AreEqual(37.737949, features[42].Coordinates[0][0].Value[1]); Assert.AreEqual(-122.364304, features[4588].Coordinates[0][0].Value[0]); Assert.AreEqual(37.819468, features[4588].Coordinates[0][0].Value[1]); Assert.AreEqual(4, reader.Features[0].Properties.Count); Assert.AreEqual("110498938555", reader.Features[0].Properties["linearid"]); Assert.AreEqual("n van horn ln", reader.Features[0].Properties["fullname"]); Assert.AreEqual("m", reader.Features[0].Properties["rttyp"]); Assert.AreEqual("s1400", reader.Features[0].Properties["mtfcc"]); Assert.AreEqual("110498935016", reader.Features[42].Properties["linearid"]); Assert.AreEqual("w portal ave", reader.Features[42].Properties["fullname"]); Assert.AreEqual("m", reader.Features[42].Properties["rttyp"]); Assert.AreEqual("s1400", reader.Features[42].Properties["mtfcc"]); Assert.AreEqual("110498933806", reader.Features[4588].Properties["linearid"]); Assert.AreEqual("avenue n", reader.Features[4588].Properties["fullname"]); Assert.AreEqual("m", reader.Features[4588].Properties["rttyp"]); Assert.AreEqual("s1400", reader.Features[4588].Properties["mtfcc"]); }
/// <summary> /// This is the method that actually does the work. /// </summary> /// <param name="DA">The DA object is used to retrieve from inputs and store in outputs.</param> protected override void SolveInstance(IGH_DataAccess DA) { //retrive inputs string filePath = ""; if (!DA.GetData(0, ref filePath)) { return; } bool readFile = false; if (!DA.GetData(1, ref readFile)) { return; } //JObject geoJsonData = null; JArray jsonObj = null; if (readFile) { //file = "filePath Harlow.ShapeFileReader harlowShpReader = new Harlow.ShapeFileReader(filePath); harlowShpReader.LoadFile(); string shpJsonString = harlowShpReader.FeaturesAsJson(); //string shpjsonString = harlowShpReader.FeatureAsJson() //System.IO.File.ReadAllText(filePath,) //geoJsonData = JObject.Parse(shpJsonString); jsonObj = JArray.Parse(shpJsonString); JObject geoJsonData = new JObject(); geoJsonData.Add("features", jsonObj); //var json = Newtonsoft.Json.JsonConvert.SerializeObject(geoJsonData, Newtonsoft.Json.Formatting.Indented); //DA.SetData(0, json); //read features JArray features = (JArray)geoJsonData["features"]; GH_Structure <GH_String> attributes = new GH_Structure <GH_String>(); GH_Structure <GH_Point> featureGeometry = new GH_Structure <GH_Point>(); int featureIndex = 0; foreach (JObject feature in features) { GH_Path currentPath = new GH_Path(featureIndex); foreach (var attr in (JObject)feature["properties"]) { JToken attributeToken = attr.Value; string thisAttribute = (string)attributeToken; GH_String thisGhAttribute = new GH_String(thisAttribute); attributes.Append(thisGhAttribute, currentPath); } int pathIndex = 0; foreach (JArray pathsArray in (JArray)feature["coordinates"]) { List <GH_Point> thisPathPoints = new List <GH_Point>(); foreach (var path in pathsArray) { Point3d thisPoint = new Point3d((double)path[0], (double)path[1], 0); GH_Point thisGhPoint = new GH_Point(thisPoint); thisPathPoints.Add(thisGhPoint); } GH_Path thisPath = new GH_Path(featureIndex, pathIndex); featureGeometry.AppendRange(thisPathPoints, thisPath); pathIndex++; } featureIndex++; }//end polyline DA.SetDataTree(1, attributes); DA.SetDataTree(2, featureGeometry); /// ///set attributes List <string> featureFields = new List <string>(); JToken fieldObjs = geoJsonData["features"][0]["properties"]; foreach (JProperty prop in fieldObjs) { string thisField = (string)prop.Name; featureFields.Add(thisField); } DA.SetDataList(0, featureFields); }//end if read file }