示例#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);
        }
示例#2
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);
        }
示例#3
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());
            }
        }