Esempio n. 1
0
        /// <summary>
        /// Creates a MapWinGIS.Shape object from the point data specified by the user.
        /// </summary>
        /// <returns>Returns a MapWinGIS.Shape object.</returns>
        public MapWinGIS.Shape ToMWShape(MapWinGIS.ShpfileType type)
        {
            if (m_Points.Count == 0)
            {
                return(null);
            }

            MapWinGIS.Shape shp = new MapWinGIS.ShapeClass();
            MapWinGIS.Point pnt;

            int partNum    = 0;
            int pointIndex = 0;

            bool bres;

            bres = shp.Create(type);
            bres = shp.InsertPart(0, ref partNum);

            for (int i = 0; i < m_Points.Count; i++)
            {
                pnt        = new MapWinGIS.PointClass();
                pnt.x      = ((PointD)m_Points[i]).x;
                pnt.y      = ((PointD)m_Points[i]).y;
                pointIndex = i;
                bres       = shp.InsertPoint(pnt, ref pointIndex);
                pnt        = null;
            }

            return(shp);
        }
Esempio n. 2
0
        /// <summary>
        /// Returns a MapWinGIS Multi-part PolyLine representing this multiLineString
        /// </summary>
        /// <returns>MapWinGIS Shape</returns>
        public static MapWinGIS.Shape mwShapeFromMultiLineString(MultiLineString MLS, MapWinGIS.ShpfileType sfType)
        {
            MapWinGIS.Shape mwShape = new MapWinGIS.Shape();
            mwShape.Create(sfType);
            int prt      = 0; // Part Index
            int pt       = 0; // Point Index
            int numParts = MLS.NumGeometries;
            int maxpt;

            while (prt < numParts)
            {
                LineString LS = MLS.GeometryN[prt] as LineString;
                maxpt = LS.Coordinates.Count;
                mwShape.InsertPart(pt, ref prt);
                for (int I = 0; I < maxpt; I++)
                {
                    MapWinGIS.Point mwPoint = new MapWinGIS.Point();
                    mwPoint.x = LS.Coordinates[I].X;
                    mwPoint.y = LS.Coordinates[I].Y;
                    bool result = mwShape.InsertPoint(mwPoint, ref pt);
                    if (result == false)
                    {
                        throw new ApplicationException(mwShape.get_ErrorMsg(mwShape.LastErrorCode));
                    }
                    pt++;
                }
                prt++;
            }
            return(mwShape);
        }
Esempio n. 3
0
        /// <summary>
        /// Merges two polygons together. If they touch, they will be UNIONed together,
        /// If they do not touch, they will be combined into one multi-part shape.
        /// </summary>
        /// <param name="poly1">The first polygon to be considered.</param>
        /// <param name="poly2">The second polygon to be considered.</param>
        /// <param name="sfType">The type of shapefile the polygons are a part of.</param>
        /// <param name="resultShp">The polygon shape that results after merging.</param>
        /// <returns>True if merging was successful, false otherwise.</returns>
        private static bool MergePolygons(ref MapWinGIS.Shape poly1, ref MapWinGIS.Shape poly2, MapWinGIS.ShpfileType sfType, out MapWinGIS.Shape resultShp)
        {
            MapWinGIS.Shape mShp = new MapWinGIS.ShapeClass();
            mShp.Create(sfType);

            if (Globals.CheckBounds(ref poly1, ref poly2))
            {
                //bounds overlap, try union
                Debug.WriteLine("Union operation is being performed.");
                mShp = SpatialOperations.Union(poly1, poly2);
                if (mShp.numPoints > 0)
                {
                    resultShp = mShp;
                    return(true);
                }
                else
                {
                    //even though bounds overlap, the polygons may not touch
                    //combine them into a multi-part polygon.
                    bool status = CombinePolygons(ref poly1, ref poly2, out mShp);
                    resultShp = mShp;
                    return(status);
                }
            }
            else
            {
                //Polygons don't overlap, try combining them into a multi-part polygon
                gErrorMsg = "Combining polygons into one multi-part shape.";
                Debug.WriteLine(gErrorMsg);
                bool status = CombinePolygons(ref poly1, ref poly2, out mShp);
                resultShp = mShp;
                MapWinUtility.Logger.Dbg(gErrorMsg);
                return(status);
            }
        }
