/// <summary>
        /// Creates shapefile from search results, shows shapefile on map display
        /// </summary>
        /// <param name="SeriesCatalogResults"></param>
        private void PutReturnOnMap(IEnumerable <FacetedSearch3.CUAHSIFacetedSearch.SeriesCatalogRecord> SeriesCatalogResults)
        {
            FeatureSet fs = new FeatureSet(FeatureType.Point);

            try
            {
                ClearPointLayersFromMap();

                // string shapeFileName = String.Format(@"{0}\{1}.shp", Settings.Instance.TempDirectory, "FacetedSearchResult");

                fs.DataTable.Columns.Add(new DataColumn("ServiceCode", typeof(string)));
                fs.DataTable.Columns.Add(new DataColumn("ServiceURL", typeof(string)));
                fs.DataTable.Columns.Add(new DataColumn("SiteCode", typeof(string)));
                fs.DataTable.Columns.Add(new DataColumn("SiteName", typeof(string))); //to improve display of labels and pop-up. shows a copy of SiteCode
                fs.DataTable.Columns.Add(new DataColumn("VarCode", typeof(string)));
                fs.DataTable.Columns.Add(new DataColumn("VarName", typeof(string)));  //to improve display of labels and pop-up. shows a copy of VarCode
                fs.DataTable.Columns.Add(new DataColumn("StartDate", typeof(DateTime)));
                fs.DataTable.Columns.Add(new DataColumn("EndDate", typeof(DateTime)));
                fs.DataTable.Columns.Add(new DataColumn("ValueCount", typeof(int)));

                foreach (FacetedSearch3.CUAHSIFacetedSearch.SeriesCatalogRecord o in SeriesCatalogResults)
                {
                    DotSpatial.Topology.Point p = new DotSpatial.Topology.Point(o.Longitude, o.Latitude);
                    IFeature f = fs.AddFeature(p);
                    f.DataRow.BeginEdit();
                    f.DataRow["ServiceCode"] = o.ServCode;
                    f.DataRow["ServiceURL"]  = o.ServURL;
                    f.DataRow["SiteCode"]    = o.SiteCode;
                    f.DataRow["SiteName"]    = o.SiteName;
                    f.DataRow["VarCode"]     = o.VarCode;
                    f.DataRow["VarName"]     = o.VariableName;
                    f.DataRow["StartDate"]   = o.StartDate;
                    f.DataRow["EndDate"]     = o.EndDate;
                    f.DataRow["ValueCount"]  = o.ValueCount;
                    f.DataRow.EndEdit();
                }

                //set the projection
                fs.Projection =
                    new ProjectionInfo();
                fs.ProjectionString = "+proj=longlat +ellps=WGS84 +no_defs";

                // the faceted search shapefile is saved to the current project directory
                // preferably this should be in the current project's directory
                // if the current project directory doesn't exist then use a temp folder
                string facetedSearchShapefileFullPath;
                if (App.SerializationManager.CurrentProjectDirectory == null)
                {
                    string hdTempPath = System.IO.Path.Combine(System.IO.Path.GetTempPath(), "HydroDesktop");
                    facetedSearchShapefileFullPath = System.IO.Path.Combine(hdTempPath, FacetedShapeFileName);
                }
                else
                {
                    facetedSearchShapefileFullPath = System.IO.Path.Combine(App.SerializationManager.CurrentProjectDirectory, FacetedShapeFileName);
                }
                fs.Filename = facetedSearchShapefileFullPath;
                fs.Save();

                // implement threshold for adding to map directly or via shapefile on disk?
                if (SeriesCatalogResults.Count <FacetedSearch3.CUAHSIFacetedSearch.SeriesCatalogRecord>() > 25000)
                {
                }
                else
                {
                }

                // need to use the full path (relative path didn't work when deploying
                // the plugin as a package)
                App.Map.AddLayer(facetedSearchShapefileFullPath);
            }
            finally
            {
                fs.Dispose();
            }

            //add featureSet to the map
            // IMapLayer newLayer = MapArgs.Map.Layers.Add(shapeFileName);

            //add labels
            // MapArgs.Map.AddLabels((IFeatureLayer)newLayer,
            // String.Format("[{0}]", "FacetedSearchResults"), String.Empty,
            //     new LabelSymbolizer());
        }
