Shapefile editing

May 20, 2011 at 1:52 PM
Edited May 20, 2011 at 1:53 PM

Hello!

I'm trying to edit polygons in shape-file. For exapmle I need to change coordinates of one point in polygon and then refresh the map (and redraw the polygon).

In my code I load the shape-file, "Map1MouseDown" method is used to select one point in the polygon, it's works well. "Map1MouseUp" changes point's coordinates.

I use ((Map)sender).Refresh(); method at the end of "Map1MouseUp", but it doesn't refresh the map.

Here is my code:

 

using System;
using System.Collections;
using System.Collections.Generic;
using System.Data;
using System.Drawing;
using System.Windows.Forms;
using DotSpatial.Controls;
using DotSpatial.Data;
using DotSpatial.Projections;
using DotSpatial.Topology;
using Point = System.Drawing.Point;


namespace DotspartialWinFormByVadya84
{
    public partial class Form1 : Form
    {

        private bool _flag;
        private IFeatureSet fs1;
        List<Coordinate> _listCoord = new List<Coordinate>();
        private Coordinate _startCoordinate;
        private Coordinate _endCoordinate;
        private IFeature _currentFeature;

        public Form1()
        {
            InitializeComponent();
            map1.Layers.Add(@"C:\dbSql\emtsa\emtsa.shp");
        }

        private bool IsPointInArea(IFeature area, Point point)
        {
            return area.Intersects(map1.PixelToProj(point));
        }

        private void Map1MouseDown(object sender, MouseEventArgs e)
        {
            IMapFeatureLayer[] layer = ((Map)sender).GetFeatureLayers();
            if (layer != null)
                foreach (IMapFeatureLayer featureLayer in layer)
                {
                    if (featureLayer as MapPolygonLayer != null)
                    {
                        var dataSet = ((MapPolygonLayer)featureLayer).DataSet;
                        if (dataSet != null)
                            foreach (IFeature feature in dataSet.Features)
                            {
                                if (IsPointInArea(feature, e.Location))
                                {
                                    _currentFeature = feature;
                                    IBasicGeometry basicGeometry = feature.BasicGeometry;
                                    IList<Coordinate> coordinates = basicGeometry.Coordinates;
                                    foreach (Coordinate coordinate in coordinates)
                                    {
                                        Point pixel = map1.ProjToPixel(coordinate);
                                        Point point = map1.ProjToPixel(map1.PixelToProj(e.Location));
                                        if (point.X > pixel.X - 3 && point.X < pixel.X + 3 && point.Y > pixel.Y - 3 &&
                                            point.Y < pixel.Y + 3)
                                        {
                                            _startCoordinate = coordinate;
                                            Cursor = Cursors.SizeAll;
                                            return;
                                        }
                                    }
                                }
                            }
                    }
                }
        }

        private void Map1MouseUp(object sender, MouseEventArgs e)
        {
            if (_startCoordinate != null)
            {
                IMapFeatureLayer[] layer = ((Map)sender).GetFeatureLayers();
                if (layer != null)
                    foreach (IMapFeatureLayer featureLayer in layer)
                    {
                        if (featureLayer as MapPolygonLayer != null)
                        {
                            IFeatureSet dataSet = ((MapPolygonLayer)featureLayer).DataSet;
                            if (dataSet != null)
                                foreach (IFeature feature in dataSet.Features)
                                {
                                    if (feature == _currentFeature)
                                    {
                                        IBasicGeometry basicGeometry = feature.BasicGeometry;
                                        IList<Coordinate> coordinates = basicGeometry.Coordinates;
                                        foreach (Coordinate coordinate in coordinates)
                                        {
                                            if (coordinate == _startCoordinate)
                                            {
                                                Coordinate toProj = map1.PixelToProj(e.Location);
                                                coordinate.X = toProj.X;
                                                coordinate.Y = toProj.Y;
                                                _startCoordinate = null;
                                            }

                                        }
                                        dataSet.Save();
                                    }
                                }
                    ((Map)sender).Refresh(); // !
                        }
                    }
                Cursor = Cursors.Default;
            }

        }
    }
} 

Can you help me?