Example #1
0
        /// <summary>
        /// Removes portions of the input line shapefile that fall within the polygons of the erase polygon shapefile.
        /// </summary>
        /// <param name="inputSF">The line shapefile to erase.</param>
        /// <param name="eraseSF">The polygon shapefile that will be used to erase portions of the line shapefile.</param>
        /// <param name="resultSF">The result shapefile.</param>
        /// <returns>False if an error was encountered, true otherwise.</returns>
        public static bool EraseLineSFWithPolySF(ref MapWinGIS.Shapefile inputSF, ref MapWinGIS.Shapefile eraseSF, ref MapWinGIS.Shapefile resultSF)
        {
            MapWinUtility.Logger.Dbg("EraseLineSFWithPolySF(inputSF: " + Macro.ParamName(inputSF) + "\n" +
                                     "                      eraseSF: " + Macro.ParamName(eraseSF) + "\n" +
                                     "                      resultSF: " + Macro.ParamName(resultSF) + ")");

            if (inputSF == null || eraseSF == null || resultSF == null)
            {
                gErrorMsg = "One of the input parameters is null.";
                Error.SetErrorMsg(gErrorMsg);
                Debug.WriteLine(gErrorMsg);
                MapWinUtility.Logger.Dbg(gErrorMsg);
                return(false);
            }
            bool status = true;

            MapWinGIS.Shapefile tempInput = new MapWinGIS.ShapefileClass();
            string tempFile = System.IO.Path.GetTempPath() + "tempLineResult.shp";

            //CDM 8/4/2006 tempInput.CreateNew(tempFile, inputSF.ShapefileType);
            Globals.PrepareResultSF(ref tempFile, ref tempInput, inputSF.ShapefileType);
            tempInput.StartEditingShapes(true, null);
            int shpIndex  = 0;
            int numInputs = inputSF.NumShapes;

            for (int i = 0; i <= numInputs - 1; i++)
            {
                tempInput.EditInsertShape(inputSF.get_Shape(i), ref shpIndex);
                shpIndex++;
            }

            int numErase = eraseSF.NumShapes;

            for (int i = 0; i <= numErase - 1; i++)
            {
                Debug.WriteLine("Num shapes in tempInput = " + tempInput.NumShapes);
                MapWinGIS.Shape eraseShp = new MapWinGIS.ShapeClass();
                eraseShp = eraseSF.get_Shape(i);
                resultSF.EditClear();
                status = EraseLineSFWithPoly(ref tempInput, ref eraseShp, ref resultSF, false);
                if (i != numErase - 1)
                {
                    int numResults = resultSF.NumShapes;
                    if (numResults > 0)
                    {
                        tempInput.EditClear();
                        shpIndex = 0;
                        for (int j = 0; j <= numResults - 1; j++)
                        {
                            tempInput.EditInsertShape(resultSF.get_Shape(j), ref shpIndex);
                            shpIndex++;
                        }
                    }
                }
            }
            MapWinUtility.Logger.Dbg("Finished EraseLineSFWithPolySF");
            return(status);
        }
        /// <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);
        }