Esempio n. 4
0
        /// <summary>
        /// Merges two lines (at matching end points) or two polygons
        /// (by dissolving the common border or by combining into a multi-part polygon)
        /// to make one result shape.
        /// This version requires that both shapes be located in the same shapefile.
        /// </summary>
        /// <param name="shapes">The shapefile containing the two shapes to be merged.</param>
        /// <param name="indexOne">The index of the first shape.</param>
        /// <param name="indexTwo">The index of the second shape.</param>
        /// <param name="resultShp">The result of merging the shapes at indexOne and indexTwo together.</param>
        /// <returns>True if the shapes were merged, false otherwise.</returns>
        public static bool MergeShapes(ref MapWinGIS.Shapefile shapes, int indexOne, int indexTwo, out MapWinGIS.Shape resultShp)
        {
            MapWinUtility.Logger.Dbg("MergeShapes(shapes: " + Macro.ParamName(shapes) + ",\n" +
                                     "            indexOne: " + indexOne.ToString() + ",\n" +
                                     "            indexTwo: " + indexTwo.ToString() + ",\n" +
                                     "            resultShp: resultShp)");
            Error.ClearErrorLog();
            MapWinGIS.ShpfileType sfType = shapes.ShapefileType;
            MapWinGIS.Shape       mShp   = new MapWinGIS.ShapeClass();
            mShp.Create(sfType);
            bool status = false;

            //having a point merge function is quite pointless...but, just in case it's needed!
            if (sfType == MapWinGIS.ShpfileType.SHP_POINT || sfType == MapWinGIS.ShpfileType.SHP_POLYGONM || sfType == MapWinGIS.ShpfileType.SHP_POLYGONZ)
            {
                MapWinGIS.Point pt1 = new MapWinGIS.PointClass();
                pt1 = shapes.QuickPoint(indexOne, 0);
                MapWinGIS.Point pt2 = new MapWinGIS.PointClass();
                pt2 = shapes.QuickPoint(indexTwo, 0);

                status = MergePoints(ref pt1, ref pt2, sfType, out mShp);

                while (Marshal.ReleaseComObject(pt1) != 0)
                {
                    ;
                }
                pt1 = null;
                while (Marshal.ReleaseComObject(pt2) != 0)
                {
                    ;
                }
                pt2 = null;

                resultShp = mShp;
                MapWinUtility.Logger.Dbg("Finished MergeShapes");
                return(status);
            }            //end of merging points

            //merge polygons
            else if (sfType == MapWinGIS.ShpfileType.SHP_POLYGON || sfType == MapWinGIS.ShpfileType.SHP_POLYGONM || sfType == MapWinGIS.ShpfileType.SHP_POLYGONZ)
            {
                MapWinGIS.Shape poly1 = new MapWinGIS.ShapeClass();
                poly1 = shapes.get_Shape(indexOne);
                MapWinGIS.Shape poly2 = new MapWinGIS.ShapeClass();
                poly2 = shapes.get_Shape(indexTwo);

                status = MergePolygons(ref poly1, ref poly2, sfType, out mShp);

                while (Marshal.ReleaseComObject(poly1) != 0)
                {
                    ;
                }
                poly1 = null;
                while (Marshal.ReleaseComObject(poly2) != 0)
                {
                    ;
                }
                poly2 = null;

                resultShp = mShp;
                MapWinUtility.Logger.Dbg("Finished MergeShapes");
                return(status);
            }            //end of merging polygons

            //Merge lines by joining them at common endpoints
            else if (sfType == MapWinGIS.ShpfileType.SHP_POLYLINE || sfType == MapWinGIS.ShpfileType.SHP_POLYLINEM || sfType == MapWinGIS.ShpfileType.SHP_POLYLINEZ)
            {
                MapWinGIS.Shape line1 = new MapWinGIS.ShapeClass();
                line1 = shapes.get_Shape(indexOne);
                MapWinGIS.Shape line2 = new MapWinGIS.ShapeClass();
                line2 = shapes.get_Shape(indexTwo);

                status = MergeLines(ref line1, ref line2, sfType, out mShp);
                if (line1 != null)
                {
                    while (Marshal.ReleaseComObject(line1) != 0)
                    {
                        ;
                    }
                }
                line1 = null;
                if (line2 != null)
                {
                    while (Marshal.ReleaseComObject(line2) != 0)
                    {
                        ;
                    }
                }
                line2 = null;

                resultShp = mShp;
                MapWinUtility.Logger.Dbg("Finished MergeShapes");
                return(status);
            }            //end of merging lines
            else
            {
                gErrorMsg = "Unknown shapefile type, aborting call to ShapeMerge.";
                Debug.WriteLine(gErrorMsg);
                Error.SetErrorMsg(gErrorMsg);
                resultShp = mShp;
                MapWinUtility.Logger.Dbg(gErrorMsg);
                return(false);
            }
        }
Esempio n. 5
0
        /// <summary>
        /// Will check to see if two points are identical.
        /// </summary>
        /// <param name="point1">The first point to consider.</param>
        /// <param name="point2">The second point to consider.</param>
        /// <param name="sfType">The type of shapefile the points belong to.</param>
        /// <param name="mergedShp">The result shape</param>
        /// <returns>True if the points were merged successfully, false otherwise.</returns>
        private static bool MergePoints(ref MapWinGIS.Point point1, ref MapWinGIS.Point point2, MapWinGIS.ShpfileType sfType, out MapWinGIS.Shape mergedShp)
        {
            MapWinUtility.Logger.Dbg("MergePoints(point1: " + Macro.ParamName(point1) + "\n," +
                                     "            point2: " + Macro.ParamName(point2) + "\n," +
                                     "            sfType: " + sfType.ToString() + "\n," +
                                     "            mergedShp: out)");

            MapWinGIS.Shape mShp = new MapWinGIS.ShapeClass();
            if (point1.x == point2.x && point1.y == point2.y && point1.Z == point2.Z)
            {
                mShp.Create(sfType);
                int ptIndex = 0;
                mShp.InsertPoint(point1, ref ptIndex);
            }
            mergedShp = mShp;
            if (mergedShp.numPoints > 0)
            {
                MapWinUtility.Logger.Dbg("Finished MergePoints");
                return(true);
            }
            else
            {
                gErrorMsg = "The points are not identical so they cannot be merged.";
                Debug.WriteLine(gErrorMsg);
                Error.SetErrorMsg(gErrorMsg);
                mergedShp = mShp;
                MapWinUtility.Logger.Dbg(gErrorMsg);
                return(false);
            }
        }
