示例#1
0
 public void CopySettingsTo(CSVModel targetModel)
 {
     targetModel.CsvFeatureLayer.Delimiter               = Delimiter;
     targetModel.CsvFeatureLayer.XColumnName             = LongitudeColumnName;
     targetModel.CsvFeatureLayer.YColumnName             = LatitudeColumnName;
     targetModel.CsvFeatureLayer.WellKnownTextColumnName = WktColumnName;
     targetModel.CsvFeatureLayer.SpatialColumnType       = MappingType;
 }
        public CSVViewModel(Collection <CSVInfoModel> entities)
        {
            this.csvModelList = new List <CSVModel>();

            foreach (CSVInfoModel entity in entities)
            {
                CsvFeatureLayer layer = new CsvFeatureLayer(entity.CSVFileName, entity.WktColumnName, entity.Delimiter);
                layer.YColumnName       = entity.LatitudeColumnName;
                layer.XColumnName       = entity.LongitudeColumnName;
                layer.SpatialColumnType = entity.MappingType;
                CSVModel model = new CSVModel(layer);
                model.PropertyChanged += (seder, e) =>
                {
                    if (e.PropertyName == "Delimiter")
                    {
                        RefreshColumnNamesAndFieldDatas();
                    }
                };

                this.csvModelList.Add(model);
            }

            currentCSVColumnNames = new ObservableCollection <string>();
            emptyDataTable        = new DataTable();
            emptyDataTable.Columns.Add("Error");
            DataRow dr = emptyDataTable.NewRow();

            dr[0] = "Invalid Delimiter.";
            emptyDataTable.Rows.Add(dr);
            pageSize   = 25;
            pageNumber = 1;

            SelectedCSVModel = this.csvModelList[0];
            SelectedCSVModel.CanAutoMatch = false;
            switch (SelectedCSVModel.Delimiter)
            {
            case ",":
                SelectedDelimiter = new KeyValuePair <string, string>("Comma", ",");
                break;

            case ".":
                SelectedDelimiter = new KeyValuePair <string, string>("Dot", ".");
                break;

            case "|":
                SelectedDelimiter = new KeyValuePair <string, string>("Pipe", "|");
                break;

            default:
                CustomDelimiter   = SelectedCSVModel.Delimiter;
                SelectedDelimiter = new KeyValuePair <string, string>("Custom", "");
                break;
            }
            SelectedCSVModel.CanAutoMatch = true;
        }
        public static CSVInfoModel FromCSVModel(CSVModel model)
        {
            CSVInfoModel entity = new CSVInfoModel();

            if (model != null)
            {
                entity.CSVFileName         = model.CsvFeatureLayer.DelimitedPathFilename;
                entity.Delimiter           = model.Delimiter;
                entity.LatitudeColumnName  = model.LatitudeColumnName;
                entity.LongitudeColumnName = model.LongitudeColumnName;
                entity.MappingType         = model.MappingType;
                entity.WktColumnName       = model.WktColumnName;
                return(entity);
            }
            return(entity);
        }
        protected override Collection <Layer> GetLayersCore(GetLayersParameters getLayersParameters)
        {
            Collection <Layer> resultLayers = base.GetLayersCore(getLayersParameters);

            Collection <CSVInfoModel> entities = new Collection <CSVInfoModel>();
            bool hasUnconfiguredCsv            = false;

            foreach (string fileName in getLayersParameters.LayerUris.Select(u => u.LocalPath))
            {
                string configFileName = Path.ChangeExtension(fileName, Path.GetExtension(fileName) + ".config");
                if (File.Exists(configFileName))
                {
                    var csvFileLastWriteTime = new FileInfo(fileName).LastWriteTime;
                    var csvConfLastWriteTime = GetConfigFileLastWriteTime(fileName);

                    if (csvFileLastWriteTime.ToString(CultureInfo.InvariantCulture).Equals(csvConfLastWriteTime.ToString(CultureInfo.InvariantCulture), StringComparison.OrdinalIgnoreCase))
                    {
                        entities.Add(CSVInfoModel.FromConfig(configFileName));
                    }
                    else
                    {
                        CleanAttachedFiles(fileName, configFileName);
                        entities.Add(new CSVInfoModel(fileName, ","));
                        hasUnconfiguredCsv = true;
                    }
                }
                else
                {
                    entities.Add(new CSVInfoModel(fileName, ","));
                    hasUnconfiguredCsv = true;
                }
            }

            try
            {
                if (entities.Count == 0)
                {
                    return(resultLayers);
                }

                List <CSVModel> csvModels = new List <CSVModel>();
                if (hasUnconfiguredCsv)
                {
                    var             viewModel = new CSVViewModel(entities);
                    CSVConfigWindow window    = new CSVConfigWindow(viewModel);
                    viewModel.SelectedCSVModel.AutoMatch();
                    if (window.ShowDialog().GetValueOrDefault())
                    {
                        window.CSVModelList.ForEach(csvModels.Add);
                    }
                }
                else
                {
                    foreach (var csvModel in new CSVViewModel(entities).CSVModelList)
                    {
                        csvModels.Add(csvModel);
                    }
                }

                csvModels.ForEach(m => resultLayers.Add(m.CsvFeatureLayer));
                foreach (CsvFeatureLayer delimitedFeatureLayer in resultLayers.OfType <CsvFeatureLayer>())
                {
                    delimitedFeatureLayer.FeatureSource.CommittedTransaction -= delegate { CSVModel.BuildDelimitedConfigurationFile(delimitedFeatureLayer); };
                    delimitedFeatureLayer.FeatureSource.CommittedTransaction += delegate { CSVModel.BuildDelimitedConfigurationFile(delimitedFeatureLayer); };
                }
            }
            catch (Exception ex)
            {
                GisEditor.LoggerManager.Log(LoggerLevel.Debug, ex.Message, new ExceptionInfo(ex));
                System.Windows.Forms.MessageBox.Show(ex.Message, "Warning", System.Windows.Forms.MessageBoxButtons.OK, System.Windows.Forms.MessageBoxIcon.Warning);
            }

            return(resultLayers);
        }
        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);
        }