/// <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)
        {
            List <string> driverName = new List <string>();

            bool run = false;

            DA.GetData(0, ref run);

            // See if gdal and ogr are working first.
            string output = "";

            try
            {
                GdalConfiguration.ConfigureOgr();

                GdalConfiguration.ConfigureGdal();

                output = "It works!";
            }

            catch (Exception e)
            {
                output = "{0} Exception caught. " + e;
            }

            if (run)
            {
                int driverCount = OSGeo.OGR.Ogr.GetDriverCount();
                //driverName.Add( driverCount.ToString() );

                for (int i = 0; i < driverCount; ++i)
                {
                    driverName.Add(OSGeo.OGR.Ogr.GetDriver(i).GetName());
                }
            }

            DA.SetData(0, output);

            DA.SetDataList(1, driverName);
        }
        /// <summary>
        /// This is the method that actually does the work.
        /// </summary>
        /// <param name="DA">The DA object can be used to retrieve data from input parameters and
        /// to store data in output parameters.</param>
        protected override void SolveInstance(IGH_DataAccess DA)
        {
            // See if gdal and ogr are working first.
            string output = "";

            try
            {
                GdalConfiguration.ConfigureOgr();

                GdalConfiguration.ConfigureGdal();

                output = "It works!";
            }

            catch (Exception e)
            {
                output = "{0} Exception caught. " + e;
            }

            string input = "";

            DA.GetData(0, ref input);

            var driver = OSGeo.OGR.Ogr.GetDriverByName("GeoJSON");
            var ds     = driver.Open(input, 0);

            long numberOfFeatures = 0;
            int  fieldCount       = 0;

            var layer           = ds.GetLayerByIndex(0);
            var layerDefinition = layer.GetLayerDefn();

            numberOfFeatures = unchecked (( int )layer.GetFeatureCount(0));
            fieldCount       = layerDefinition.GetFieldCount();

            var fields = new List <string>();

            for (int i = 0; i < fieldCount; ++i)
            {
                fields.Add(layerDefinition.GetFieldDefn(i).GetName());
            }

            int fieldToSelect = 0;

            DA.GetData(1, ref fieldToSelect);

            var pointOut = new DataTree <Point3d>();
            var fieldOut = new DataTree <double>();

            for (int i = 0; i < numberOfFeatures; ++i)
            {
                var path       = new GH_Path(i);
                var feature    = layer.GetFeature(i);
                var geo        = feature.GetGeometryRef();
                var ring       = geo.GetGeometryRef(0);
                int pointCount = ring.GetPointCount();
                fieldOut.Add(feature.GetFieldAsDouble(fieldToSelect), path);

                for (int j = 0; j < pointCount; ++j)
                {
                    double[] pointList = { 0, 0, 0 };
                    ring.GetPoint(j, pointList);
                    pointOut.Add(new Point3d(pointList[0], pointList[1], pointList[2]), path);
                }
            }

            DA.SetData(0, output);
            DA.SetData(1, numberOfFeatures);
            DA.SetDataList(2, fields);
            DA.SetDataTree(3, pointOut);
            DA.SetDataTree(4, fieldOut);
        }
        /// <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)
        {
            // See if gdal and ogr are working first.
            string output = "";

            try
            {
                GdalConfiguration.ConfigureOgr();

                GdalConfiguration.ConfigureGdal();

                output = "It works!";
            }

            catch (Exception e)
            {
                output = "{0} Exception caught. " + e;
            }

            string input = "";

            DA.GetData(0, ref input);

            var driver = OSGeo.OGR.Ogr.GetDriverByName("ESRI Shapefile");

            if (driver == null)
            {
                output = "Driver is null";
            }

            else
            {
                var ds = driver.Open(input, 0);

                if (ds == null)
                {
                    output = "DataSource is null";
                }

                else
                {
                    long numberOfFeatures = 0;
                    int  fieldCount       = 0;

                    int fieldSelector = 0;

                    DA.GetData(1, ref fieldSelector);

                    var layer = ds.GetLayerByIndex(0);

                    string geoType = layer.GetGeomType().ToString();

                    var layerDefinition = layer.GetLayerDefn();

                    numberOfFeatures = unchecked ((int)layer.GetFeatureCount(0));
                    fieldCount       = layerDefinition.GetFieldCount();

                    string[] fields = new string[fieldCount];

                    object[] fieldOut = new object[numberOfFeatures];

                    for (int i = 0; i < fieldCount; ++i)
                    {
                        fields[i] = layerDefinition.GetFieldDefn(i).GetName();
                    }

                    int fieldToSelect = 0;
                    DA.GetData(1, ref fieldToSelect);

                    //bool parallel = false;

                    //DA.GetData( 2, ref parallel );

                    OSGeo.OGR.Feature feature = null;

                    //if( parallel == false )
                    {
                        for (int i = 0; i < numberOfFeatures; ++i)
                        {
                            feature     = layer.GetFeature(i);
                            fieldOut[i] = feature.GetFieldAsString(fieldSelector);
                        }
                    }

                    /*else
                     * {
                     *
                     *  try
                     *  {
                     *
                     *      object locker = new object();
                     *
                     *      System.Threading.Tasks.Parallel.For( 0, numberOfFeatures, i =>
                     *      {
                     *
                     *          feature = layer.GetFeature(i);
                     *
                     *          lock( locker )
                     *          {
                     *
                     *              fieldOut[i] = feature.GetFieldAsString( fieldSelector );
                     *
                     *          }
                     *
                     *          //feature = null;
                     *
                     *      });
                     *
                     *  }
                     *
                     *  catch( Exception e )
                     *  {
                     *
                     *      output = "{0} Exception caught. " + e;
                     *
                     *  }
                     *
                     * }*/

                    DA.SetData(1, numberOfFeatures);
                    DA.SetDataList(2, fields);
                    DA.SetDataList(3, fieldOut);
                    DA.SetData(4, geoType);
                }
            }

            DA.SetData(0, output);
        }