Example #4
0
        /// <summary>
        /// Removes portions of the lineSF that fall within the erase polygon
        /// </summary>
        /// <param name="lineSF">The shapefile of lines to be erased.</param>
        /// <param name="erasePoly">The polygon to be used for erasing portion of the line shapefile.</param>
        /// <param name="resultSF">The resulting line shapefile with portions removed.</param>
        /// <param name="CopyAttributes">Indicates whether to copy attributes</param>
        /// <returns>False if an error was encountered, true otherwise.</returns>
        public static bool EraseLineSFWithPoly(ref MapWinGIS.Shapefile lineSF, ref MapWinGIS.Shape erasePoly, ref MapWinGIS.Shapefile resultSF, bool CopyAttributes)
        {
            MapWinUtility.Logger.Dbg("EraseLineSFWithPoly(lineSF: " + Macro.ParamName(lineSF) + ",\n" +
                                     "                    erasePoly: " + Macro.ParamName(erasePoly) + ",\n" +
                                     "                    resultSF: " + Macro.ParamName(resultSF) + ",\n" +
                                     "                    CopyAttributes: " + CopyAttributes.ToString() + ")");
            if (lineSF == null || erasePoly == null || resultSF == null)
            {
                gErrorMsg = "One of the input parameters is null.";
                Error.SetErrorMsg(gErrorMsg);
                Debug.WriteLine(gErrorMsg);
                MapWinUtility.Logger.Dbg(gErrorMsg);
                return(false);
            }

            if (CopyAttributes)
            {
                string          tmpName;
                MapWinGIS.Field tmpField, currField;
                for (int f = 0; f <= lineSF.NumFields - 1; f++)
                {
                    tmpField      = new MapWinGIS.Field();
                    currField     = lineSF.get_Field(f);
                    tmpName       = currField.Name;
                    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 shpIndex = 0;
            int numLines = lineSF.NumShapes;

            for (int i = 0; i <= numLines - 1; i++)
            {
                MapWinGIS.Shape currLine = new MapWinGIS.ShapeClass();
                currLine.Create(lineSF.ShapefileType);
                currLine = lineSF.get_Shape(i);

                MapWinGIS.Shape lineEnvelope = new MapWinGIS.ShapeClass();
                lineEnvelope.Create(MapWinGIS.ShpfileType.SHP_POLYGON);
                //create lineExtents' points out of the line extent points
                MapWinGIS.Point lTop, rTop, rBottom, lBottom;
                lTop      = new MapWinGIS.PointClass();
                lTop.x    = currLine.Extents.xMin;
                lTop.y    = currLine.Extents.yMax;
                rTop      = new MapWinGIS.PointClass();
                rTop.x    = currLine.Extents.xMax;
                rTop.y    = currLine.Extents.yMax;
                rBottom   = new MapWinGIS.PointClass();
                rBottom.x = currLine.Extents.xMax;
                rBottom.y = currLine.Extents.yMin;
                lBottom   = new MapWinGIS.PointClass();
                lBottom.x = currLine.Extents.xMin;
                lBottom.y = currLine.Extents.yMin;
                //now add the extent points to the new polygon shape: lineEnvelope
                int ptIndex = 0;
                lineEnvelope.InsertPoint(lTop, ref ptIndex);
                ptIndex++;
                lineEnvelope.InsertPoint(rTop, ref ptIndex);
                ptIndex++;
                lineEnvelope.InsertPoint(rBottom, ref ptIndex);
                ptIndex++;
                lineEnvelope.InsertPoint(lBottom, ref ptIndex);
                ptIndex++;
                lineEnvelope.InsertPoint(lTop, ref ptIndex);
                //remove COM points from memory
                while (Marshal.ReleaseComObject(lTop) != 0)
                {
                    ;
                }
                while (Marshal.ReleaseComObject(rTop) != 0)
                {
                    ;
                }
                while (Marshal.ReleaseComObject(rBottom) != 0)
                {
                    ;
                }
                while (Marshal.ReleaseComObject(lBottom) != 0)
                {
                    ;
                }

                //Check if line extents and polygon extents overlap
                if (Globals.CheckBounds(ref lineEnvelope, ref erasePoly))
                {
                    //make the envelope polygon slightly larger
                    MapWinGIS.Shape lgEnvelope = new MapWinGIS.ShapeClass();
                    lgEnvelope.Create(MapWinGIS.ShpfileType.SHP_POLYGON);
                    SpatialOperations.BufferPolygon(ref lineEnvelope, 0.5, Enumerations.Buffer_HoleTreatment.Ignore, Enumerations.Buffer_CapStyle.Pointed, out lgEnvelope);
                    //take the difference of the envelope polygon with the erase polygon.
                    MapWinGIS.Shape diff = new MapWinGIS.ShapeClass();
                    diff.Create(MapWinGIS.ShpfileType.SHP_POLYGON);
                    diff = SpatialOperations.Difference(lgEnvelope, erasePoly);
                    if (diff.numPoints > 0)
                    {
                        //the difference shape represents the line envelope
                        //minus the area of the erase polygon.
                        MapWinGIS.Shapefile inputLine = new MapWinGIS.ShapefileClass();
                        string tempPath = System.IO.Path.GetTempPath() + "tempInputLine.shp";
                        //CDM 8/4/2006 inputLine.CreateNew(tempPath, lineSF.ShapefileType);
                        Globals.PrepareResultSF(ref tempPath, ref inputLine, lineSF.ShapefileType);
                        shpIndex = 0;
                        inputLine.EditInsertShape(currLine, ref shpIndex);

                        int numParts = diff.NumParts;
                        if (numParts == 0)
                        {
                            numParts = 1;
                        }

                        if (numParts > 1)
                        {
                            //separate and test each part individually
                            MapWinGIS.Shape[] diffParts = new MapWinGIS.Shape[numParts];
                            Globals.SeparateParts(ref diff, out diffParts);
                            for (int j = 0; j <= numParts - 1; j++)
                            {
                                //don't check inside of holes
                                if (Globals.IsClockwise(ref diffParts[j]))
                                {
                                    MapWinGIS.Shapefile tempLineResult = new MapWinGIS.ShapefileClass();
                                    string tempLineFile = System.IO.Path.GetTempPath() + "tempLines.shp";
                                    DataManagement.DeleteShapefile(ref tempLineFile);
                                    //CDM 8/4/2006 tempLineResult.CreateNew(tempLineFile, lineSF.ShapefileType);
                                    Globals.PrepareResultSF(ref tempLineFile, ref tempLineResult, lineSF.ShapefileType);
                                    tempLineResult.StartEditingShapes(true, null);

                                    SpatialOperations.ClipShapesWithPolygon(ref inputLine, ref diffParts[j], out tempLineResult, false);

                                    int numResults = tempLineResult.NumShapes;
                                    if (numResults > 0)
                                    {
                                        //add results to the final result file.
                                        for (int k = 0; k <= numResults - 1; k++)
                                        {
                                            shpIndex = resultSF.NumShapes;
                                            resultSF.EditInsertShape(tempLineResult.get_Shape(k), ref shpIndex);
                                            if (CopyAttributes)
                                            {
                                                for (int f = 0; f <= lineSF.NumFields - 1; f++)
                                                {
                                                    bool tmpbool = resultSF.EditCellValue(f, shpIndex, lineSF.get_CellValue(f, i));
                                                }
                                            }
                                        }
                                    }                    //clipping successful
                                }                        //done checking islands
                            }                            //done looping through parts of the difference shape
                        }
                        else
                        {
                            MapWinGIS.Shapefile tempLineResult = new MapWinGIS.ShapefileClass();
                            string tempLineFile = System.IO.Path.GetTempPath() + "tempLines.shp";
                            DataManagement.DeleteShapefile(ref tempLineFile);
                            //CDM 8/4/2006 tempLineResult.CreateNew(tempLineFile, lineSF.ShapefileType);
                            Globals.PrepareResultSF(ref tempLineFile, ref tempLineResult, lineSF.ShapefileType);

                            tempLineResult.StartEditingShapes(true, null);

                            SpatialOperations.ClipShapesWithPolygon(ref inputLine, ref diff, out tempLineResult, false);

                            int numResults = tempLineResult.NumShapes;
                            if (numResults > 0)
                            {
                                //add results to the final result file.
                                for (int k = 0; k <= numResults - 1; k++)
                                {
                                    shpIndex = resultSF.NumShapes;
                                    resultSF.EditInsertShape(tempLineResult.get_Shape(k), ref shpIndex);
                                    if (CopyAttributes)
                                    {
                                        for (int f = 0; f <= lineSF.NumFields - 1; f++)
                                        {
                                            bool tmpbool = resultSF.EditCellValue(f, shpIndex, lineSF.get_CellValue(f, i));
                                        }
                                    }
                                }
                            }    //clipping successful
                        }
                    }            //difference operation successful
                }                //bounds overlapped
                else
                {
                    shpIndex = resultSF.NumShapes;
                    resultSF.EditInsertShape(currLine, ref shpIndex);
                    if (CopyAttributes)
                    {
                        for (int f = 0; f <= lineSF.NumFields - 1; f++)
                        {
                            bool tmpbool = resultSF.EditCellValue(f, shpIndex, lineSF.get_CellValue(f, i));
                        }
                    }
                }
            }            //end of looping through lines in the input shapefile
            MapWinUtility.Logger.Dbg("Finished EraseLineSFWithPoly");
            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);
        }
Example #7
0
        private void btnOK_Click(object sender, EventArgs e)
        {
            try
            {
                if (null == tempPath)
                {
                    FolderBrowserDialog dlgFolder = new FolderBrowserDialog();
                    if (dlgFolder.ShowDialog() == DialogResult.OK)
                    {
                        tempPath = dlgFolder.SelectedPath + "\\";
                    }
                    else //No output folder chosen, exit
                    {
                        return;
                    }
                }
                int count = lstToAddLayers.Items.Count;
                for (int i = 0; i < count; i++)
                {
                    MapWinGIS.Shapefile sf = new MapWinGIS.ShapefileClass();
                    string featureName     = lstToAddLayers.Items[i].ToString();
                    string strSql          = null;

                    switch (frmConnection.DbType)
                    {
                    case "MySQL 5.0":
                        strSql = "select Shape from " + featureName;
                        MySql.Data.MySqlClient.MySqlDataAdapter daMySql = new MySql.Data.MySqlClient.MySqlDataAdapter(strSql, (MySqlConnection)frmConnection.SqlConnection);
                        DataSet dsMySql = new DataSet();
                        daMySql.Fill(dsMySql);
                        DataTable dtMySql = dsMySql.Tables[0];

                        int rowCountMySql = dtMySql.Rows.Count;

                        if (rowCountMySql > 0)
                        {
                            MapWinGIS.Shape shp = new MapWinGIS.Shape();
                            shp.CreateFromString((string)dtMySql.Rows[i][0]);
                            sf.CreateNew(tempPath + featureName + ".shp", shp.ShapeType);
                            sf.StartEditingShapes(true, null);
                            int stupidx = 0;
                            sf.EditInsertShape(shp, ref stupidx);

                            for (int j = 1; j < rowCountMySql; j++)
                            {
                                MapWinGIS.Shape newShp = new MapWinGIS.Shape();
                                newShp.CreateFromString((string)dtMySql.Rows[j][0]);
                                int veryStupidRefForTheFunEditInsertShape = sf.NumShapes;
                                sf.EditInsertShape(newShp, ref veryStupidRefForTheFunEditInsertShape);
                            }
                            sf.StopEditingShapes(true, true, null);
                        }
                        break;
                    //case "Oracle 9i":

                    //    break;
                    //case "Oracle 10g":
                    //    strSql = "select \"Shape\" from OPENHYDRO." + "\"" + featureName + "\"";
                    //    OracleDataAdapter daOra = new OracleDataAdapter(strSql, (OracleConnection)frmConnection.SqlConnection);
                    //    DataSet dsOra = new DataSet();
                    //    daOra.Fill(dsOra);
                    //    DataTable dtOra = dsOra.Tables[0];

                    //    int rowCountOra = dtOra.Rows.Count;

                    //    if (rowCountOra > 0)
                    //    {

                    //        MapWinGIS.Shape shp = new MapWinGIS.Shape();
                    //        shp.CreateFromString((string)dtOra.Rows[i][0]);
                    //        sf.CreateNew(tempPath + featureName + ".shp", shp.ShapeType);
                    //        sf.StartEditingShapes(true, null);
                    //        int stupidx = 0;
                    //        sf.EditInsertShape(shp, ref stupidx);

                    //        for (int j = 1; j < rowCountOra; j++)
                    //        {
                    //            MapWinGIS.Shape newShp = new MapWinGIS.Shape();
                    //            newShp.CreateFromString((string)dtOra.Rows[j][0]);
                    //            int veryStupidRefForTheFunEditInsertShape = sf.NumShapes;
                    //            sf.EditInsertShape(newShp, ref veryStupidRefForTheFunEditInsertShape);
                    //        }
                    //        sf.StopEditingShapes(true, true, null);
                    //    }

                    //    break;
                    case "PostGIS":

                        strSql = "select f_table_name from geometry_columns";
                        IDbDataAdapter daPgsql = new Npgsql.NpgsqlDataAdapter(strSql, (Npgsql.NpgsqlConnection)frmConnection.SqlConnection);
                        DataSet        dsPg    = new DataSet();
                        daPgsql.Fill(dsPg);
                        DataTable dtPg = dsPg.Tables[0];

                        //store user name and password information for future usage:
                        string username = frmConnection.Username;
                        string password = frmConnection.Password;

                        for (int x = 0; x < dtPg.Rows.Count; x++)
                        {
                            //Export all the features into shapefiles and then open them in MapWindow:
                            string fName  = (string)dtPg.Rows[x][0];
                            string strCmd = " -u " + username + " -P " + password + " postgis " + fName + " -f " + tempPath + fName + ".shp";
                            if (lstToAddLayers.Items.Contains(fName))
                            {
                                System.Diagnostics.ProcessStartInfo info = new System.Diagnostics.ProcessStartInfo();
                                info.FileName        = "pgsql2shp.exe ";
                                info.Arguments       = strCmd;
                                info.CreateNoWindow  = true;
                                info.UseShellExecute = false;
                                System.Diagnostics.Process proc = new System.Diagnostics.Process();
                                proc.StartInfo = info;
                                proc.Start();
                                proc.WaitForExit();
                            }
                        }

                        break;

                    default:
                        break;
                    }
                }
                this.Hide();
            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.ToString());
            }
        }