protected override FeatureLayer CreateFeatureLayerCore(ConfigureFeatureLayerParameters featureLayerStructureParameters) { string layerPath = LayerPluginHelper.GetLayerUriToSave(featureLayerStructureParameters.LayerUri, ExtensionFilter); if (string.IsNullOrEmpty(layerPath)) { return(null); } featureLayerStructureParameters.LayerUri = new Uri(layerPath); Collection <TabDbfColumn> tabDbfColumns = new Collection <TabDbfColumn>(); foreach (var column in featureLayerStructureParameters.AddedColumns) { var columnLenght = column.MaxLength; var decimalLength = 0; DbfColumnType columnType = DbfColumnType.Character; switch (column.TypeName.ToUpperInvariant()) { case "DOUBLE": columnLenght = columnLenght == 0 ? 10 : columnLenght; decimalLength = 4; columnType = DbfColumnType.Float; break; case "DATE": case "DATETIME": columnLenght = columnLenght == 0 ? 10 : columnLenght; decimalLength = 0; columnType = DbfColumnType.Date; break; case "INTEGER": case "INT": columnLenght = columnLenght == 0 ? 10 : columnLenght; decimalLength = 0; columnType = DbfColumnType.Numeric; break; case "STRING": case "CHARACTER": columnLenght = columnLenght == 0 ? 255 : columnLenght; decimalLength = 0; columnType = DbfColumnType.Character; break; default: break; } tabDbfColumns.Add(new TabDbfColumn(column.ColumnName, columnType, columnLenght, decimalLength, false, false)); } if (featureLayerStructureParameters.CustomData.ContainsKey("SourceLayer")) { FeatureLayer sourcefeatureLayer = featureLayerStructureParameters.CustomData["SourceLayer"] as FeatureLayer; double precisionInMeter = TinyGeoFeatureLayer.GetOptimalPrecision(sourcefeatureLayer, GisEditor.ActiveMap.MapUnit, DistanceUnit.Meter, TinyGeoPrecisionMode.PreventSplitting); TinyGeoFeatureLayer.CreateTinyGeoFile(featureLayerStructureParameters.LayerUri.OriginalString, sourcefeatureLayer, GisEditor.ActiveMap.MapUnit, featureLayerStructureParameters.AddedColumns.Select(f => f.ColumnName).ToList(), "", precisionInMeter, Encoding.Default, featureLayerStructureParameters.WellKnownType); string prjPath = Path.ChangeExtension(featureLayerStructureParameters.LayerUri.OriginalString, "prj"); File.WriteAllText(prjPath, Proj4Projection.ConvertProj4ToPrj(featureLayerStructureParameters.Proj4ProjectionParametersString)); } var resultLayer = new TinyGeoFeatureLayer(featureLayerStructureParameters.LayerUri.OriginalString); return(resultLayer); }
protected override FeatureLayer CreateFeatureLayerCore(ConfigureFeatureLayerParameters featureLayerStructureParameters) { string layerPath = LayerPluginHelper.GetLayerUriToSave(featureLayerStructureParameters.LayerUri, ExtensionFilter); string fileName = Path.GetFileNameWithoutExtension(layerPath); if (string.IsNullOrEmpty(layerPath)) { return(null); } if (fileName.FirstOrDefault() != null && Char.IsNumber(fileName.FirstOrDefault())) { MessageBox.Show("Table name can not start with number."); return(null); } featureLayerStructureParameters.LayerUri = new Uri(layerPath); Collection <FeatureSourceColumn> columns = new Collection <FeatureSourceColumn>(); System.Text.RegularExpressions.Regex regx = new System.Text.RegularExpressions.Regex(@"\W"); foreach (var column in featureLayerStructureParameters.AddedColumns) { FeatureSourceColumn newColumn = column; if (newColumn.ColumnName.Equals("Null", StringComparison.Ordinal)) { newColumn.ColumnName = "Null_"; } newColumn.ColumnName = regx.Replace(newColumn.ColumnName, string.Empty); columns.Add(newColumn); } string tableName = string.Empty; if (featureLayerStructureParameters.CustomData.ContainsKey("TableName")) { //if (createFeatureLayerParameters.Tag != null) //tableName = createFeatureLayerParameters.Tag.ToString().Split('|').First(); tableName = featureLayerStructureParameters.CustomData["TableName"] as string; } else { tableName = Path.GetFileNameWithoutExtension(featureLayerStructureParameters.LayerUri.OriginalString); } FileGeoDatabaseFeatureLayer.CreateFileGeoDatabase(featureLayerStructureParameters.LayerUri.OriginalString); FileGeoDatabaseFeatureLayer.CreateTable(featureLayerStructureParameters.LayerUri.OriginalString, tableName, featureLayerStructureParameters.WellKnownType, columns); string prjPath = Path.ChangeExtension(featureLayerStructureParameters.LayerUri.OriginalString, "prj"); File.WriteAllText(prjPath, Proj4Projection.ConvertProj4ToPrj(featureLayerStructureParameters.Proj4ProjectionParametersString)); FileGeoDatabaseFeatureLayer fileGeoDatabaseFeatureLayer = new FileGeoDatabaseFeatureLayer(featureLayerStructureParameters.LayerUri.LocalPath, tableName); if (featureLayerStructureParameters.AddedFeatures.Count > 0) { fileGeoDatabaseFeatureLayer.Open(); fileGeoDatabaseFeatureLayer.EditTools.BeginTransaction(); foreach (var feature in featureLayerStructureParameters.AddedFeatures) { fileGeoDatabaseFeatureLayer.EditTools.Add(feature); } fileGeoDatabaseFeatureLayer.EditTools.CommitTransaction(); fileGeoDatabaseFeatureLayer.Close(); } return(fileGeoDatabaseFeatureLayer); }
protected override FeatureLayer CreateFeatureLayerCore(ConfigureFeatureLayerParameters featureLayerStructureParameters) { bool hasFeatures = featureLayerStructureParameters.AddedFeatures.Count > 0; if (hasFeatures) { List <string> columns = featureLayerStructureParameters.AddedFeatures[0].ColumnValues.Keys.ToList(); WKTColumn = GetUniqueColumn(WKTColumn, columns, 0); LongitudeColumn = GetUniqueColumn(LongitudeColumn, columns, 0); LatitudeColumn = GetUniqueColumn(LatitudeColumn, columns, 0); foreach (var feature in featureLayerStructureParameters.AddedFeatures) { PointShape pointBaseShape = feature.GetShape() as PointShape; if (pointBaseShape != null) { feature.ColumnValues[WKTColumn] = pointBaseShape.GetWellKnownText(); } else { feature.ColumnValues[LongitudeColumn] = ""; feature.ColumnValues[LatitudeColumn] = ""; } } } string layerPath = LayerPluginHelper.GetLayerUriToSave(featureLayerStructureParameters.LayerUri, ExtensionFilter); if (string.IsNullOrEmpty(layerPath)) { return(null); } featureLayerStructureParameters.LayerUri = new Uri(layerPath); Collection <string> csvColumns = new Collection <string>(); foreach (var column in featureLayerStructureParameters.AddedColumns) { csvColumns.Add(column.ColumnName); } string delimiter = ","; if (featureLayerStructureParameters.CustomData.ContainsKey("Delimiter")) { delimiter = featureLayerStructureParameters.CustomData["Delimiter"].ToString(); } else if (featureLayerStructureParameters.WellKnownType != WellKnownType.Point && featureLayerStructureParameters.WellKnownType != WellKnownType.Multipoint) { delimiter = "\t"; } DelimitedSpatialColumnsType csvMappingType = DelimitedSpatialColumnsType.WellKnownText; if (featureLayerStructureParameters.CustomData.ContainsKey("MappingType")) { csvMappingType = (DelimitedSpatialColumnsType)featureLayerStructureParameters.CustomData["MappingType"]; } else { if (featureLayerStructureParameters.WellKnownType == WellKnownType.Point || featureLayerStructureParameters.WellKnownType == WellKnownType.Multipoint) { csvMappingType = DelimitedSpatialColumnsType.XAndY; featureLayerStructureParameters.CustomData["X"] = LongitudeColumn; featureLayerStructureParameters.CustomData["Y"] = LatitudeColumn; csvColumns.Add(LongitudeColumn); csvColumns.Add(LatitudeColumn); if (hasFeatures) { csvColumns.Add(WKTColumn); } } else { csvMappingType = DelimitedSpatialColumnsType.WellKnownText; featureLayerStructureParameters.CustomData["WKT"] = WKTColumn; if (hasFeatures) { csvColumns.Add(LongitudeColumn); csvColumns.Add(LatitudeColumn); } csvColumns.Add(WKTColumn); } } CsvFeatureLayer.CreateDelimitedFile(featureLayerStructureParameters.LayerUri.OriginalString, csvColumns, delimiter, OverwriteMode.Overwrite); string prjPath = Path.ChangeExtension(featureLayerStructureParameters.LayerUri.OriginalString, "prj"); File.WriteAllText(prjPath, Proj4Projection.ConvertProj4ToPrj(featureLayerStructureParameters.Proj4ProjectionParametersString)); var resultLayer = new CsvFeatureLayer(); resultLayer.DelimitedPathFilename = featureLayerStructureParameters.LayerUri.LocalPath; resultLayer.Delimiter = delimiter; resultLayer.SpatialColumnType = csvMappingType; resultLayer.RequireIndex = false; if (featureLayerStructureParameters.CustomData.ContainsKey("X")) { resultLayer.XColumnName = featureLayerStructureParameters.CustomData["X"].ToString(); } if (featureLayerStructureParameters.CustomData.ContainsKey("Y")) { resultLayer.YColumnName = featureLayerStructureParameters.CustomData["Y"].ToString(); } if (featureLayerStructureParameters.CustomData.ContainsKey("WKT")) { resultLayer.WellKnownTextColumnName = featureLayerStructureParameters.CustomData["WKT"].ToString(); } resultLayer.Open(); if (featureLayerStructureParameters.AddedFeatures.Count > 0) { resultLayer.EditTools.BeginTransaction(); foreach (var feature in featureLayerStructureParameters.AddedFeatures) { resultLayer.EditTools.Add(feature); } resultLayer.EditTools.CommitTransaction(); } CSVModel.BuildDelimitedConfigurationFile(resultLayer); resultLayer.Close(); return(resultLayer); }
protected override FeatureLayer CreateFeatureLayerCore(ConfigureFeatureLayerParameters featureLayerStructureParameters) { string layerPath = LayerPluginHelper.GetLayerUriToSave(featureLayerStructureParameters.LayerUri, ExtensionFilterCore); if (string.IsNullOrEmpty(layerPath)) { return(null); } featureLayerStructureParameters.LayerUri = new Uri(layerPath); ShapeFileType shapeFileType = ShapeFileType.Null; switch (featureLayerStructureParameters.WellKnownType) { case WellKnownType.Multipoint: shapeFileType = ShapeFileType.Multipoint; break; case WellKnownType.Point: shapeFileType = ShapeFileType.Point; break; case WellKnownType.Line: case WellKnownType.Multiline: shapeFileType = ShapeFileType.Polyline; break; case WellKnownType.Polygon: case WellKnownType.Multipolygon: shapeFileType = ShapeFileType.Polygon; break; } Dictionary <string, DbfColumn> dbfColumns = new Dictionary <string, DbfColumn>(); Collection <FeatureSourceColumn> addedColumns = featureLayerStructureParameters.AddedColumns; Dictionary <string, string> oldNewNames = new Dictionary <string, string>(); Collection <Feature> addedFeatures = featureLayerStructureParameters.AddedFeatures; bool truncateLongColumn = featureLayerStructureParameters.LongColumnTruncateMode == LongColumnTruncateMode.Truncate; if (truncateLongColumn) { Dictionary <string, string> editColumns = new Dictionary <string, string>(); if (featureLayerStructureParameters.CustomData.ContainsKey("EditedColumns")) { editColumns = featureLayerStructureParameters.CustomData["EditedColumns"] as Dictionary <string, string>; } addedColumns = TruncateLongColumnNames(featureLayerStructureParameters.AddedColumns, oldNewNames, editColumns); } foreach (var column in addedColumns) { if (!string.IsNullOrEmpty(column.ColumnName)) { DbfColumn dbfColumn = column as DbfColumn; if (dbfColumn != null) { if (dbfColumn.ColumnType == DbfColumnType.DoubleInBinary || dbfColumn.ColumnType == DbfColumnType.DateTime) { dbfColumn.Length = 8; dbfColumn.DecimalLength = 0; } else if (dbfColumn.ColumnType == DbfColumnType.IntegerInBinary) { dbfColumn.Length = 4; dbfColumn.DecimalLength = 0; } } else { int columnLenght = column.MaxLength; int decimalLength = 0; switch (column.TypeName.ToUpperInvariant()) { case "DOUBLE": case "NUMERIC": columnLenght = columnLenght == 0 ? 10 : columnLenght; if (columnLenght < 4) { columnLenght = 10; } decimalLength = 4; break; case "DATE": case "DATETIME": columnLenght = columnLenght == 0 ? 10 : columnLenght; decimalLength = 0; break; case "INTEGER": case "INT": columnLenght = columnLenght == 0 ? 10 : columnLenght; decimalLength = 0; break; case "STRING": case "CHARACTER": columnLenght = columnLenght == 0 ? characterTypeLength : columnLenght; decimalLength = 0; break; case "LOGICAL": columnLenght = 5; decimalLength = 0; break; } DbfColumnType type = DbfColumnType.Character; if (column.TypeName.Equals("DOUBLE", StringComparison.InvariantCultureIgnoreCase)) { column.TypeName = DbfColumnType.Float.ToString(); } if (column.TypeName.Equals("INT", StringComparison.InvariantCultureIgnoreCase)) { column.TypeName = DbfColumnType.Numeric.ToString(); } bool isSuccess = Enum.TryParse <DbfColumnType>(column.TypeName, true, out type); if (!isSuccess) { type = DbfColumnType.Character; } dbfColumn = new DbfColumn(column.ColumnName, type, columnLenght, decimalLength); dbfColumn.TypeName = column.TypeName; dbfColumn.MaxLength = column.MaxLength; } //Feature firstFeature = featureLayerStructureParameters.AddedFeatures.FirstOrDefault(); ////This is to fix that fox pro columns cannot write to dbf, convert all linked columns to character column type. //string tempColumnName = column.ColumnName; //if (oldNewNames.ContainsValue(column.ColumnName)) //{ // tempColumnName = oldNewNames.FirstOrDefault(f => f.Value == column.ColumnName).Key; //} //if (tempColumnName.Contains(".") && firstFeature != null && firstFeature.LinkColumnValues.ContainsKey(tempColumnName)) //{ // if (dbfColumn.ColumnType != DbfColumnType.Memo) // { // dbfColumn.ColumnType = DbfColumnType.Character; // dbfColumn.Length = characterTypeLength; // dbfColumn.DecimalLength = 0; // } //} dbfColumns[dbfColumn.ColumnName] = dbfColumn; } } bool convertMemoToCharacter = featureLayerStructureParameters.MemoColumnConvertMode == MemoColumnConvertMode.ToCharacter; Dictionary <string, int> columnLength = new Dictionary <string, int>(); foreach (var feature in addedFeatures) { //foreach (var linkColumnValue in feature.LinkColumnValues) //{ // if (!feature.ColumnValues.ContainsKey(linkColumnValue.Key)) // { // string[] values = linkColumnValue.Value.Select(v => // { // if (v.Value == null) // { // return string.Empty; // } // if (v.Value is DateTime) // { // return ((DateTime)v.Value).ToShortDateString(); // } // return v.Value.ToString(); // }).ToArray(); // if (values.All(v => string.IsNullOrEmpty(v) || string.IsNullOrWhiteSpace(v))) // { // if (oldNewNames.ContainsKey(linkColumnValue.Key)) // feature.ColumnValues[oldNewNames[linkColumnValue.Key]] = string.Empty; // else // feature.ColumnValues[linkColumnValue.Key] = string.Empty; // } // else // { // string tempColumName = linkColumnValue.Key; // if (oldNewNames.ContainsKey(linkColumnValue.Key)) // { // tempColumName = oldNewNames[linkColumnValue.Key]; // } // string linkValue = string.Join(",", values); // feature.ColumnValues[tempColumName] = linkValue; // //Choose the max length // if (columnLength.ContainsKey(tempColumName)) // { // if (columnLength[tempColumName] < linkValue.Length) // { // columnLength[tempColumName] = linkValue.Length; // } // } // else // { // columnLength[tempColumName] = linkValue.Length > 254 ? 254 : linkValue.Length; // } // } // } //} foreach (var item in oldNewNames) { if (feature.ColumnValues.ContainsKey(item.Key)) { feature.ColumnValues[oldNewNames[item.Key]] = feature.ColumnValues[item.Key]; feature.ColumnValues.Remove(item.Key); } } if (!convertMemoToCharacter) { foreach (var item in feature.ColumnValues) { if (item.Value.Length > characterTypeLength && dbfColumns[item.Key].ColumnType != DbfColumnType.Memo) { dbfColumns[item.Key].ColumnType = DbfColumnType.Memo; dbfColumns[item.Key].Length = 4; dbfColumns[item.Key].DecimalLength = 0; } } } } foreach (var column in dbfColumns) { Feature firstFeature = featureLayerStructureParameters.AddedFeatures.FirstOrDefault(); //This is to fix that fox pro columns cannot write to dbf, convert all linked columns to character column type. string tempColumnName = column.Key; if (oldNewNames.ContainsValue(column.Key)) { tempColumnName = oldNewNames.FirstOrDefault(f => f.Value == column.Key).Key; } //if (tempColumnName.Contains(".") && firstFeature != null && firstFeature.LinkColumnValues.ContainsKey(tempColumnName)) //{ // if (column.Value.ColumnType != DbfColumnType.Memo) // { // column.Value.ColumnType = DbfColumnType.Character; // //column.Value.Length = characterTypeLength; // column.Value.DecimalLength = 0; // if (columnLength.ContainsKey(tempColumnName) && column.Value.Length < columnLength[tempColumnName]) // { // column.Value.Length = columnLength[tempColumnName]; // } // } //} } ShapeFileFeatureLayer.CreateShapeFile(shapeFileType, featureLayerStructureParameters.LayerUri.OriginalString, dbfColumns.Values, DefaultEncoding, OverwriteMode.Overwrite); string encodingPathFileName = Path.ChangeExtension(featureLayerStructureParameters.LayerUri.OriginalString, ".cpg"); if (File.Exists(encodingPathFileName)) { File.Delete(encodingPathFileName); } File.WriteAllText(encodingPathFileName, DefaultEncoding.CodePage.ToString(CultureInfo.InvariantCulture)); string prjPath = Path.ChangeExtension(featureLayerStructureParameters.LayerUri.OriginalString, "prj"); File.WriteAllText(prjPath, Proj4Projection.ConvertProj4ToPrj(featureLayerStructureParameters.Proj4ProjectionParametersString)); ShapeFileFeatureLayer resultLayer = new ShapeFileFeatureLayer(featureLayerStructureParameters.LayerUri.LocalPath, GeoFileReadWriteMode.ReadWrite); if (addedFeatures.Count > 0) { resultLayer.Open(); resultLayer.EditTools.BeginTransaction(); foreach (var feature in addedFeatures) { if (convertMemoToCharacter) { foreach (var item in dbfColumns) { if (feature.ColumnValues.ContainsKey(item.Key) && feature.ColumnValues[item.Key].Length > 254) { feature.ColumnValues[item.Key] = feature.ColumnValues[item.Key].Substring(0, 254); } if (feature.ColumnValues.ContainsKey(item.Key) && feature.ColumnValues[item.Key].Length > item.Value.MaxLength) { feature.ColumnValues[item.Key] = feature.ColumnValues[item.Key].Substring(0, item.Value.MaxLength); } } } resultLayer.EditTools.Add(feature); } resultLayer.EditTools.CommitTransaction(); resultLayer.Close(); } return(resultLayer); }
protected override FeatureLayer CreateFeatureLayerCore(ConfigureFeatureLayerParameters featureLayerStructureParameters) { string layerPath = LayerPluginHelper.GetLayerUriToSave(featureLayerStructureParameters.LayerUri, ExtensionFilter); if (string.IsNullOrEmpty(layerPath)) { return(null); } featureLayerStructureParameters.LayerUri = new Uri(layerPath); Collection <TabDbfColumn> tabDbfColumns = new Collection <TabDbfColumn>(); foreach (var column in featureLayerStructureParameters.AddedColumns) { var columnLenght = column.MaxLength; var decimalLength = 0; switch (column.TypeName.ToUpperInvariant()) { case "DOUBLE": columnLenght = columnLenght == 0 ? 10 : columnLenght; decimalLength = 4; break; case "DATE": case "DATETIME": columnLenght = columnLenght == 0 ? 10 : columnLenght; decimalLength = 0; break; case "INTEGER": case "INT": columnLenght = columnLenght == 0 ? 10 : columnLenght; decimalLength = 0; break; case "STRING": columnLenght = columnLenght == 0 ? 255 : columnLenght; decimalLength = 0; break; } tabDbfColumns.Add(new TabDbfColumn(column.ColumnName, (DbfColumnType)Enum.Parse(typeof(DbfColumnType), column.TypeName, true), columnLenght, decimalLength, false, false)); } TabFeatureLayer.CreateTabFile(featureLayerStructureParameters.LayerUri.OriginalString, tabDbfColumns, new Collection <Feature>(), OverwriteMode.Overwrite); TabFeatureLayer.BuildIndexFile(featureLayerStructureParameters.LayerUri.OriginalString); string prjPath = Path.ChangeExtension(featureLayerStructureParameters.LayerUri.OriginalString, "prj"); File.WriteAllText(prjPath, Proj4Projection.ConvertProj4ToPrj(featureLayerStructureParameters.Proj4ProjectionParametersString)); var resultLayer = new TabFeatureLayer(featureLayerStructureParameters.LayerUri.LocalPath, GeoFileReadWriteMode.ReadWrite); if (featureLayerStructureParameters.AddedFeatures.Count > 0) { resultLayer.Open(); resultLayer.EditTools.BeginTransaction(); foreach (var feature in featureLayerStructureParameters.AddedFeatures) { resultLayer.EditTools.Add(feature); } resultLayer.EditTools.CommitTransaction(); resultLayer.Close(); } return(resultLayer); }