Example #4
0
        /// <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)
        {
            // See if gdal and ogr are working first.
            string output = "";

            try
            {
                GdalConfiguration.ConfigureOgr();

                GdalConfiguration.ConfigureGdal();

                output = "It works!";
            }

            catch (Exception e)
            {
                output = "{0} Exception caught. " + e;
            }

            string input = "";

            DA.GetData(0, ref input);

            var driver = OSGeo.OGR.Ogr.GetDriverByName("ESRI Shapefile");

            if (driver == null)
            {
                output = "Driver is null";
            }

            else
            {
                var ds = driver.Open(input, 0);

                if (ds == null)
                {
                    output = "DataSource is null";
                }

                else
                {
                    Grasshopper.DataTree <double> latitudesOut = new Grasshopper.DataTree <double>(), longitudesOut = new Grasshopper.DataTree <double>(), altitudesOut = new Grasshopper.DataTree <double>();
                    Grasshopper.DataTree <bool>   cullPattern = new Grasshopper.DataTree <bool>();

                    long numberOfFeatures = 0;

                    var layer = ds.GetLayerByIndex(0);

                    string geoType = layer.GetGeomType().ToString();

                    numberOfFeatures = unchecked ((int)layer.GetFeatureCount(0));

                    bool parallel = false;

                    DA.GetData(1, ref parallel);

                    GH_Path path = new GH_Path();

                    OSGeo.OGR.Feature  feature = null;
                    OSGeo.OGR.Geometry geo = null, ring = null;

                    double[] pointList = { 0, 0, 0 };

                    if (parallel == false)
                    {
                        if (geoType == "wkbPolygon")
                        {
                            for (int i = 0; i < numberOfFeatures; ++i)
                            {
                                path    = new GH_Path(i);
                                feature = layer.GetFeature(i);
                                geo     = feature.GetGeometryRef();
                                ring    = geo.GetGeometryRef(0);
                                int pointCount = ring.GetPointCount();

                                cullPattern.Add(pointCount > 0 ? true : false, path);

                                for (int j = 0; j < pointCount; ++j)
                                {
                                    ring.GetPoint(j, pointList);

                                    latitudesOut.Add(pointList[1], path);
                                    longitudesOut.Add(pointList[0], path);
                                    altitudesOut.Add(pointList[2], path);
                                }
                            }
                        }

                        else if (geoType == "wkbLineString")
                        {
                            for (int i = 0; i < numberOfFeatures; ++i)
                            {
                                path    = new GH_Path(i);
                                feature = layer.GetFeature(i);
                                geo     = feature.GetGeometryRef();
                                int pointCount = geo.GetPointCount();

                                cullPattern.Add(pointCount > 0 ? true : false, path);

                                for (int j = 0; j < pointCount; ++j)
                                {
                                    geo.GetPoint(j, pointList);

                                    latitudesOut.Add(pointList[1], path);
                                    longitudesOut.Add(pointList[0], path);
                                    altitudesOut.Add(pointList[2], path);
                                }
                            }
                        }

                        else if (geoType == "wkbPoint" || geoType == "wkbPoint25D")
                        {
                            // TODO add reprojections if needed.
                            //OSGeo.OSR.CoordinateTransformation.TransformPoint(  )

                            for (int i = 0; i < numberOfFeatures; ++i)
                            {
                                path = new GH_Path(i);

                                try
                                {
                                    feature = layer.GetFeature(i);

                                    geo = feature.GetGeometryRef();

                                    geo.GetPoint(0, pointList);

                                    latitudesOut.Add(pointList[1], path);
                                    longitudesOut.Add(pointList[0], path);
                                    altitudesOut.Add(pointList[2], path);
                                }

                                catch (Exception e)
                                {
                                    output = "{0} Exception caught. " + e;
                                }
                            }
                        }

                        else
                        {
                            output = "Geometry type not implemented yet";
                        }
                    }

                    else
                    {
                        if (geoType == "wkbPolygon")
                        {
                            double[][] latMat = new double[numberOfFeatures][];
                            double[][] lonMat = new double[numberOfFeatures][];
                            double[][] altMat = new double[numberOfFeatures][];

                            for (int i = 0; i < numberOfFeatures; ++i)
                            {
                                path    = new GH_Path(i);
                                feature = layer.GetFeature(i);
                                geo     = feature.GetGeometryRef();
                                ring    = geo.GetGeometryRef(0);
                                int pointCount = ring.GetPointCount();

                                latMat[i] = new double[pointCount];
                                lonMat[i] = new double[pointCount];
                                altMat[i] = new double[pointCount];

                                System.Threading.Tasks.Parallel.For(0, pointCount, j =>
                                {
                                    layer.GetFeature(i).GetGeometryRef().GetPoint(j, pointList);

                                    latMat[i][j] = pointList[1];
                                    lonMat[i][j] = pointList[0];
                                    altMat[i][j] = pointList[2];
                                });

                                if (pointCount > 0)
                                {
                                    latitudesOut.AddRange(latMat[i], path);
                                    longitudesOut.AddRange(lonMat[i], path);
                                    altitudesOut.AddRange(altMat[i], path);
                                    cullPattern.Add(true, path);
                                }

                                else
                                {
                                    cullPattern.Add(false, path);
                                }
                            }
                        }

                        else if (geoType == "wkbLineString")
                        {
                            double[][]           latMat = new double[numberOfFeatures][];
                            double[][]           lonMat = new double[numberOfFeatures][];
                            double[][]           altMat = new double[numberOfFeatures][];
                            OSGeo.OGR.Geometry[] g      = new OSGeo.OGR.Geometry[numberOfFeatures];

                            for (int i = 0; i < numberOfFeatures; ++i)
                            {
                                path    = new GH_Path(i);
                                feature = layer.GetFeature(i);
                                geo     = feature.GetGeometryRef();

                                g[i] = geo;

                                int pointCount = geo.GetPointCount();

                                latMat[i] = new double[pointCount];
                                lonMat[i] = new double[pointCount];
                                altMat[i] = new double[pointCount];

                                System.Threading.Tasks.Parallel.For(0, pointCount, j =>
                                {
                                    g[i].GetPoint(j, pointList);



                                    latMat[i][j] = pointList[1];
                                    lonMat[i][j] = pointList[0];
                                    altMat[i][j] = pointList[2];
                                });

                                if (pointCount > 0)
                                {
                                    latitudesOut.AddRange(latMat[i], path);
                                    longitudesOut.AddRange(lonMat[i], path);
                                    altitudesOut.AddRange(altMat[i], path);
                                    cullPattern.Add(true, path);
                                }

                                else
                                {
                                    cullPattern.Add(false, path);
                                }
                            }
                        }

                        else if (geoType == "wkbPoint" || geoType == "wkbPoint25D")
                        {
                            double[] latMat = new double[numberOfFeatures];
                            double[] lonMat = new double[numberOfFeatures];
                            double[] altMat = new double[numberOfFeatures];

                            System.Threading.Tasks.Parallel.For(0, numberOfFeatures, i =>
                            {
                                //feature = layer.GetFeature(i);

                                //geo = feature.GetGeometryRef();
                                layer.GetFeature(i).GetGeometryRef().GetPoint(0, pointList);

                                latMat[i] = pointList[1];
                                lonMat[i] = pointList[0];
                                altMat[i] = pointList[2];
                            });

                            for (int i = 0; i < numberOfFeatures; ++i)
                            {
                                path = new GH_Path(i);

                                latitudesOut.Add(latMat[i], path);
                                longitudesOut.Add(lonMat[i], path);
                                altitudesOut.Add(altMat[i], path);
                            }
                        }

                        else
                        {
                            output = "Geometry type not implemented yet";
                        }
                    }

                    DA.SetDataTree(1, latitudesOut);
                    DA.SetDataTree(2, longitudesOut);
                    DA.SetDataTree(3, altitudesOut);
                    DA.SetDataTree(4, cullPattern);
                }
            }

            DA.SetData(0, output);
        }