Esempio n. 6
0
        /// <summary>
        /// Merges two lines together if they share a common end point.
        /// </summary>
        /// <param name="line1">The first line to be considered.</param>
        /// <param name="line2">The second line to be considered.</param>
        /// <param name="sfType">The type of shapefile the lines are a part of.</param>
        /// <param name="resultShp">The two lines merged into one multipart line</param>
        /// <returns>True if merging was successful, false otherwise.</returns>
        private static bool MergeLines(ref MapWinGIS.Shape line1, ref MapWinGIS.Shape line2, MapWinGIS.ShpfileType sfType, out MapWinGIS.Shape resultShp)
        {
            MapWinGIS.Shape mShp = new MapWinGIS.ShapeClass();
            mShp.Create(sfType);
            //int pt = 0;
            //int prt = 0;
            if (line1 == null && line2 == null)
            {
                resultShp = mShp;
                return(false);
            }
            if (line1 == null && line2 != null)
            {
                resultShp = line2;
                return(false);
            }
            if (line2 == null)
            {
                resultShp = line1;
                return(false);
            }

            Topology2D.MultiLineString MLS1 = new MapWinGeoProc.Topology2D.MultiLineString(line1);
            Topology2D.MultiLineString MLS2 = new MapWinGeoProc.Topology2D.MultiLineString(line2);
            Topology2D.LineString      LS2;
            // Join the linestrings from MLS1 where possible
            int j = 0;

            while (j < MLS1.NumGeometries)
            {
                LS2 = MLS1.GeometryN[j] as Topology2D.LineString;
                Topology2D.LineString LS1;
                Topology2D.Geometry   outGeom;
                for (int i = 0; i < MLS1.NumGeometries; i++)
                {
                    if (i == j)
                    {
                        continue;
                    }
                    LS1     = MLS1.GeometryN[i] as Topology2D.LineString;
                    outGeom = LS1.Union(LS2);
                    // Attempt to join each linestring to linestrings in MLS1
                    if (outGeom != null)
                    {
                        MLS1.GeometryN[i] = outGeom;
                        MLS1.GeometryN.RemoveAt(j);
                        continue;
                        // don't increment j because we removed one instead
                    }
                }
                j++;
            }

            // Add each linestring to MLS1, merging into single linestrings where possible
            while (MLS2.NumGeometries > 0)
            {
                LS2 = MLS2.GeometryN[0] as Topology2D.LineString;
                Topology2D.LineString LS1;
                Topology2D.Geometry   outGeom;
                bool merged = false;
                for (int i = 0; i < MLS1.NumGeometries; i++)
                {
                    LS1     = MLS1.GeometryN[i] as Topology2D.LineString;
                    outGeom = LS1.Union(LS2);

                    // Attempt to join each linestring to linestrings in MLS1
                    if (outGeom != null)
                    {
                        // The endpoint merge was successful

                        MLS1.GeometryN[i] = outGeom;
                        merged            = true;
                        break;
                    }
                }
                // If they don't connect, add the linestring to the end of the list
                if (merged == false)
                {
                    // endPoint merging was not successful
                    MLS1.GeometryN.Add(LS2);
                }
                // either way, remove the line
                MLS2.GeometryN.RemoveAt(0);
            }
            resultShp = Topology2D.GeometryFactory.mwShapeFromMultiLineString(MLS1, sfType);
            return(true);
        }
        /// <summary>
        /// Returns a shapefile of points from the input shapefile that fall within the polygon.
        /// </summary>
        /// <param name="pointSFPath">Full path to the point shapefile.</param>
        /// <param name="polygon">The polygon used for clipping the point shapefile.</param>
        /// <param name="resultSFPath">Full path to where the resulting point shapefile should be saved.</param>
        /// <param name="copyAttributes">True if copying attributes over</param>
        /// <returns>False if an error was encountered, true otherwise.</returns>
        public static bool ClipPointSFWithPolygon(ref string pointSFPath, ref MapWinGIS.Shape polygon, ref string resultSFPath, bool copyAttributes)
        {
            MapWinUtility.Logger.Dbg("ClipPointSFWithPolygon(pointSFPath: " + pointSFPath + ",\n" +
                                     "                       polygon: " + Macro.ParamName(polygon) + ",\n" +
                                     "                       resultSFPath: " + resultSFPath.ToString() + ",\n" +
                                     "                       copyAttributes: " + copyAttributes.ToString() + ")");

            MapWinGIS.Shapefile resultSF = new MapWinGIS.ShapefileClass();
            MapWinGIS.Shapefile pointSF  = new MapWinGIS.ShapefileClass();
            int    shpIndex = 0;          //all new shapes will be placed at the beginning of the shapefile
            string tmpName;

            if (pointSF.Open(pointSFPath, null) == false)
            {
                gErrorMsg = "Failure to open input shapefile: " + pointSF.get_ErrorMsg(pointSF.LastErrorCode);
                MapWinUtility.Logger.Dbg(gErrorMsg);
                return(false);
            }
            MapWinGIS.ShpfileType sfType = pointSF.ShapefileType;

            //make sure we are dealing with a valid shapefile type
            if (sfType == MapWinGIS.ShpfileType.SHP_POINT || sfType == MapWinGIS.ShpfileType.SHP_POINTM || sfType == MapWinGIS.ShpfileType.SHP_POINTZ)
            {
                if (Globals.PrepareResultSF(ref resultSFPath, ref resultSF, sfType, copyAttributes) == false)
                {
                    return(false);
                }

                if (copyAttributes)
                {
                    MapWinGIS.Field tmpField, pointField;
                    for (int f = 0; f <= pointSF.NumFields - 1; f++)
                    {
                        tmpField   = new MapWinGIS.Field();
                        pointField = pointSF.get_Field(f);
                        tmpName    = pointField.Name;
                        if (tmpName.Contains("MWShapeID"))
                        {
                            tmpField.Name = "Last_" + tmpName;
                        }
                        else
                        {
                            tmpField.Name = tmpName;
                        }
                        tmpField.Width     = pointField.Width;
                        tmpField.Type      = pointField.Type;
                        tmpField.Precision = pointField.Precision;

                        tmpField.Key = pointField.Key;
                        if (!resultSF.EditInsertField(tmpField, ref f, null))
                        {
                            return(false);
                        }
                    }
                }

                int             numTargetPoints = pointSF.NumShapes;
                MapWinGIS.Point targetPoint     = new MapWinGIS.PointClass();
                //MapWinGIS.Utils utils = new MapWinGIS.UtilsClass();
                int numParts = polygon.NumParts;
                if (numParts == 0)
                {
                    numParts = 1;
                }
                Globals.Vertex[][] polyVertArray = new Globals.Vertex[numParts][];
                Globals.ConvertPolyToVertexArray(ref polygon, out polyVertArray);

                for (int i = 0; i <= numTargetPoints - 1; i++)
                {
                    targetPoint = pointSF.QuickPoint(i, 0);

                    if (Utils.PointInPoly(ref polyVertArray, ref targetPoint) == true)
                    {
                        resultSF.EditInsertShape(pointSF.get_Shape(i), ref shpIndex);
                        if (copyAttributes)
                        {
                            for (int f = 0; f <= pointSF.NumFields - 1; f++)
                            {
                                bool tmpbool = resultSF.EditCellValue(f, shpIndex, pointSF.get_CellValue(f, i));
                            }
                        }
                    }
                }

                MapWinGIS.Field ID = new MapWinGIS.FieldClass();
                ID.Name = "MWShapeID";
                ID.Type = MapWinGIS.FieldType.INTEGER_FIELD;
                int fieldIndex = 0;
                if (resultSF.EditInsertField(ID, ref fieldIndex, null) == false)
                {
                    gErrorMsg = "Problem inserting field into .dbf table: " + resultSF.get_ErrorMsg(resultSF.LastErrorCode);
                    Debug.WriteLine(gErrorMsg);
                    Error.SetErrorMsg(gErrorMsg);
                    MapWinUtility.Logger.Dbg(gErrorMsg);
                    return(false);
                }

                int numIDs = resultSF.NumShapes;
                for (int i = 0; i <= numIDs - 1; i++)
                {
                    if (resultSF.EditCellValue(fieldIndex, i, i) == false)
                    {
                        gErrorMsg = "Problem inserting value into .dbf table for shape " + i + ": " + resultSF.get_ErrorMsg(resultSF.LastErrorCode);
                        Debug.WriteLine(gErrorMsg);
                        Error.SetErrorMsg(gErrorMsg);
                        MapWinUtility.Logger.Dbg(gErrorMsg);
                        return(false);
                    }
                }

                if (resultSF.StopEditingShapes(true, true, null) == false)
                {
                    gErrorMsg = "Problem with StopEditingShapes: " + resultSF.get_ErrorMsg(resultSF.LastErrorCode);
                    Debug.WriteLine(gErrorMsg);
                    Error.SetErrorMsg(gErrorMsg);
                    MapWinUtility.Logger.Dbg(gErrorMsg);
                    return(false);
                }

                resultSF.Close();
                pointSF.Close();
                MapWinUtility.Logger.Dbg("Finished ClipPointSFWithPolygon");
            }
            else
            {
                pointSF.Close();
                gErrorMsg = "Shapefile type is incorrect. Must be of type Point.";
                Debug.WriteLine(gErrorMsg);
                MapWinGeoProc.Error.SetErrorMsg(gErrorMsg);
                MapWinUtility.Logger.Dbg(gErrorMsg);
                return(false);
            }
            return(true);
        }
        /// <summary>
        /// Returns an in-memory shapefile of points from the input shapefile that fall within the polygon.
        /// </summary>
        /// <param name="pointSF">Full path to the point shapefile.</param>
        /// <param name="polygon">The polygon used for clipping the point shapefile.</param>
        /// <param name="result">Full path to where the resulting point shapefile should be saved.</param>
        /// <param name="copyAttributes">True if copying attributes over</param>
        /// <returns>False if an error was encountered, true otherwise.</returns>
        public static bool ClipPointSFWithPolygon(ref MapWinGIS.Shapefile pointSF, ref MapWinGIS.Shape polygon, out MapWinGIS.Shapefile result, bool copyAttributes)
        {
            MapWinUtility.Logger.Dbg("ClipPointSFWithPolygon(pointSF: " + Macro.ParamName(pointSF) + ",\n" +
                                     "                       polygon: " + Macro.ParamName(polygon) + ",\n" +
                                     "                       result: out, \n" +
                                     "                       copyAttributes: " + copyAttributes.ToString() + ",\n");
            MapWinGIS.Shapefile resultSF = new MapWinGIS.ShapefileClass();
            int shpIndex = 0;             //all new shapes will be placed at the beginning of the shapefile

            MapWinGIS.ShpfileType sfType = pointSF.ShapefileType;

            //make sure we are dealing with a valid shapefile type
            if (sfType == MapWinGIS.ShpfileType.SHP_POINT || sfType == MapWinGIS.ShpfileType.SHP_POINTM || sfType == MapWinGIS.ShpfileType.SHP_POINTZ)
            {
                string tempPath = System.IO.Path.GetTempPath() + "resultSF.shp";
                DataManagement.DeleteShapefile(ref tempPath);
                string tmpName;
                //create the result shapeFile if it does not already exist
                //if(resultSF.CreateNew(tempPath, sfType) == false)
                //{
                //    gErrorMsg = "Problem creating the result shapeFile: " + resultSF.get_ErrorMsg(resultSF.LastErrorCode);
                //    Debug.WriteLine(gErrorMsg);
                //    MapWinGeoProc.Error.SetErrorMsg(gErrorMsg);
                //    result = resultSF;
                //    return false;
                //}
                //CDM 8/4/2006 resultSF.CreateNew(resultSFPath, sfType);
                Globals.PrepareResultSF(ref tempPath, ref resultSF, sfType);

                if (copyAttributes)
                {
                    MapWinGIS.Field tmpField, pointField;
                    for (int f = 0; f <= pointSF.NumFields - 1; f++)
                    {
                        tmpField   = new MapWinGIS.Field();
                        pointField = pointSF.get_Field(f);
                        tmpName    = pointField.Name;
                        if (tmpName.Contains("MWShapeID"))
                        {
                            tmpField.Name = "Last_" + tmpName;
                        }
                        else
                        {
                            tmpField.Name = tmpName;
                        }
                        tmpField.Width     = pointField.Width;
                        tmpField.Type      = pointField.Type;
                        tmpField.Precision = pointField.Precision;
                        tmpField.Key       = pointField.Key;
                        resultSF.EditInsertField(tmpField, ref f, null);
                    }
                }

                int             numTargetPoints = pointSF.NumShapes;
                MapWinGIS.Point targetPoint     = new MapWinGIS.PointClass();
                //MapWinGIS.Utils utils = new MapWinGIS.UtilsClass();
                int numParts = polygon.NumParts;
                if (numParts == 0)
                {
                    numParts = 1;
                }
                Globals.Vertex[][] polyVertArray = new Globals.Vertex[numParts][];
                Globals.ConvertPolyToVertexArray(ref polygon, out polyVertArray);

                for (int i = 0; i <= numTargetPoints - 1; i++)
                {
                    targetPoint = pointSF.QuickPoint(i, 0);

                    if (Utils.PointInPoly(ref polyVertArray, ref targetPoint) == true)
                    {
                        resultSF.EditInsertShape(pointSF.get_Shape(i), ref shpIndex);
                        if (copyAttributes)
                        {
                            for (int f = 0; f <= pointSF.NumFields - 1; f++)
                            {
                                bool tmpbool = resultSF.EditCellValue(f, shpIndex, pointSF.get_CellValue(f, i));
                            }
                        }
                    }
                }
            }
            else
            {
                gErrorMsg = "The shapefile is of the wrong type. Should be of type Point.";
                Debug.WriteLine(gErrorMsg);
                MapWinGeoProc.Error.SetErrorMsg(gErrorMsg);
                result = resultSF;
                MapWinUtility.Logger.Dbg(gErrorMsg);
                return(false);
            }
            result = resultSF;
            MapWinUtility.Logger.Dbg("Finished ClipPointSFWithPolygon");
            return(true);
        }
        /// <summary>
        /// Returns all portions of the shapefile polygons that fall within the clipper polygon.
        /// </summary>
        /// <param name="polySFPath">The full path to the shapefile of polygons to be clipped.</param>
        /// <param name="polygon">The polygon used for clipping the shapefile.</param>
        /// <param name="resultSFPath">The full path to the result file for where the clipped polygons should be saved.</param>
        /// <param name="copyAttributes">True if copying attrs</param>
        /// <returns>False if an error was encountered, true otherwise.</returns>
        public static bool ClipPolygonSFWithPolygon(ref string polySFPath, ref MapWinGIS.Shape polygon, ref string resultSFPath, bool copyAttributes)
        {
            MapWinUtility.Logger.Dbg("ClipPolygonSFWithPolygon(polySFPath: " + polySFPath + ",\n " +
                                     "                         polygon: + " + Macro.ParamName(polygon) + ",\n" +
                                     "                         resultsSFPath: " + resultSFPath + ",\n" +
                                     "                         copyAttributes: " + copyAttributes + ",\n");

            MapWinGIS.Shapefile resultSF = new MapWinGIS.ShapefileClass();
            MapWinGIS.Shapefile polySF   = new MapWinGIS.ShapefileClass();
            polySF.Open(polySFPath, null);
            MapWinGIS.ShpfileType sfType = polySF.ShapefileType;
            int    shapeIndex            = 0;//insert new shapes at the beginning of the shapefile
            string tmpName;

            if (Globals.PrepareResultSF(ref resultSFPath, ref resultSF, sfType, copyAttributes) == false)
            {
                polySF.Close();
                return(false);
            }

            if (copyAttributes)
            {
                MapWinGIS.Field tmpField, currField;
                for (int f = 0; f <= polySF.NumFields - 1; f++)
                {
                    tmpField  = new MapWinGIS.Field();
                    currField = polySF.get_Field(f);
                    tmpName   = currField.Name;
                    if (tmpName.Contains("MWShapeID"))
                    {
                        tmpField.Name = "Last_" + tmpName;
                    }
                    else
                    {
                        tmpField.Name = tmpName;
                    }

                    tmpField.Width     = currField.Width;
                    tmpField.Type      = currField.Type;
                    tmpField.Precision = currField.Precision;
                    tmpField.Key       = currField.Key;
                    resultSF.EditInsertField(tmpField, ref f, null);
                }
            }

            if (sfType == MapWinGIS.ShpfileType.SHP_POLYGON || sfType == MapWinGIS.ShpfileType.SHP_POLYGONM || sfType == MapWinGIS.ShpfileType.SHP_POLYGONZ)
            {
                MapWinGIS.Shape   resultShape = new MapWinGIS.ShapeClass();
                MapWinGIS.Extents shpExtents  = new MapWinGIS.ExtentsClass();
                int  numShapes       = polySF.NumShapes;
                bool boundsIntersect = false;

                for (int i = 0; i <= numShapes - 1; i++)
                {
                    MapWinGIS.Shape currPoly = new MapWinGIS.ShapeClass();
                    currPoly = polySF.get_Shape(i);
                    //check to see if bounds intersect before sending shape to GPC clip function
                    boundsIntersect = Globals.CheckBounds(ref currPoly, ref polygon);

                    if (boundsIntersect == true)
                    {
                        //find the shape resulting from intersection
                        resultShape = SpatialOperations.Intersection(currPoly, polygon);
                        if (resultShape.numPoints != 0)
                        {
                            //save the new shape to the result shapefile
                            if (resultSF.EditInsertShape(resultShape, ref shapeIndex) == false)
                            {
                                gErrorMsg = "Problem inserting shape: " + resultSF.get_ErrorMsg(resultSF.LastErrorCode);
                                Debug.WriteLine(gErrorMsg);
                                Error.SetErrorMsg(gErrorMsg);
                                MapWinUtility.Logger.Dbg(gErrorMsg);
                                return(false);
                            }
                            if (copyAttributes)
                            {
                                for (int f = 0; f <= polySF.NumFields - 1; f++)
                                {
                                    bool tmpbool = resultSF.EditCellValue(f, shapeIndex, polySF.get_CellValue(f, i));
                                }
                            }
                        }
                    }
                }
            }
            if (copyAttributes)
            {
                MapWinGIS.Field ID = new MapWinGIS.FieldClass();
                ID.Name = "MWShapeID";
                ID.Type = MapWinGIS.FieldType.INTEGER_FIELD;
                int fieldIndex = 0;
                if (resultSF.EditInsertField(ID, ref fieldIndex, null) == false)
                {
                    gErrorMsg = "Problem inserting field into .dbf table: " + resultSF.get_ErrorMsg(resultSF.LastErrorCode);
                    Debug.WriteLine(gErrorMsg);
                    Error.SetErrorMsg(gErrorMsg);
                    MapWinUtility.Logger.Dbg(gErrorMsg);
                    return(false);
                }
            }
            int numIDs = resultSF.NumShapes;

            for (int i = 0; i <= numIDs - 1; i++)
            {
                if (resultSF.EditCellValue(0, i, i) == false)
                {
                    gErrorMsg = "Problem inserting value into .dbf table for shape " + i + ": " + resultSF.get_ErrorMsg(resultSF.LastErrorCode);
                    Debug.WriteLine(gErrorMsg);
                    Error.SetErrorMsg(gErrorMsg);
                    MapWinUtility.Logger.Dbg(gErrorMsg);
                    return(false);
                }
            }
            if (resultSF.StopEditingShapes(true, true, null) == false)
            {
                gErrorMsg = "Problem with StopEditingShapes: " + resultSF.get_ErrorMsg(resultSF.LastErrorCode);
                Debug.WriteLine(gErrorMsg);
                Error.SetErrorMsg(gErrorMsg);
                MapWinUtility.Logger.Dbg(gErrorMsg);
                return(false);
            }
            resultSF.Close();
            polySF.Close();
            MapWinUtility.Logger.Dbg("Finished ClipPolygonSFWithPolygon");
            return(true);
        }
        /// <summary>
        /// Returns the portions of the polygons in polySF that lie within polygon as a
        /// new shapefile of polygons: resultPolySF.
        /// </summary>
        /// <param name="polySF">The shapefile of polygons that are to be clipped.</param>
        /// <param name="polygon">The polygon used for clipping.</param>
        /// <param name="resultPolySF">The result shapefile for the resulting polygons to be saved (in-memory).</param>
        /// <param name="copyAttributes">True if copying attrs</param>
        /// <returns>False if an error was encountered, true otherwise.</returns>
        public static bool ClipPolygonSFWithPolygon(ref MapWinGIS.Shapefile polySF, ref MapWinGIS.Shape polygon, out MapWinGIS.Shapefile resultPolySF, bool copyAttributes)
        {
            MapWinUtility.Logger.Dbg("ClipPolygonSFWithPolygon(polySF: " + Macro.ParamName(polySF) + ",\n" +
                                     "                         polygon: " + Macro.ParamName(polygon) + ",\n" +
                                     "                         resultPolySF: out,\n" +
                                     "                         copyAttributes: " + copyAttributes.ToString() + "\n");

            MapWinGIS.Shapefile   resultSF = new MapWinGIS.ShapefileClass();
            MapWinGIS.ShpfileType sfType   = polySF.ShapefileType;
            string tempPath = System.IO.Path.GetTempPath() + "resultSF.shp";

            DataManagement.DeleteShapefile(ref tempPath);
            string tmpName;

            Globals.PrepareResultSF(ref tempPath, ref resultSF, sfType, copyAttributes);

            if (copyAttributes)
            {
                MapWinGIS.Field tmpField, currField;
                for (int f = 0; f <= polySF.NumFields - 1; f++)
                {
                    tmpField  = new MapWinGIS.Field();
                    currField = polySF.get_Field(f);
                    tmpName   = currField.Name;
                    if (tmpName.Contains("MWShapeID"))
                    {
                        tmpField.Name = "Last_" + tmpName;
                    }
                    else
                    {
                        tmpField.Name = tmpName;
                    }
                    tmpField.Width     = currField.Width;
                    tmpField.Type      = currField.Type;
                    tmpField.Precision = currField.Precision;
                    tmpField.Key       = currField.Key;
                    resultSF.EditInsertField(tmpField, ref f, null);
                }
            }

            int shapeIndex = 0;            //insert new shapes at the beginning of the shapefile

            if (polySF.NumShapes != 0 && polygon.numPoints != 0 && (sfType == MapWinGIS.ShpfileType.SHP_POLYGON || sfType == MapWinGIS.ShpfileType.SHP_POLYGONM || sfType == MapWinGIS.ShpfileType.SHP_POLYGONZ))
            {
                MapWinGIS.Shape resultShape     = new MapWinGIS.ShapeClass();
                int             numShapes       = polySF.NumShapes;
                bool            boundsIntersect = false;

                for (int i = 0; i <= numShapes - 1; i++)
                {
                    MapWinGIS.Shape currPoly = new MapWinGIS.ShapeClass();
                    currPoly = polySF.get_Shape(i);
                    //check to see if bounds intersect before sending shape to GPC clip function
                    boundsIntersect = Globals.CheckBounds(ref currPoly, ref polygon);

                    if (boundsIntersect == true)
                    {
                        //find the shape resulting intersection
                        resultShape = SpatialOperations.Intersection(polySF.get_Shape(i), polygon);
                        if (resultShape.numPoints != 0)
                        {
                            //save the new shape to the result shapefile
                            if (resultSF.EditInsertShape(resultShape, ref shapeIndex) == false)
                            {
                                gErrorMsg = "Problem inserting shape: " + resultSF.get_ErrorMsg(resultSF.LastErrorCode);
                                Debug.WriteLine(gErrorMsg);
                                MapWinGeoProc.Error.SetErrorMsg(gErrorMsg);
                                resultPolySF = resultSF;
                                return(false);
                            }
                            if (copyAttributes)
                            {
                                for (int f = 0; f <= polySF.NumFields - 1; f++)
                                {
                                    bool tmpbool = resultSF.EditCellValue(f, shapeIndex, polySF.get_CellValue(f, i));
                                }
                            }
                        }
                    }
                }
            }
            resultPolySF = resultSF;
            return(true);
        }
