////////////////////////////////////////////////////// ////////////////////////////////////////////////////// ///Converting GDAL geometry types to Rhino/GH geometry types ///Rhino transform for each method is required in order to scale points first, then mesh or make polyline or whatever. ///Without doing this, we get floating point distortions in the geometry, especially meshes. public static Point3d OgrPointToPoint3d(OSGeo.OGR.Geometry ogrPoint, Transform transform) { Point3d pt3d = new Point3d(ogrPoint.GetX(0), ogrPoint.GetY(0), ogrPoint.GetZ(0)); pt3d.Transform(transform); return(pt3d); }
////////////////////////////////////////////////////// ////////////////////////////////////////////////////// ///Converting GDAL geometry types to Rhino/GH geometry types public static Point3d OgrPointToPoint3d(OSGeo.OGR.Geometry ogrPoint) { Point3d pt3d = new Point3d(ogrPoint.GetX(0), ogrPoint.GetY(0), ogrPoint.GetZ(0)); ///convert should happen in the component or elsewhere, not here. point should come in pre-converted to SRS //return Convert.WGSToXYZ(pt3d); return(pt3d); }
/************************************* 点角式 *****************************************/ /// <summary> /// 清理等后生成新的文件 /// /// </summary> public static void claenPoint(string filePath, string savefile, double jiaodu, int cishu) { OSGeo.OGR.Ogr.RegisterAll(); OSGeo.OGR.Driver dr = OSGeo.OGR.Ogr.GetDriverByName("ESRI shapefile"); //进来的Layer OSGeo.OGR.DataSource oriDs = dr.Open(filePath, 0); OSGeo.OGR.Layer oriLayer = oriDs.GetLayerByIndex(0); //出去的Layer if (System.IO.File.Exists(savefile)) { savefile.deleteFiles(); } OSGeo.OGR.DataSource targDs = dr.CreateDataSource(savefile, null); OSGeo.OGR.Layer targLayer = targDs.CreateLayer("targ", null, OSGeo.OGR.wkbGeometryType.wkbPolygon, null); int featCount = oriLayer.GetFeatureCount(0); for (int i = 0; i < featCount; i++) { ///进来的Featuer OSGeo.OGR.Feature oriFeat = oriLayer.GetFeature(i); ///把一个Featuer转为点数组 OSGeo.OGR.Geometry oriGeom = oriFeat.GetGeometryRef(); OSGeo.OGR.Geometry subGeom = oriGeom.GetGeometryRef(0); int pointCount = subGeom.GetPointCount(); Point[] aFeat = new Point[pointCount]; for (int c = 0; c < pointCount; c++) { aFeat[c].X = subGeom.GetX(c); aFeat[c].Y = subGeom.GetY(c); aFeat[c].Z = subGeom.GetZ(c); } ///调选点方法,得到一个新的Featuer Geometry newGeom = null; if (aFeat.Length > cishu * 3) { newGeom = JID(aFeat, jiaodu, cishu); } else { oriFeat.Dispose(); continue; } if (newGeom != null) { Feature temp = new Feature(new FeatureDefn("")); temp.SetGeometry(newGeom); targLayer.CreateFeature(temp); temp.Dispose(); } oriFeat.Dispose(); Console.WriteLine("已完成{0}/{1}", i, featCount); } oriDs.Dispose(); targDs.Dispose(); }
public static Curve OgrLinestringToCurve(OSGeo.OGR.Geometry linestring, Transform transform) { List <Point3d> ptList = new List <Point3d>(); for (int i = 0; i < linestring.GetPointCount(); i++) { Point3d pt = new Point3d(linestring.GetX(i), linestring.GetY(i), linestring.GetZ(i)); pt.Transform(transform); ptList.Add(pt); } Polyline pL = new Polyline(ptList); return(pL.ToNurbsCurve()); }
/// <summary> /// 清理原文件 /// </summary> /// <param name="filePath"></param> public static void claenPoint(string filePath, double jiaodu, int cishu) { OSGeo.OGR.Ogr.RegisterAll(); OSGeo.OGR.Driver dr = OSGeo.OGR.Ogr.GetDriverByName("ESRI shapefile"); OSGeo.OGR.DataSource oriDs = dr.Open(filePath, 1); OSGeo.OGR.Layer oriLayer = oriDs.GetLayerByIndex(0); int featCount = oriLayer.GetFeatureCount(0); for (int i = 0; i < featCount; i++) { OSGeo.OGR.Feature oriFeat = oriLayer.GetFeature(i); OSGeo.OGR.Geometry oriGeom = oriFeat.GetGeometryRef(); OSGeo.OGR.Geometry subGeom = oriGeom.GetGeometryRef(0); int pointCount = subGeom.GetPointCount(); Point[] aFeat = new Point[pointCount]; for (int c = 0; c < pointCount; c++) { aFeat[c].X = subGeom.GetX(c); aFeat[c].Y = subGeom.GetY(c); aFeat[c].Z = subGeom.GetZ(c); } OSGeo.OGR.Geometry newGeom = null; if (aFeat.Length > cishu * 3) { newGeom = JID(aFeat, jiaodu, cishu); } else { oriFeat.Dispose(); continue; } if (newGeom != null) { oriFeat.SetGeometry(newGeom); oriLayer.SetFeature(oriFeat); } Console.WriteLine("cleanPoint已完成{0}/{1}", i, featCount); oriFeat.Dispose(); } oriDs.Dispose(); }
public static Curve OgrRingToCurve(OSGeo.OGR.Geometry ring, Transform transform) { double tol = Rhino.RhinoDoc.ActiveDoc.ModelAbsoluteTolerance; List <Point3d> ptList = new List <Point3d>(); for (int i = 0; i < ring.GetPointCount(); i++) { Point3d pt = new Point3d(ring.GetX(i), ring.GetY(i), ring.GetZ(i)); pt.Transform(transform); ptList.Add(pt); } //ptList.Add(ptList[0]); Polyline pL = new Polyline(ptList); Curve crv = pL.ToNurbsCurve(); //crv.MakeClosed(tol); return(crv); }
public static List <int> _平角点ID集(Feature aFeatuer, double userSet, int seleTime) { OSGeo.OGR.Geometry oriGeom = aFeatuer.GetGeometryRef(); OSGeo.OGR.Geometry subGeom = oriGeom.GetGeometryRef(0); int pointCount = subGeom.GetPointCount(); Point[] pointList = new Point[pointCount]; for (int c = 0; c < pointCount; c++) { pointList[c].X = subGeom.GetX(c); pointList[c].Y = subGeom.GetY(c); pointList[c].Z = subGeom.GetZ(c); } if (pointList.Length < seleTime * 3) { MessageBox.Show("这个图形点数小于重复次数~试试降低重复次数~或删除"); return(null); } List <int> ids = new List <int>(); for (int i = 0; i < pointList.Length; i++) { int frontId, thisId, backId; bool[] yon = new bool[seleTime]; for (int t = 1; t <= seleTime; t++) { frontId = i < t ? pointList.Length - 1 + i - t : i - t; thisId = i; backId = i > pointList.Length - 1 - t ? i - (pointList.Length - 1) + t : i + t; double jiaodu = cosCalculator(pointList[frontId], pointList[thisId], pointList[backId]); yon[t - 1] = jiaodu > userSet; } if (yon.Contains(true)) { ids.Add(i); } } return(ids); }
/// <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) { ///GDAL setup RESTful.GdalConfiguration.ConfigureOgr(); ///Working with data trees allows us to only call the osr coordinate transformation once, which seems to be expensive GH_Structure <GH_Point> sourcePoints = new GH_Structure <GH_Point>(); DA.GetDataTree(0, out sourcePoints); GH_Structure <GH_Point> destPoints = new GH_Structure <GH_Point>(); string sourceString = string.Empty; DA.GetData(1, ref sourceString); OSGeo.OSR.SpatialReference sourceSRS = new OSGeo.OSR.SpatialReference(""); sourceSRS.SetFromUserInput(sourceString); if (sourceSRS.Validate() == 1) { AddRuntimeMessage(GH_RuntimeMessageLevel.Error, "Invalid Source SRS."); return; } string destString = string.Empty; DA.GetData(2, ref destString); OSGeo.OSR.SpatialReference destSRS = new OSGeo.OSR.SpatialReference(""); destSRS.SetFromUserInput(destString); if (destSRS.Validate() == 1) { AddRuntimeMessage(GH_RuntimeMessageLevel.Error, "Invalid Destination SRS."); return; } OSGeo.OSR.CoordinateTransformation trans = new OSGeo.OSR.CoordinateTransformation(sourceSRS, destSRS); foreach (var path in sourcePoints.Paths) { List <GH_Point> branchPts = (List <GH_Point>)sourcePoints.get_Branch(path); foreach (var sp in branchPts) { OSGeo.OGR.Geometry destOgrPoint = new OSGeo.OGR.Geometry(wkbGeometryType.wkbPoint); destOgrPoint.AddPoint(sp.Value.X, sp.Value.Y, sp.Value.Z); destOgrPoint.AssignSpatialReference(sourceSRS); destOgrPoint.Transform(trans); Point3d destPoint = new Point3d(destOgrPoint.GetX(0), destOgrPoint.GetY(0), destOgrPoint.GetZ(0)); destPoints.Append(new GH_Point(destPoint), path); destOgrPoint.Dispose(); } } DA.SetDataTree(0, destPoints); }
public static Curve OgrLinestringToCurve(OSGeo.OGR.Geometry linestring) { List <Point3d> ptList = new List <Point3d>(); for (int i = 0; i < linestring.GetPointCount(); i++) { ptList.Add(Convert.ToXYZ(new Point3d(linestring.GetX(i), linestring.GetY(i), linestring.GetZ(i)))); } Polyline pL = new Polyline(ptList); return(new Polyline(ptList).ToNurbsCurve()); }
////////////////////////////////////////////////////// ////////////////////////////////////////////////////// ///Converting GDAL geometry types to Rhino/GH geometry types public static Point3d OgrPointToPoint3d(OSGeo.OGR.Geometry ogrPoint) { Point3d pt3d = new Point3d(ogrPoint.GetX(0), ogrPoint.GetY(0), ogrPoint.GetZ(0)); return(Convert.ToXYZ(pt3d)); }
protected override void SolveInstance(IGH_DataAccess DA) { List <Curve> boundary = new List <Curve>(); DA.GetDataList <Curve>(0, boundary); string shpFileLoc = ""; DA.GetData <string>("Shapefile Location", ref shpFileLoc); ////int SRef = 3857; GdalConfiguration.ConfigureOgr(); GdalConfiguration.ConfigureGdal(); OSGeo.OGR.Driver drv = OSGeo.OGR.Ogr.GetDriverByName("ESRI Shapefile"); OSGeo.OGR.DataSource ds = OSGeo.OGR.Ogr.Open(shpFileLoc, 0); List <OSGeo.OGR.Layer> layerset = new List <OSGeo.OGR.Layer>(); List <int> fc = new List <int>(); for (int iLayer = 0; iLayer < ds.GetLayerCount(); iLayer++) { OSGeo.OGR.Layer layer = ds.GetLayerByIndex(iLayer); if (layer == null) { Console.WriteLine("FAILURE: Couldn't fetch advertised layer " + iLayer); System.Environment.Exit(-1); } long count = layer.GetFeatureCount(1); int featureCount = System.Convert.ToInt32(count); fc.Add(featureCount); layerset.Add(layer); } //Get OGR envelope of Shapefile OSGeo.OGR.Envelope ext = new OSGeo.OGR.Envelope(); layerset[0].GetExtent(ext, 1); Point3d extMin = new Point3d(); Point3d extMax = new Point3d(); extMin.X = ext.MinX; extMin.Y = ext.MinY; extMax.X = ext.MaxX; extMax.Y = ext.MaxY; OSGeo.OSR.SpatialReference sr = layerset[0].GetSpatialRef(); OSGeo.OSR.SpatialReference dst = new OSGeo.OSR.SpatialReference(""); dst.SetWellKnownGeogCS("WGS84"); //Get the spatial refernce of the input Shapefile string sRef; sr.ExportToWkt(out sRef); OSGeo.OSR.CoordinateTransformation coordTransform = new OSGeo.OSR.CoordinateTransformation(sr, dst); OSGeo.OSR.CoordinateTransformation revTransform = new OSGeo.OSR.CoordinateTransformation(dst, sr); //Get bounding box of data in Shapefile double[] extMinPT = new double[3] { extMin.X, extMin.Y, extMin.Z }; double[] extMaxPT = new double[3] { extMax.X, extMax.Y, extMax.Z }; coordTransform.TransformPoint(extMinPT); coordTransform.TransformPoint(extMaxPT); Point3d extPTmin = new Point3d(extMinPT[0], extMinPT[1], extMinPT[2]); Point3d extPTmax = new Point3d(extMaxPT[0], extMaxPT[1], extMaxPT[2]); Rectangle3d rec = new Rectangle3d(Plane.WorldXY, Heron.Convert.ToXYZ(extPTmin), Heron.Convert.ToXYZ(extPTmax)); //Declare trees GH_Structure <GH_String> fset = new GH_Structure <GH_String>(); GH_Structure <GH_Point> gset = new GH_Structure <GH_Point>(); GH_Structure <GH_String> layname = new GH_Structure <GH_String>(); OSGeo.OGR.FeatureDefn def = layerset[0].GetLayerDefn(); //Loop through input boundaries for (int i = 0; i < boundary.Count; i++) { if (rec.BoundingBox.Contains(boundary[i].GetBoundingBox(true).Min) && (rec.BoundingBox.Contains(boundary[i].GetBoundingBox(true).Max))) { //Create bounding box for clipping geometry Point3d min = Heron.Convert.ToWGS(boundary[i].GetBoundingBox(true).Min); Point3d max = Heron.Convert.ToWGS(boundary[i].GetBoundingBox(true).Max); double[] minpT = new double[3]; double[] maxpT = new double[3]; minpT[0] = min.X; minpT[1] = min.Y; minpT[2] = min.Z; maxpT[0] = max.X; maxpT[1] = max.Y; maxpT[2] = max.Z; revTransform.TransformPoint(minpT); revTransform.TransformPoint(maxpT); OSGeo.OGR.Geometry bbox = OSGeo.OGR.Geometry.CreateFromWkt("POLYGON((" + min.X + " " + min.Y + ", " + min.X + " " + max.Y + ", " + max.X + " " + max.Y + ", " + max.X + " " + min.Y + ", " + min.X + " " + min.Y + "))"); OSGeo.OGR.Geometry ebbox = OSGeo.OGR.Geometry.CreateFromWkt("POLYGON((" + minpT[0] + " " + minpT[1] + ", " + minpT[0] + " " + maxpT[1] + ", " + maxpT[0] + " " + maxpT[1] + ", " + maxpT[0] + " " + minpT[1] + ", " + minpT[0] + " " + minpT[1] + "))"); //Clip Shapefile //http://pcjericks.github.io/py-gdalogr-cookbook/vector_layers.html OSGeo.OGR.Layer clipped_layer = layerset[0]; clipped_layer.SetSpatialFilter(ebbox); //Loop through geometry OSGeo.OGR.Feature feat; def = clipped_layer.GetLayerDefn(); int m = 0; while ((feat = layerset[0].GetNextFeature()) != null) { if (feat.GetGeometryRef() != null) { //Get geometry points and field values OSGeo.OGR.Geometry geom = feat.GetGeometryRef(); OSGeo.OGR.Geometry sub_geom; //Start get points if open polylines and points for (int gpc = 0; gpc < geom.GetPointCount(); gpc++) { //Loop through geometry points double[] pT = new double[3]; pT[0] = geom.GetX(gpc); pT[1] = geom.GetY(gpc); pT[2] = geom.GetZ(gpc); if (Double.IsNaN(geom.GetZ(gpc))) { pT[2] = 0; } coordTransform.TransformPoint(pT); Point3d pt3D = new Point3d(); pt3D.X = pT[0]; pt3D.Y = pT[1]; pt3D.Z = pT[2]; gset.Append(new GH_Point(Heron.Convert.ToXYZ(pt3D)), new GH_Path(i, m)); //End loop through geometry points // Get Feature Values if (fset.PathExists(new GH_Path(i, m))) { fset.get_Branch(new GH_Path(i, m)).Clear(); } for (int iField = 0; iField < feat.GetFieldCount(); iField++) { OSGeo.OGR.FieldDefn fdef = def.GetFieldDefn(iField); if (feat.IsFieldSet(iField)) { fset.Append(new GH_String(feat.GetFieldAsString(iField)), new GH_Path(i, m)); } else { fset.Append(new GH_String("null"), new GH_Path(i, m)); } } //End Get Feature Values } //End getting points if open polylines or points //Start getting points if closed polylines and multipolygons for (int gi = 0; gi < geom.GetGeometryCount(); gi++) { sub_geom = geom.GetGeometryRef(gi); List <Point3d> geom_list = new List <Point3d>(); for (int ptnum = 0; ptnum < sub_geom.GetPointCount(); ptnum++) { //Loop through geometry points double[] pT = new double[3]; pT[0] = sub_geom.GetX(ptnum); pT[1] = sub_geom.GetY(ptnum); pT[2] = sub_geom.GetZ(ptnum); coordTransform.TransformPoint(pT); Point3d pt3D = new Point3d(); pt3D.X = pT[0]; pt3D.Y = pT[1]; pt3D.Z = pT[2]; gset.Append(new GH_Point(Heron.Convert.ToXYZ(pt3D)), new GH_Path(i, m, gi)); //End loop through geometry points // Get Feature Values if (fset.PathExists(new GH_Path(i, m))) { fset.get_Branch(new GH_Path(i, m)).Clear(); } for (int iField = 0; iField < feat.GetFieldCount(); iField++) { OSGeo.OGR.FieldDefn fdef = def.GetFieldDefn(iField); if (feat.IsFieldSet(iField)) { fset.Append(new GH_String(feat.GetFieldAsString(iField)), new GH_Path(i, m)); } else { fset.Append(new GH_String("null"), new GH_Path(i, m)); } } //End Get Feature Values } //End getting points from closed polylines } m++; } feat.Dispose(); } } else { AddRuntimeMessage(GH_RuntimeMessageLevel.Warning, "One or more boundaries may be outside the bounds of the Shapefile dataset."); //return; } } //Get the field names List <string> fieldnames = new List <string>(); for (int iAttr = 0; iAttr < def.GetFieldCount(); iAttr++) { OSGeo.OGR.FieldDefn fdef = def.GetFieldDefn(iAttr); fieldnames.Add(fdef.GetNameRef()); } DA.SetData(0, def.GetName()); DA.SetDataList(1, fc); DA.SetData(2, rec); DA.SetData(3, sRef); DA.SetDataList(4, fieldnames); DA.SetDataTree(5, fset); DA.SetDataTree(6, gset); }
protected override void SolveInstance(IGH_DataAccess DA) { List <Curve> boundary = new List <Curve>(); DA.GetDataList <Curve>(0, boundary); string shpFileLoc = ""; DA.GetData <string>("Vector Data Location", ref shpFileLoc); ///GDAL setup ///Some preliminary testing has been done to read SHP, GeoJSON, OSM, KML, MVT, GML and GDB ///It can be spotty with KML, MVT and GML and doesn't throw informative errors. Likely has to do with getting a valid CRS and ///TODO: resolve errors with reading KML, MVT, GML. RESTful.GdalConfiguration.ConfigureOgr(); OSGeo.OGR.Ogr.RegisterAll(); OSGeo.OGR.Driver drv = OSGeo.OGR.Ogr.GetDriverByName("ESRI Shapefile"); OSGeo.OGR.DataSource ds = OSGeo.OGR.Ogr.Open(shpFileLoc, 0); if (ds == null) { AddRuntimeMessage(GH_RuntimeMessageLevel.Error, "The vector datasource was unreadable by this component. It may not a valid file type for this component or otherwise null/empty."); return; } List <OSGeo.OGR.Layer> layerset = new List <OSGeo.OGR.Layer>(); List <int> fc = new List <int>(); for (int iLayer = 0; iLayer < ds.GetLayerCount(); iLayer++) { OSGeo.OGR.Layer layer = ds.GetLayerByIndex(iLayer); if (layer == null) { Console.WriteLine("Couldn't fetch advertised layer " + iLayer); System.Environment.Exit(-1); } else { layerset.Add(layer); } } ///Declare trees GH_Structure <GH_String> layname = new GH_Structure <GH_String>(); GH_Structure <GH_Integer> fcs = new GH_Structure <GH_Integer>(); GH_Structure <GH_Rectangle> recs = new GH_Structure <GH_Rectangle>(); GH_Structure <GH_String> sRefs = new GH_Structure <GH_String>(); GH_Structure <GH_String> fnames = new GH_Structure <GH_String>(); GH_Structure <GH_String> fset = new GH_Structure <GH_String>(); GH_Structure <GH_Point> gset = new GH_Structure <GH_Point>(); ///Loop through each layer. Layers usually occur in Geodatabase GDB format. SHP usually has only one layer. for (int iLayer = 0; iLayer < ds.GetLayerCount(); iLayer++) { OSGeo.OGR.Layer layer = ds.GetLayerByIndex(iLayer); if (layer == null) { Console.WriteLine("Couldn't fetch advertised layer " + iLayer); System.Environment.Exit(-1); } long count = layer.GetFeatureCount(1); int featureCount = System.Convert.ToInt32(count); fcs.Append(new GH_Integer(featureCount), new GH_Path(iLayer)); layname.Append(new GH_String(layer.GetName()), new GH_Path(iLayer)); ///Get OGR envelope of the data in the layer OSGeo.OGR.Envelope ext = new OSGeo.OGR.Envelope(); layer.GetExtent(ext, 1); Point3d extMin = new Point3d(); Point3d extMax = new Point3d(); extMin.X = ext.MinX; extMin.Y = ext.MinY; extMax.X = ext.MaxX; extMax.Y = ext.MaxY; ///Get the spatial reference of the input vector file and set to WGS84 if not known OSGeo.OSR.SpatialReference sr = new SpatialReference(Osr.SRS_WKT_WGS84); string sRef = ""; if (layer.GetSpatialRef() == null) { AddRuntimeMessage(GH_RuntimeMessageLevel.Remark, "Coordinate Reference System (CRS) is missing. CRS set automatically set to WGS84."); sr.ImportFromXML(shpFileLoc); string pretty = ""; sr.ExportToPrettyWkt(out pretty, 0); AddRuntimeMessage(GH_RuntimeMessageLevel.Remark, pretty); sr.SetWellKnownGeogCS("WGS84"); sRef = "Coordinate Reference System (CRS) is missing. CRS set automatically set to WGS84."; //sr.ImportFromEPSG(2263); } else { if (layer.GetSpatialRef().Validate() != 0) { AddRuntimeMessage(GH_RuntimeMessageLevel.Remark, "Coordinate Reference System (CRS) is unknown or unsupported. CRS set automatically set to WGS84."); sr.SetWellKnownGeogCS("WGS84"); sRef = "Coordinate Reference System (CRS) is unknown or unsupported. SRS set automatically set to WGS84."; } else { AddRuntimeMessage(GH_RuntimeMessageLevel.Remark, "Coordinate Reference System (CRS) set from layer" + iLayer + "."); sr = layer.GetSpatialRef(); sr.ExportToWkt(out sRef); } } sRefs.Append(new GH_String(sRef), new GH_Path(iLayer)); ///Set transform from input spatial reference to Rhino spatial reference ///TODO: look into adding a step for transforming to CRS set in SetCRS OSGeo.OSR.SpatialReference dst = new OSGeo.OSR.SpatialReference(""); dst.SetWellKnownGeogCS("WGS84"); OSGeo.OSR.CoordinateTransformation coordTransform = new OSGeo.OSR.CoordinateTransformation(sr, dst); OSGeo.OSR.CoordinateTransformation revTransform = new OSGeo.OSR.CoordinateTransformation(dst, sr); ///Get bounding box of data in layer double[] extMinPT = new double[3] { extMin.X, extMin.Y, extMin.Z }; double[] extMaxPT = new double[3] { extMax.X, extMax.Y, extMax.Z }; coordTransform.TransformPoint(extMinPT); coordTransform.TransformPoint(extMaxPT); Point3d extPTmin = new Point3d(extMinPT[0], extMinPT[1], extMinPT[2]); Point3d extPTmax = new Point3d(extMaxPT[0], extMaxPT[1], extMaxPT[2]); Rectangle3d rec = new Rectangle3d(Plane.WorldXY, Heron.Convert.ToXYZ(extPTmin), Heron.Convert.ToXYZ(extPTmax)); recs.Append(new GH_Rectangle(rec), new GH_Path(iLayer)); ///Loop through input boundaries for (int i = 0; i < boundary.Count; i++) { OSGeo.OGR.FeatureDefn def = layer.GetLayerDefn(); ///Get the field names List <string> fieldnames = new List <string>(); for (int iAttr = 0; iAttr < def.GetFieldCount(); iAttr++) { OSGeo.OGR.FieldDefn fdef = def.GetFieldDefn(iAttr); fnames.Append(new GH_String(fdef.GetNameRef()), new GH_Path(i, iLayer)); } ///Check if boundary is contained in extent if (!rec.IsValid || ((rec.Height == 0) && (rec.Width == 0))) { AddRuntimeMessage(GH_RuntimeMessageLevel.Warning, "One or more vector datasource bounds are not valid."); OSGeo.OGR.Feature feat; int m = 0; while ((feat = layer.GetNextFeature()) != null) { ///Loop through field values for (int iField = 0; iField < feat.GetFieldCount(); iField++) { OSGeo.OGR.FieldDefn fdef = def.GetFieldDefn(iField); fset.Append(new GH_String(feat.GetFieldAsString(iField)), new GH_Path(i, iLayer, m)); fdef.Dispose(); } m++; feat.Dispose(); } } else if (boundary[i] == null) { AddRuntimeMessage(GH_RuntimeMessageLevel.Warning, "Clipping boundary " + i + " not set."); } else if (!boundary[i].IsValid) { AddRuntimeMessage(GH_RuntimeMessageLevel.Warning, "Clipping boundary " + i + " is not valid."); } else if (rec.IsValid && Curve.PlanarClosedCurveRelationship(rec.ToNurbsCurve(), boundary[i], Plane.WorldXY, Rhino.RhinoDoc.ActiveDoc.ModelAbsoluteTolerance) == RegionContainment.Disjoint) { AddRuntimeMessage(GH_RuntimeMessageLevel.Warning, "One or more boundaries may be outside the bounds of the vector datasource."); } else { ///Create bounding box for clipping geometry Point3d min = Heron.Convert.ToWGS(boundary[i].GetBoundingBox(true).Min); Point3d max = Heron.Convert.ToWGS(boundary[i].GetBoundingBox(true).Max); double[] minpT = new double[3]; double[] maxpT = new double[3]; minpT[0] = min.X; minpT[1] = min.Y; minpT[2] = min.Z; maxpT[0] = max.X; maxpT[1] = max.Y; maxpT[2] = max.Z; revTransform.TransformPoint(minpT); revTransform.TransformPoint(maxpT); ///Convert to OGR geometry ///TODO: add conversion from GH geometry to OGR to Convert class OSGeo.OGR.Geometry ebbox = OSGeo.OGR.Geometry.CreateFromWkt("POLYGON((" + minpT[0] + " " + minpT[1] + ", " + minpT[0] + " " + maxpT[1] + ", " + maxpT[0] + " " + maxpT[1] + ", " + maxpT[0] + " " + minpT[1] + ", " + minpT[0] + " " + minpT[1] + "))"); ///Clip Shapefile ///http://pcjericks.github.io/py-gdalogr-cookbook/vector_layers.html ///TODO: allow for polyline/curve as clipper, not just bounding box OSGeo.OGR.Layer clipped_layer = layer; clipped_layer.SetSpatialFilter(ebbox); ///Loop through geometry OSGeo.OGR.Feature feat; def = clipped_layer.GetLayerDefn(); int m = 0; while ((feat = clipped_layer.GetNextFeature()) != null) { OSGeo.OGR.Geometry geom = feat.GetGeometryRef(); OSGeo.OGR.Geometry sub_geom; ///reproject geometry to WGS84 ///TODO: look into using the SetCRS global variable here geom.Transform(coordTransform); if (feat.GetGeometryRef() != null) { ///Start get points if open polylines and points for (int gpc = 0; gpc < geom.GetPointCount(); gpc++) { ///Loop through geometry points double[] pT = new double[3]; pT[0] = geom.GetX(gpc); pT[1] = geom.GetY(gpc); pT[2] = geom.GetZ(gpc); if (Double.IsNaN(geom.GetZ(gpc))) { pT[2] = 0; } //coordTransform.TransformPoint(pT); Point3d pt3D = new Point3d(); pt3D.X = pT[0]; pt3D.Y = pT[1]; pt3D.Z = pT[2]; gset.Append(new GH_Point(Heron.Convert.ToXYZ(pt3D)), new GH_Path(i, iLayer, m)); ///End loop through geometry points /// Get Feature Values if (fset.PathExists(new GH_Path(i, iLayer, m))) { fset.get_Branch(new GH_Path(i, iLayer, m)).Clear(); } for (int iField = 0; iField < feat.GetFieldCount(); iField++) { OSGeo.OGR.FieldDefn fdef = def.GetFieldDefn(iField); if (feat.IsFieldSet(iField)) { fset.Append(new GH_String(feat.GetFieldAsString(iField)), new GH_Path(i, iLayer, m)); } else { fset.Append(new GH_String("null"), new GH_Path(i, iLayer, m)); } } ///End Get Feature Values } ///End getting points if open polylines or points ///Start getting points if closed polylines and multipolygons for (int gi = 0; gi < geom.GetGeometryCount(); gi++) { sub_geom = geom.GetGeometryRef(gi); OSGeo.OGR.Geometry subsub_geom; List <Point3d> geom_list = new List <Point3d>(); ///trouble getting all points. this is a troubleshoot ///gset.Append(new GH_Point(new Point3d(0, 0, sub_geom.GetGeometryCount())), new GH_Path(i, iLayer, m, gi)); if (sub_geom.GetGeometryCount() > 0) { for (int n = 0; n < sub_geom.GetGeometryCount(); n++) { subsub_geom = sub_geom.GetGeometryRef(n); for (int ptnum = 0; ptnum < subsub_geom.GetPointCount(); ptnum++) { ///Loop through geometry points double[] pT = new double[3]; pT[0] = subsub_geom.GetX(ptnum); pT[1] = subsub_geom.GetY(ptnum); pT[2] = subsub_geom.GetZ(ptnum); Point3d pt3D = new Point3d(); pt3D.X = pT[0]; pt3D.Y = pT[1]; pt3D.Z = pT[2]; gset.Append(new GH_Point(Heron.Convert.ToXYZ(pt3D)), new GH_Path(i, iLayer, m, gi, n)); ///End loop through geometry points } subsub_geom.Dispose(); } } else { for (int ptnum = 0; ptnum < sub_geom.GetPointCount(); ptnum++) { ///Loop through geometry points double[] pT = new double[3]; pT[0] = sub_geom.GetX(ptnum); pT[1] = sub_geom.GetY(ptnum); pT[2] = sub_geom.GetZ(ptnum); Point3d pt3D = new Point3d(); pt3D.X = pT[0]; pt3D.Y = pT[1]; pt3D.Z = pT[2]; gset.Append(new GH_Point(Heron.Convert.ToXYZ(pt3D)), new GH_Path(i, iLayer, m, gi)); ///End loop through geometry points } } sub_geom.Dispose(); /// Get Feature Values if (fset.PathExists(new GH_Path(i, iLayer, m))) { fset.get_Branch(new GH_Path(i, iLayer, m)).Clear(); } for (int iField = 0; iField < feat.GetFieldCount(); iField++) { OSGeo.OGR.FieldDefn fdef = def.GetFieldDefn(iField); if (feat.IsFieldSet(iField)) { fset.Append(new GH_String(feat.GetFieldAsString(iField)), new GH_Path(i, iLayer, m)); } else { fset.Append(new GH_String("null"), new GH_Path(i, iLayer, m)); } } ///End Get Feature Values } //m++; } m++; feat.Dispose(); } ///end while loop through features } } //end loop through boundaries layer.Dispose(); }///end loop through layers ds.Dispose(); DA.SetDataTree(0, layname); DA.SetDataTree(1, fcs); DA.SetDataTree(2, recs); DA.SetDataTree(3, sRefs); DA.SetDataTree(4, fnames); DA.SetDataTree(5, fset); DA.SetDataTree(6, gset); }