Exemplo n.º 1
0
 private void OpenCommandBinding_Executed(object sender, ExecutedRoutedEventArgs e)
 {
     if (openFileDialog.ShowDialog() == true)
     {
         try
         {
             Layer layer = new Layer();
             layer.Name     = Path.GetFileNameWithoutExtension(openFileDialog.SafeFileName);
             layer.FileName = openFileDialog.FileName;
             using (var dataReader = new ShapefileDataReader(openFileDialog.FileName, new GeometryFactory()))
             {
                 layer.Header            = dataReader.DbaseHeader;
                 layer.ShapeGeometryType = dataReader.ShapeHeader.ShapeType;
                 int numRecords = dataReader.DbaseHeader.NumRecords;
                 int length     = dataReader.DbaseHeader.NumFields;
                 for (int j = 0; j < numRecords; j++)
                 {
                     dataReader.Read();
                     ShapefileShape shape = new ShapefileShape(dataReader.Geometry);
                     for (int i = 0; i < length; i++)
                     {
                         shape.Attributes.Add(new ShapefileAttributeEntry(layer.Attributes[i], dataReader.GetValue(i + 1)));
                     }
                     layer.Shapes.Add(shape);
                 }
                 Layers.Add(layer);
             }
         }
         catch (Exception ex)
         {
             Console.WriteLine(ex);
         }
     }
 }
Exemplo n.º 2
0
        private void NewCommandBinding_Executed(object sender, ExecutedRoutedEventArgs e)
        {
            Layer currentLayer;

            switch ((string)e.Parameter)
            {
            case "layer":
                NewLayerDialog newLayerDialog = new NewLayerDialog();
                if (newLayerDialog.ShowDialog() == true)
                {
                    Layer           newLayer = new Layer();
                    DbaseFileReader reader   = new DbaseFileReader(
                        new NetTopologySuite.IO.Streams.ByteStreamProvider(
                            NetTopologySuite.IO.Streams.StreamTypes.Data,
                            new MemoryStream(Properties.Resources.EmptyDbaseHeader1)));
                    newLayer.Header    = reader.GetHeader();
                    newLayer.Name      = newLayerDialog.LayerName;
                    newLayer.ShapeType = newLayerDialog.ShapeType;
                    Layers.Add(newLayer);
                }
                break;

            case "field":
                currentLayer = (Layer)CollectionViewSource.GetDefaultView(Layers).CurrentItem;
                currentLayer.Header.AddColumn(newFieldName.Text, ((string)((ComboBoxItem)newFieldType.SelectedItem).Tag)[0], newFieldLength.Value ?? 0, newFieldDecimals.Value ?? 0);
                DbaseFieldDescriptor descriptor = currentLayer.Header.Fields.Last();
                currentLayer.Attributes.Add(descriptor);
                object newValue = null;
                var    @switch  = new Dictionary <Type, Action> {
                    { typeof(Int32), () => newValue = 0 },
                    { typeof(Double), () => newValue = 0.0 },
                    { typeof(String), () => newValue = "" },
                    { typeof(DateTime), () => newValue = DateTime.UtcNow },
                    { typeof(Boolean), () => newValue = false },
                };
                @switch[descriptor.Type]();
                foreach (ShapefileShape shape in currentLayer.Shapes)
                {
                    shape.Attributes.Add(new ShapefileAttributeEntry(descriptor, newValue));
                }
                break;

            case "shape":
                currentLayer = (Layer)CollectionViewSource.GetDefaultView(Layers).CurrentItem;
                ShapefileShape newShape = new ShapefileShape(currentLayer.ShapeType);
                newShape.CreateAttributes(currentLayer.Attributes);
                currentLayer.Shapes.Add(newShape);
                break;

            default:
                break;
            }
        }