Esempio n. 11
0
        /// <summary>
        /// Generalization of polyline
        /// shapefiles using the Douglas-Peucker line simplification
        /// algorithm. This method will output a line shapefile.
        /// </summary>
        /// <param name="inFileName">Input shapefile</param>
        /// <param name="outFileName">Output shapefile</param>
        /// <param name="tolerance">tolerance parameter -
        /// specfies the maximum allowed distance between original polyline
        /// and simplified polyline</param>
        /// <param name="cback">Use this parameter for reporting progress. Set to null if not needed</param>
        public static void Generalize(string inFileName, string outFileName, double tolerance, MapWinGIS.ICallback cback)
        {
            MapWinGIS.Shapefile oldSF = new MapWinGIS.Shapefile();
            if (!oldSF.Open(inFileName, null))
            {
                throw new ArgumentException(string.Format("Shapefile {0} could not be opened. Error: {1}",
                                                          inFileName, oldSF.get_ErrorMsg(oldSF.LastErrorCode)));
            }

            //Check if it's a line shapefile
            if (!(oldSF.ShapefileType == MapWinGIS.ShpfileType.SHP_POLYLINE ||
                  oldSF.ShapefileType == MapWinGIS.ShpfileType.SHP_POLYLINEM ||
                  oldSF.ShapefileType == MapWinGIS.ShpfileType.SHP_POLYLINEZ))
            {
                throw new ArgumentException(string.Format("Shapefile {0} must be a polyline shapefile.", inFileName));
            }

            int numShapes = oldSF.NumShapes;
            int numFields = oldSF.NumFields;

            //create a new output shapefile
            MapWinGIS.Shapefile   newSF  = new MapWinGIS.Shapefile();
            MapWinGIS.ShpfileType sftype = MapWinGIS.ShpfileType.SHP_POLYLINE;

            // if shapefile exists - open it and clear all shapes
            if (System.IO.File.Exists(outFileName))
            {
                try
                {
                    //TODO: ask for overwriting..
                    bool deleted = MapWinGeoProc.DataManagement.DeleteShapefile(ref outFileName);
                }
                finally
                {
                }
            }

            if (!newSF.CreateNew(outFileName, sftype))
            {
                throw new InvalidOperationException
                          ("Error creating shapefile " + outFileName + " " + newSF.get_ErrorMsg(newSF.LastErrorCode));
            }
            newSF.StartEditingShapes(true, cback);

            //Copy all fields
            if (!Globals.CopyFields(ref oldSF, ref newSF))
            {
                throw new InvalidOperationException(string.Format("Error copying fields from {0} to {1}",
                                                                  oldSF.Filename, newSF.Filename));
            }

            int newShapeIndex = 0;

            for (int shpIdx = 0; shpIdx < numShapes; ++shpIdx)
            {
                MapWinGIS.Shape shp = oldSF.get_Shape(shpIdx);

                // convert each part of the polyline shape to a 'geometry' object
                Geometry geom = MapWinGeoProc.NTS_Adapter.ShapeToGeometry(shp);
                for (int partIdx = 0; partIdx < geom.NumGeometries; ++partIdx)
                {
                    Geometry geomPart = (Geometry)geom.GetGeometryN(partIdx);

                    //do the simplification
                    ICoordinate[] oldCoords = geomPart.Coordinates;
                    DouglasPeuckerLineSimplifier simplifier = new DouglasPeuckerLineSimplifier(oldCoords);
                    simplifier.DistanceTolerance = tolerance;
                    ICoordinate[] newCoords = simplifier.Simplify();

                    //convert the coordinates back to a geometry
                    Geometry newGeom = new LineString(newCoords);

                    //convert the geometry back to a shape
                    MapWinGIS.Shape newShape = MapWinGeoProc.NTS_Adapter.GeometryToShape(newGeom);

                    //add the shape to the new shapefile
                    newShapeIndex = newSF.NumShapes;
                    if (newSF.EditInsertShape(newShape, ref newShapeIndex) == false)
                    {
                        throw new InvalidOperationException("Error inserting shape: " +
                                                            newSF.get_ErrorMsg(newSF.LastErrorCode));
                    }
                    //add attribute values
                    for (int fldIdx = 0; fldIdx < numFields; ++fldIdx)
                    {
                        object val = oldSF.get_CellValue(fldIdx, shpIdx);
                        if (newSF.EditCellValue(fldIdx, newSF.NumShapes - 1, val) == false)
                        {
                            throw new InvalidOperationException("Error editing cell value: " +
                                                                newSF.get_ErrorMsg(newSF.LastErrorCode));
                        }
                    }
                }
            }
            //close the old shapefile
            oldSF.Close();

            //stop editing and close the new shapefile
            newSF.StopEditingShapes(true, true, cback);
            newSF.Close();
        }