Пример #1
0
        /// <summary>
        /// Export CurrentList to .xlsx
        /// </summary>
        /// <param name="path">path to target file</param>
        private void CurrentListAsExcel(string path)
        {
            using (var workbook = new XLWorkbook())
            {
                var worksheet = workbook.Worksheets.Add("POI");

                for (int i = 0; i < CurrentList.Count; i++)
                {
                    POI poi = CurrentList[i];

                    List <KeyValuePair <string, string> > properties = poi.GetPropertyList();

                    if (i == 0) // Create header
                    {
                        for (int column = 1; column <= properties.Count; column++)
                        {
                            string name = properties[column - 1].Key;
                            worksheet.Cell(1, column).SetValue <string>(name);
                            worksheet.Cell(1, column).Style.Fill.BackgroundColor = XLColor.LightSlateGray;
                            worksheet.Cell(1, column).Style.Font.FontColor       = XLColor.White;
                        }
                    }

                    for (int column = 1; column <= properties.Count; column++)
                    {
                        string value = properties[column - 1].Value;
                        worksheet.Cell(i + 2, column).SetValue <string>(value == null ? "null" : value);
                    }
                }

                workbook.SaveAs(path);
            }
        }
Пример #2
0
        /// <summary>
        /// Export CurrentList to .shp
        /// </summary>
        /// <param name="path">path to target shapefile</param>
        private void CurrentListAsShapefile(string path)
        {
            if (CurrentList.Count == 0)
            {
                throw new Exception("Current list is empty.");
            }

            // Create fields
            List <Field> fields = new List <Field>();
            List <KeyValuePair <string, string> > properties = CurrentList[0].GetPropertyList();
            int additionalFieldColumnIndexShapefile          = 0;

            for (int column = 1; column <= properties.Count; column++) //
            {
                string name = properties[column - 1].Key;
                int    length;
                if (name == "additional")
                {
                    additionalFieldColumnIndexShapefile = column + 1;
                    length = 16;
                }
                else
                {
                    length = 100;
                }
                fields.Add(GeoHelper.CreateField(name, name == "additional" ? esriFieldType.esriFieldTypeDouble : esriFieldType.esriFieldTypeString, length));
            }

            // Create spatial reference
            ISpatialReferenceFactory    srFactory            = new SpatialReferenceEnvironment();
            SpatialReferenceEnvironment pSpatialReferenceEnv = default(SpatialReferenceEnvironment);
            ISpatialReference2          pSpatialReference    = default(ISpatialReference2);
            //IGeographicCoordinateSystem cs = srFactory.CreateGeographicCoordinateSystem((int)esriSRGeoCSType.esriSRGeoCS_WGS1984);

            // Add data
            string filePath = "";
            string fileName = "";

            FileUtil.ExtractFileNameFromFullPath(path, ref filePath, ref fileName);
            GeoHelper.CreateShpFile(filePath, fileName, fields.ToArray(), pSpatialReference, (featureClass) =>
            {
                for (int i = 0; i < CurrentList.Count; i++)
                {
                    POI poi = CurrentList[i];

                    List <KeyValuePair <string, string> > _properties = poi.GetPropertyList();

                    IPoint point = new ESRI.ArcGIS.Geometry.Point();
                    point.X      = poi._lon ?? 0;
                    point.Y      = poi._lat ?? 0;

                    IFeature feature = featureClass.CreateFeature();
                    feature.Shape    = point;

                    // Attributes table record column index starts at 2
                    for (int column = 2; column <= _properties.Count + 1; column++) //
                    {
                        string value = _properties[column - 2].Value;
                        if (additionalFieldColumnIndexShapefile == column)
                        {
                            if (value == null)
                            {
                                value = "0";
                            }
                            feature.set_Value(column, double.Parse(value));
                        }
                        else
                        {
                            feature.set_Value(column, value);
                        }
                    }

                    feature.Store();
                }

                return(true);
            });
        }