Ejemplo n.º 1
0
        /// <summary>
        /// Returns the indexes of shapes that fall within the specified polygon.
        /// </summary>
        /// <returns>False if an error occurs, true otherwise.</returns>
        public static bool SelectLinesWithPolygon(ref MapWinGIS.Shapefile inputSF, ref MapWinGIS.Shape polygon, ref System.Collections.ArrayList results)
        {
            int numLines = inputSF.NumShapes;

            results = new System.Collections.ArrayList();

            // Boundary intersection test variables
            double xMin1, xMax1, yMin1, yMax1, zMin1, zMax1, xMin2, xMax2, yMin2, yMax2, zMin2, zMax2;

            // Get the masking polygon's boundaries only once:
            polygon.Extents.GetBounds(out xMin2, out yMin2, out zMin2, out xMax2, out yMax2, out zMax2);

            MapWinGIS.Shape currLine;
            MapWinGIS.Point currPt;
            for (int i = 0; i <= numLines - 1; i++)
            {
                currLine = inputSF.get_Shape(i);
                currLine.Extents.GetBounds(out xMin1, out yMin1, out zMin1, out xMax1, out yMax1, out zMax1);

                // Are the boundaries intersecting?
                if (!(xMin1 > xMax2 || xMax1 < xMin2 || yMin1 > yMax2 || yMax1 < yMin2))
                {
                    //lines are nasty, just because the boundaries intersect it
                    //doesn't mean the line enters the polygon
                    //do a quick point check before doing a more thorough investigation

                    int  numPoints = currLine.numPoints;
                    bool ptInside  = false;
                    for (int j = 0; j <= numPoints - 1; j++)
                    {
                        currPt = currLine.get_Point(j);
                        if (polygon.PointInThisPoly(currPt))
                        {
                            ptInside = true;
                            break;
                        }
                    }
                    if (ptInside)
                    {
                        results.Add(i);
                    }//end of ptInside check
                    else
                    {
                        // Avoid using a temp file to test each individual file;
                        // instead, just see if the line crosses the polygon
                        if (LineCrossesPoly(ref currLine, ref polygon))
                        {
                            //part of the line lies within the polygon, add to result file
                            results.Add(i);
                        } //end of successful cross
                    }     //end of else no points were found inside polygon
                }         //end of checking bounds
            }             //end of looping through lines

            return(results.Count > 0);
        }
Ejemplo n.º 2
0
        /// <summary>
        /// Exports the shapes from the inputSF which fall within the given polygon, saving to the resultSF provided.
        /// </summary>
        /// <returns>False if an error occurs, true otherwise.</returns>
        public static bool SelectLinesWithPolygon(ref MapWinGIS.Shapefile inputSF, ref MapWinGIS.Shape polygon, ref MapWinGIS.Shapefile resultSF, bool SkipMWShapeID)
        {
            int numLines = inputSF.NumShapes;
            int shpIndex = 0;

            // Boundary intersection test variables
            double xMin1, xMax1, yMin1, yMax1, zMin1, zMax1, xMin2, xMax2, yMin2, yMax2, zMin2, zMax2;

            // Get the masking polygon's boundaries only once:
            polygon.Extents.GetBounds(out xMin2, out yMin2, out zMin2, out xMax2, out yMax2, out zMax2);

            if (Globals.CopyFields(ref inputSF, ref resultSF) == false)
            {
                return(false);
            }

            MapWinGIS.Shape currLine;
            MapWinGIS.Point currPt;
            for (int i = 0; i <= numLines - 1; i++)
            {
                currLine = inputSF.get_Shape(i);
                currLine.Extents.GetBounds(out xMin1, out yMin1, out zMin1, out xMax1, out yMax1, out zMax1);

                // Are the boundaries intersecting?
                if (!(xMin1 > xMax2 || xMax1 < xMin2 || yMin1 > yMax2 || yMax1 < yMin2))
                {
                    //lines are nasty, just because the boundaries intersect it
                    //doesn't mean the line enters the polygon
                    //do a quick point check before doing a more thorough investigation

                    int  numPoints = currLine.numPoints;
                    bool ptInside  = false;
                    for (int j = 0; j <= numPoints - 1; j++)
                    {
                        currPt = currLine.get_Point(j);
                        if (polygon.PointInThisPoly(currPt))
                        {
                            ptInside = true;
                            break;
                        }
                    }
                    if (ptInside)
                    {
                        //we know part of the line is inside the polygon so add line to result file
                        shpIndex = resultSF.NumShapes;
                        if (resultSF.EditInsertShape(currLine, ref shpIndex) == false)
                        {
                            gErrorMsg = "Problem inserting shape into result file: " + resultSF.get_ErrorMsg(resultSF.LastErrorCode);
                            Debug.WriteLine(gErrorMsg);
                            Error.SetErrorMsg(gErrorMsg);
                            return(false);
                        }
                        //add the table values
                        int numFields = resultSF.NumFields;
                        for (int j = 0; j <= numFields - 1; j++)
                        {
                            if (resultSF.EditCellValue(j, shpIndex, inputSF.get_CellValue(j, i)) == false)
                            {
                                gErrorMsg = "Problem inserting value into DBF table: " + resultSF.get_ErrorMsg(resultSF.LastErrorCode);
                                Debug.WriteLine(gErrorMsg);
                                Error.SetErrorMsg(gErrorMsg);
                                return(false);
                            }
                        }                //end of looping through table
                    }                    //end of ptInside check
                    else
                    {
                        // Avoid using a temp file to test each individual file;
                        // instead, just see if the line crosses the polygon
                        if (LineCrossesPoly(ref currLine, ref polygon))
                        {
                            //part of the line lies within the polygon, add to result file
                            shpIndex = resultSF.NumShapes;
                            if (resultSF.EditInsertShape(currLine, ref shpIndex) == false)
                            {
                                gErrorMsg = "Problem inserting shape into result file: " + resultSF.get_ErrorMsg(resultSF.LastErrorCode);
                                Debug.WriteLine(gErrorMsg);
                                Error.SetErrorMsg(gErrorMsg);
                                return(false);
                            }
                            //add the table values
                            int numFields = resultSF.NumFields;
                            for (int j = 0; j <= numFields - 1; j++)
                            {
                                if (resultSF.EditCellValue(j, shpIndex, inputSF.get_CellValue(j, i)) == false)
                                {
                                    gErrorMsg = "Problem inserting value into DBF table: " + resultSF.get_ErrorMsg(resultSF.LastErrorCode);
                                    Debug.WriteLine(gErrorMsg);
                                    Error.SetErrorMsg(gErrorMsg);
                                    return(false);
                                }
                            } //end of looping through table
                        }     //end of successful cross
                    }         //end of else no points were found inside polygon
                }             //end of checking bounds
            }                 //end of looping through lines

            if (resultSF.NumShapes > 0)
            {
                if (resultSF.NumFields == 0 || !SkipMWShapeID)
                {
                    //add the ID field and values
                    if (Globals.DoInsertIDs(ref resultSF) == false)
                    {
                        return(false);
                    }
                }
                return(true);
            }
            else
            {
                return(false);
            }
        }