Пример #2
0
        /// <summary>
        /// given a data table, create an in-memory point feature set.
        /// The feature set must have the 'Latitude' and 'Longitude' numeric
        /// columns
        /// </summary>
        /// <param name="themeTable">The table of distinct series</param>
        /// <param name="projection">The projection of the theme feature set</param>
        /// <returns>A point FeatureSet in the WGS-84 coordinate system
        /// All columns of the data table will be converted to atrribute fields</returns>
        private IFeatureSet TableToFeatureSet(DataTable themeTable, ProjectionInfo projection = null)
        {
            //index of the Latitude column
            int latColIndex = -1;

            //index of the Longitude column
            int lonColIndex = -1;

            //get the latitude and longitude column indices
            for (int col = 0; col < themeTable.Columns.Count; col++)
            {
                string colName = themeTable.Columns[col].ColumnName.ToLower();

                if (colName == "latitude")
                {
                    latColIndex = col;
                }

                if (colName == "longitude")
                {
                    lonColIndex = col;
                }
            }

            //check if the latitude column exists
            if (latColIndex == -1)
            {
                throw new ArgumentException("The table doesn't have a column 'Latitude'");
            }
            //check if the longitude column exists
            if (lonColIndex == -1)
            {
                throw new ArgumentException("The table doesn't have a column 'Longitude'");
            }

            //generate attribute table schema
            var fs             = new FeatureSet(FeatureType.Point);
            var attributeTable = fs.DataTable;

            foreach (DataColumn column in themeTable.Columns)
            {
                attributeTable.Columns.Add(column.DataType == typeof(DateTime)
                                               ? new Field(column.ColumnName, 'C', 16, 0)
                                               : new DataColumn(column.ColumnName, column.DataType));
            }

            //generate features
            foreach (DataRow row in themeTable.Rows)
            {
                double lat        = Convert.ToDouble(row[latColIndex]);
                double lon        = Convert.ToDouble(row[lonColIndex]);
                var    coord      = new Coordinate(lon, lat);
                var    newFeature = new Feature(FeatureType.Point, new[] { coord });
                fs.Features.Add(newFeature);

                var featureRow = newFeature.DataRow;
                for (int c = 0; c < attributeTable.Columns.Count; c++)
                {
                    featureRow[c] = themeTable.Columns[c].DataType == typeof(DateTime)
                                        ? ConvertTime((DateTime)row[c])
                                        : row[c];
                }
            }

            //to save the feature set to a file with unique name
            string uniqueID = DateTime.Now.ToString("yyyyMMdd_hhmmss");
            var    rnd      = new Random();

            uniqueID += rnd.Next(100).ToString("000");
            var filename = Path.Combine(Settings.Instance.CurrentProjectDirectory, "theme_" + uniqueID + ".shp");

            fs.Filename   = filename;
            fs.Projection = _wgs84Projection;
            fs.Save();
            fs.Dispose();

            var fs2 = FeatureSet.OpenFile(filename);

            //to reproject the feature set
            if (projection != null)
            {
                fs2.Reproject(projection);
            }

            return(fs2);
        }
Пример #3
0
        public void shpWrite(string path)
        {
            fs.DataTable.Columns.Add(new DataColumn("ID", typeof(int)));
            DataColumn col = new DataColumn("Project", typeof(string));

            col.MaxLength = 50;
            fs.DataTable.Columns.Add(col);
            fs.DataTable.Columns.Add(new DataColumn("Area", typeof(double)));
            IFeatureSet fsource = FeatureSet.Open(@"Sample\Sample.shp");

            fs.Projection = fsource.Projection;
            fsource.Close();
            int ID = 0;

            foreach (Geometries geometry in project.Geometries)
            {
                ID++;
                Polygon[] pgs = new Polygon[geometry.Polygons.Count];
                int       i   = 0;
                //foreach (Geometries geometry in project.Geometries) {

                foreach (GeoPolygon polygon in geometry.Polygons)
                {
                    List <Coordinate> vertices = new List <Coordinate>();

                    //polygon.Points.Reverse();

                    if (polygon.Circle > 1 && !polygon.GetDirection())
                    {
                        polygon.Points.Reverse();
                    }
                    else if (polygon.Circle == 1 && polygon.GetDirection())
                    {
                        polygon.Points.Reverse();
                    }

                    foreach (GeoPoint point in polygon.Points)
                    {
                        Coordinate vertice = new Coordinate();
                        vertice.X = point.X;
                        vertice.Y = point.Y;
                        vertices.Add(vertice);
                    }

                    Polygon geom = new Polygon(vertices);

                    pgs[i] = geom;

                    i++;
                }
                //}


                MultiPolygon geoms = new MultiPolygon(pgs);
                geoms.ToText();
                IFeature feature = fs.AddFeature(geoms);
                feature.DataRow.BeginEdit();
                feature.DataRow["ID"]      = ID;
                feature.DataRow["Project"] = project.Name;
                //feature.DataRow["Area"] = feature.Area();
                feature.DataRow.EndEdit();
            }
            //fs.Projection = ProjectionInfo.(@"F:\数据\2013SHP\DLTB.shp");
            //fs.ProjectionString = " +x_0=40500000 +y_0=0 +lat_0=0 +lon_0=120 +proj=tmerc +a=6378140 +b=6356755.28815753 +no_defs";

            fs.SaveAs(path, true);

            fs.Dispose();
            GeoRead gr = new GeoRead(path);

            gr.shpAreaReCalculate();
            //fs = FeatureSet.Open(path);
        }