示例#1
0
        private bool InitMap(AxMap axMap1, string dataPath)
        {
            axMap1.Projection             = tkMapProjection.PROJECTION_GOOGLE_MERCATOR;
            axMap1.GrabProjectionFromData = true;
            axMap1.ZoomBehavior           = tkZoomBehavior.zbUseTileLevels;

            axMap1.DisableWaitCursor = true;

            string filename1 = dataPath + "buildings.shp";
            string filename2 = dataPath + "roads.shp";


            if (!File.Exists(filename1) || !File.Exists(filename2))
            {
                MessageBox.Show("Couldn't find the files (buildings.shp, roads.shp): " + dataPath);
                return(false);
            }

            Shapefile sf = new Shapefile();

            sf.Open(filename1, null);
            axMap1.AddLayer(sf, true);

            Debug.Print(axMap1.GeoProjection.ExportToWKT());

            sf = new Shapefile();
            sf.Open(filename2, null);
            sf.Labels.Generate("[Name]", tkLabelPositioning.lpLongestSegement, false);

            axMap1.ZoomToMaxExtents();
            return(true);
        }
        // <summary>
        // Split a shapefile into several ones according the values the specified attribute.
        // </summary>
        public void SplitByAttribute(AxMap axMap1, string dataPath)
        {
            axMap1.Projection             = tkMapProjection.PROJECTION_NONE;
            axMap1.GrabProjectionFromData = true;

            string filename = dataPath + "natural.shp";

            if (!File.Exists(filename))
            {
                MessageBox.Show("Couldn't file the file: " + filename);
            }
            else
            {
                Shapefile sf = new Shapefile();
                sf.Open(filename, null);

                int fieldIndex = sf.Table.FieldIndexByName["type"];
                sf.Categories.Generate(fieldIndex, tkClassificationType.ctUniqueValues, 0);
                sf.Categories.ApplyExpressions();

                ColorScheme scheme = new ColorScheme();
                scheme.SetColors2(tkMapColor.White, tkMapColor.Black);

                for (int i = 0; i < sf.Categories.Count; i++)
                {
                    Shapefile sfNew       = sf.Clone();
                    int       layerHandle = axMap1.AddLayer(sfNew, true);

                    for (int shapeIndex = 0; shapeIndex < sf.NumShapes; shapeIndex++)
                    {
                        if (sf.ShapeCategory[shapeIndex] == i)
                        {
                            Shape shape = sf.Shape[shapeIndex].Clone();
                            int   index = sfNew.NumShapes;
                            sfNew.EditInsertShape(shape, ref index);
                        }
                    }

                    ShapefileCategory category = sf.Categories.Item[i];
                    string            name     = category.Name.Substring(category.Name.IndexOf("=") + 1);

                    uint color = scheme.get_RandomColor((i + 1) / sf.Categories.Count);
                    sfNew.DefaultDrawingOptions.FillColor = color;

                    axMap1.set_LayerName(layerHandle, name);

                    //sfNew.SaveAs(path + name + ".shp", null);    // saves shapefile
                }
                ShowLegend();
                axMap1.ZoomToMaxExtents();
                axMap1.Redraw();
            }
        }
        // <summary>
        // Performs a segmentation of shapes by regular grid
        // </summary>
        public void Segmentation(AxMap axMap1, string dataPath)
        {
            axMap1.Projection             = tkMapProjection.PROJECTION_NONE;
            axMap1.GrabProjectionFromData = true;

            string[] filenames = new string[2];
            filenames[0] = dataPath + "natural.shp";
            filenames[1] = dataPath + "landuse.shp";

            if (!File.Exists(filenames[0]) || !File.Exists(filenames[1]))
            {
                MessageBox.Show("Couldn't file the files (natural.shp, landuse.shp): " + dataPath);
                return;
            }

            List <Shapefile> layers = new List <Shapefile>();

            for (int i = 0; i < filenames.Length; i++)
            {
                Shapefile sf = new Shapefile();
                sf.Open(filenames[i], null);
                layers.Add(sf);
                axMap1.AddLayer(layers[i], true);
            }

            int     count = 4;
            Extents ext   = axMap1.MaxExtents as Extents;
            double  xStep = (ext.xMax - ext.xMin) / count;
            double  yStep = (ext.yMax - ext.yMin) / count;

            Shapefile sfGrid = new Shapefile();

            sfGrid.CreateNewWithShapeID("", ShpfileType.SHP_POLYGON);
            sfGrid.GeoProjection.CopyFrom(layers[0].GeoProjection);

            ColorScheme scheme = new ColorScheme();

            scheme.SetColors2(tkMapColor.Orange, tkMapColor.Yellow);

            for (int i = 0; i < layers.Count; i++)
            {
                string name       = Path.GetFileNameWithoutExtension(layers[i].Filename);
                int    fieldIndex = sfGrid.EditAddField(name, FieldType.DOUBLE_FIELD, 10, 12);
                uint   color      = scheme.GraduatedColor[(i + 1) / (double)layers.Count];
                sfGrid.Charts.AddField2(fieldIndex, color);
            }

            for (int i = 0; i < count; i++)
            {
                for (int j = 0; j < count; j++)
                {
                    Shape shp = new Shape();
                    shp.Create(ShpfileType.SHP_POLYGON);
                    shp.AddPoint(ext.xMin + i * xStep, ext.yMin + j * yStep);
                    shp.AddPoint(ext.xMin + i * xStep, ext.yMin + (j + 1) * yStep);
                    shp.AddPoint(ext.xMin + (i + 1) * xStep, ext.yMin + (j + 1) * yStep);
                    shp.AddPoint(ext.xMin + (i + 1) * xStep, ext.yMin + j * yStep);
                    shp.AddPoint(ext.xMin + i * xStep, ext.yMin + j * yStep);
                    int shapeIndex = sfGrid.EditAddShape(shp);

                    for (int layer = 0; layer < layers.Count; layer++)
                    {
                        Shapefile sf   = layers[layer];
                        double    area = 0.0;
                        for (int n = 0; n < sf.NumShapes; n++)
                        {
                            Shape shp2 = sf.Shape[n];
                            if (shp.Intersects(shp2))
                            {
                                Shape shpResult = shp2.Clip(shp, tkClipOperation.clIntersection);
                                if (shpResult != null)
                                {
                                    area += shpResult.Area;
                                }
                            }
                        }

                        // divide by 10000.0 to convert square meters to hectars
                        bool success = sfGrid.EditCellValue(layer + 1, shapeIndex, area / 10000.0);
                    }
                }
            }

            // generating charts
            Charts charts = sfGrid.Charts;

            charts.Generate(tkLabelPositioning.lpCenter);
            charts.ChartType     = tkChartType.chtPieChart;
            charts.PieRadius     = 18;
            charts.Thickness     = 10;
            charts.ValuesVisible = true;
            charts.Visible       = true;

            ShapeDrawingOptions options = sfGrid.DefaultDrawingOptions;

            options.FillVisible = false;
            options.LineWidth   = 2;
            options.LineStipple = tkDashStyle.dsDash;
            options.LineColor   = 255;

            axMap1.AddLayer(sfGrid, true);
            axMap1.ZoomToMaxExtents();
        }
        // <summary>
        // Selects buildings which lie within specified distance from the parks.
        // </summary>
        public void SelectByDistance(AxMap axMap1, string dataPath)
        {
            axMap1.Projection             = tkMapProjection.PROJECTION_NONE;
            axMap1.GrabProjectionFromData = true;

            string filename1 = dataPath + "buildings.shp";
            string filename2 = dataPath + "natural.shp";

            if (!File.Exists(filename1) || !File.Exists(filename2))
            {
                MessageBox.Show("Failed to open shapefile (natural.shp, buildings.shp): " + dataPath);
                return;
            }

            var sfBuildings = new Shapefile();

            sfBuildings.Open(filename1, null);

            var sfParks = new Shapefile();

            sfParks.Open(filename2, null);
            ShapefileCategory ct = sfParks.Categories.Add("Parks");

            // choose parks and make them green
            ct.Expression = "[Type] = \"Park\"";
            var utils = new Utils();

            ct.DrawingOptions.FillColor = utils.ColorByName(tkMapColor.Green);
            sfParks.Categories.ApplyExpression(0);

            // hide the rest types of objects on the layer
            sfParks.DefaultDrawingOptions.Visible = false;

            double maxDistance = 150.0;        // in meters

            bool editing = sfBuildings.StartEditingShapes(true, null);

            sfBuildings.UseQTree = true;   // this will build a spatial index to speed up selection

            for (int i = 0; i < sfParks.NumShapes; i++)
            {
                int index = sfParks.ShapeCategory[i];
                if (index == 0)
                {
                    object result = null;
                    Shape  shp    = sfParks.Shape[i];
                    if (sfBuildings.SelectShapes(shp.Extents, maxDistance, SelectMode.INTERSECTION, ref result))
                    {
                        int[] shapes = result as int[];
                        if (shapes == null)
                        {
                            return;
                        }
                        for (int j = 0; j < shapes.Length; j++)
                        {
                            if (!sfBuildings.ShapeSelected[shapes[j]])
                            {
                                Shape  shp2 = sfBuildings.Shape[shapes[j]];
                                double dist = shp.Distance(shp2);
                                if (dist < maxDistance)
                                {
                                    sfBuildings.set_ShapeSelected(shapes[j], true);
                                }
                            }
                        }
                    }
                }
            }

            axMap1.AddLayer(sfParks, true);
            axMap1.AddLayer(sfBuildings, true);
            axMap1.ZoomToMaxExtents();
        }
        // <summary>
        // Calculates the length of intersection of rivers and land parcels
        // </summary>
        public void IntersectionLength(AxMap axMap1, ToolStripStatusLabel label, string dataPath)
        {
            axMap1.Projection             = tkMapProjection.PROJECTION_NONE;
            axMap1.GrabProjectionFromData = true;

            string filename1 = dataPath + "landuse.shp";
            string filename2 = dataPath + "waterways.shp";

            if (!File.Exists(filename1) || !File.Exists(filename2))
            {
                MessageBox.Show("The necessary files (waterways.shp, building.shp) are missing: " + dataPath);
            }
            else
            {
                Shapefile sfParcels = new Shapefile();
                sfParcels.Open(filename1, null);
                sfParcels.StartEditingShapes(true, null);

                Field field = new Field {
                    Name = "Length", Type = FieldType.DOUBLE_FIELD, Precision = 10
                };
                int fieldIndex = sfParcels.NumShapes;
                sfParcels.EditInsertField(field, ref fieldIndex, null);

                Shapefile sfRivers = new Shapefile();
                sfRivers.Open(filename2, null);
                sfRivers.StartEditingShapes(true, null);
                Utils utils = new Utils();
                sfRivers.DefaultDrawingOptions.LineWidth = 2;
                sfRivers.DefaultDrawingOptions.LineColor = utils.ColorByName(tkMapColor.Blue);

                Shapefile           sfNew   = sfRivers.Clone();
                ShapeDrawingOptions options = sfNew.DefaultDrawingOptions;

                LinePattern pattern = new LinePattern();
                pattern.AddLine(utils.ColorByName(tkMapColor.Blue), 8, tkDashStyle.dsSolid);
                pattern.AddLine(utils.ColorByName(tkMapColor.LightBlue), 4, tkDashStyle.dsSolid);
                options.LinePattern    = pattern;
                options.UseLinePattern = true;

                for (int i = 0; i < sfParcels.NumShapes; i++)
                {
                    Shape  shp1   = sfParcels.Shape[i];
                    double length = 0.0;    // the length of intersection

                    for (int j = 0; j < sfRivers.NumShapes; j++)
                    {
                        Shape shp2 = sfRivers.Shape[j];
                        if (shp1.Intersects(shp2))
                        {
                            Shape result = shp1.Clip(shp2, tkClipOperation.clIntersection);
                            if (result != null)
                            {
                                int index = sfNew.EditAddShape(result);
                                length += result.Length;
                            }
                        }
                    }
                    sfParcels.EditCellValue(fieldIndex, i, length);
                    label.Text = string.Format("Parcel: {0}/{1}", i + 1, sfParcels.NumShapes);
                    Application.DoEvents();
                }

                // generating charts
                var chartField = new ChartField();
                chartField.Name  = "Length";
                chartField.Color = utils.ColorByName(tkMapColor.LightBlue);
                chartField.Index = fieldIndex;
                sfParcels.Charts.AddField(chartField);
                sfParcels.Charts.Generate(tkLabelPositioning.lpInteriorPoint);
                sfParcels.Charts.ChartType     = tkChartType.chtBarChart;
                sfParcels.Charts.BarHeight     = 100;
                sfParcels.Charts.ValuesVisible = true;
                sfParcels.Charts.Visible       = true;

                axMap1.AddLayer(sfParcels, true);
                axMap1.AddLayer(sfRivers, true);
                axMap1.AddLayer(sfNew, true);
                axMap1.ZoomToMaxExtents();
            }
        }