/// <summary> /// Creates an ExtendedData KML element from a graphic's attributes. /// </summary> /// <param name="attributes">A dictionary of graphic attributes</param> /// <returns></returns> public static ExtendedData ToExtendedData(Dictionary <string, object> attributes) { var datas = from kvp in attributes where !_geometryInNameRe.IsMatch(kvp.Key) select new Data { Name = kvp.Key, //Value = kvp.Value?.ToString() // VS 2015 Value = kvp.Value != null?kvp.Value.ToString() : null }; var xData = new ExtendedData(); foreach (var data in datas) { xData.AddData(data); } return(xData); }
/// <summary> /// converte la feature class in kml. Se non sono presenti le coordinate il dato non viene inserito /// </summary> /// <param name="sourceWorkspace">workspace con la feature class</param> /// <param name="outputName">nome della feature class e del csv di output</param> /// <param name="targetPath">percorso dove salvare il file di output</param> /// <param name="delimitator">delimitatore utilizzato nel csv</param> private void ConvertFeatureClassPointToKml(IWorkspace sourceWorkspace, string outputName, string targetPath) { IFeatureCursor featureCursor = null; try { IFeatureWorkspace featureWorkspace = sourceWorkspace as IFeatureWorkspace; IFeatureClass featureClass = featureWorkspace.OpenFeatureClass(outputName); if (featureClass.ShapeType != esriGeometryType.esriGeometryPoint) { throw new Exception($"Per esportare in {Enum.GetName(typeof(FileExtension), FileExtension.kmz)} occorre una feature class di tipo {Enum.GetName(typeof(esriGeometryType), esriGeometryType.esriGeometryPoint)}!"); } featureCursor = featureClass.Search(null, true); var folder = new Folder(); folder.Id = outputName; folder.Name = outputName; Dictionary <int, string> fields = new Dictionary <int, string>(); IField field = null; for (int i = 0; i < featureCursor.Fields.FieldCount; i++) { field = featureCursor.Fields.Field[i]; if ((field.Type == esriFieldType.esriFieldTypeBlob) || (field.Type == esriFieldType.esriFieldTypeGeometry) || (field.Type == esriFieldType.esriFieldTypeGlobalID) || (field.Type == esriFieldType.esriFieldTypeGUID) || (field.Type == esriFieldType.esriFieldTypeRaster) || (field.Type == esriFieldType.esriFieldTypeXML)) { continue; } fields.Add(i, field.Name); } IFeature feature = null; Placemark placemark = null; Vector vector = null; ExtendedData extendedData = null; while ((feature = featureCursor.NextFeature()) != null) { if ((feature.ShapeCopy == null) || (feature.ShapeCopy.IsEmpty)) { continue; } IPoint p = feature.ShapeCopy as IPoint; if (p.SpatialReference.FactoryCode != Helper.SpatialReferenceWGS84.FactoryCode) { p.Project(Helper.SpatialReferenceWGS84); } vector = new Vector(p.Y, p.X); placemark = new Placemark(); placemark.Id = feature.OID.ToString(); placemark.Name = feature.OID.ToString(); extendedData = new ExtendedData(); foreach (int i in fields.Keys) { Data data = new Data(); data.DisplayName = fields[i]; data.Name = fields[i]; data.Value = feature.get_Value(i).ToString(); extendedData.AddData(data); } placemark.ExtendedData = extendedData; placemark.Geometry = new SharpKml.Dom.Point { Coordinate = vector }; folder.AddFeature(placemark); } Kml kml = new Kml(); kml.AddNamespacePrefix(KmlNamespaces.GX22Prefix, KmlNamespaces.GX22Namespace); kml.Feature = folder; KmlFile kmlfile = KmlFile.Create(kml, false); using (var stream = File.OpenWrite(System.IO.Path.Combine(targetPath, System.IO.Path.ChangeExtension(outputName, Enum.GetName(typeof(FileExtension), FileExtension.kmz))))) { kmlfile.Save(stream); } } catch { throw; } finally { if (featureCursor != null) { Marshal.FinalReleaseComObject(featureCursor); } } }