예제 #1
0
        public TestShape(string filename)
        {
            int cnt  = 0;
            int pcnt = 0;

            try
            {
//              string	drv = "Driver={Microsoft dBase Driver (*.dbf)};DBQ=";
//              OdbcConnection con = new OdbcConnection(drv + Path.GetDirectoryName( filename ));
//              con.Open();

                using (ShapeFileReader sr = new ShapeFileReader(filename))
                {
                    while (sr.Read())
                    {
                        cnt++;
                        if (sr.shapeType == ShapeUnit.Polygon)
                        {
                            ShapePolygon sp = (ShapePolygon)sr.Get( );

                            for (int p = 0; p < sp.numParts; p++)
                            {
                                uint max = sp.numPoints;
                                if (p < sp.numParts - 1)
                                {
                                    max = sp.parts[p + 1];
                                }

                                uint len = sp.numPoints;
                                if (p < sp.numParts - 1)
                                {
                                    len = sp.parts[p + 1] - sp.parts[p];
                                }
                                else
                                {
                                    len = sp.numPoints - sp.parts[p];
                                }

                                Point[] pnt = new Point[len];
                                int     j   = 0;
                                for (uint i = sp.parts[p]; i < max; i++)
                                {
                                    pnt[j].X = (int)sp.points[i].X;
                                    pnt[j].Y = (int)sp.points[i].Y;
                                    j++;
                                }
                                pcnt += (int)len;
                            }
                        }
                        if (sr.shapeType == ShapeUnit.PolyLine)
                        {
                            ShapePolyline sp = (ShapePolyline)sr.Get();

                            for (int p = 0; p < sp.numParts; p++)
                            {
                                uint max = sp.numPoints;
                                if (p < sp.numParts - 1)
                                {
                                    max = sp.parts[p + 1];
                                }

                                uint len = sp.numPoints;
                                if (p < sp.numParts - 1)
                                {
                                    len = sp.parts[p + 1] - sp.parts[p];
                                }
                                else
                                {
                                    len = sp.numPoints - sp.parts[p];
                                }

                                Point[] pnt = new Point[len];
                                int     j   = 0;
                                for (uint i = sp.parts[p]; i < max; i++)
                                {
                                    pnt[j].X = (int)sp.points[i].X;
                                    pnt[j].Y = (int)sp.points[i].Y;
                                    j++;
                                }
                                pcnt += (int)len;
                            }
                        }

/*                        string str = null;
 *                      object[] an = sr.GetAttrNames();
 *                      object[] tp = sr.GetAttrTypes();
 *                      for (int i = 0; i < an.Length; i++)
 *                          str += an[i] + " " + tp[i] + "\n";
 *                      Console.WriteLine(str, "»мена полей и типов");
 */
                    }
                }
//                con.Close();
            }
            catch (Exception e)
            {
                Console.WriteLine(e.ToString());
            }
            Console.WriteLine("Read " + cnt.ToString() + " objects, " + pcnt.ToString() + " points",
                              this.GetType().ToString());
        }
예제 #2
0
        public GLib Load()
        {
            foreach (string filePath in fileNames)
            {
                using (ShapeFileReader sr = new ShapeFileReader(filePath))
                {
                    UpdateBounds(sr.bound);
                }
            }
            if (!boundsUpdated)
            {
                return(null);
            }

            SetTransform();
            CreateLib();

            foreach (string filePath in fileNames)
            {
                using (ShapeFileReader sr = new ShapeFileReader(filePath))
                {
                    //					object[] attrs;
                    if (!CreateType(sr.GetUnitType()))
                    {
                        continue;
                    }
                    curType.Name = Path.GetFileNameWithoutExtension(filePath);
                    while (sr.Read())
                    {
                        switch (sr.GetUnitType())
                        {
                        case ShapeUnit.Arc:
                            //								Read((ShapeArc)sr.Get());
                            break;

                        case ShapeUnit.Point:
                            Read((ShapePoint)sr.Get());
                            break;

                        case ShapeUnit.Polygon:
                            Read((ShapePolygon)sr.Get());
                            break;

//							case ShapeUnit.PointGroup:
                        //								Read((ShapePointGroup)sr.Get());
//								break;
                        case ShapeUnit.PolyLine:
                            Read((ShapePolyline)sr.Get());
                            break;
                        }
                    }
                }
            }
            lib.SMin = 10;
            if (lib.Bounds.IsNull)
            {
                lib.SetBounds(new Rect(0, 0, 1000000, 1000000));
            }
            lib.SMax = Geomethod.GeoLib.GeoLibUtils.RoundScale(lib.Bounds.MaxSize / 10);
            lib.Scales.InitScales();
            return(lib);
        }
예제 #3
0
        public TestShape()
        {
            /*			string	drv = "Driver={Microsoft dBase Driver (*.dbf)};DBQ=";
             *
             *          //		«десь конструктор с connection
             *
             * //			string	filename1 = @"f:\dima\shape\bk_st.shp";
             *          string	filename1 = @"f:\dima\shape\detroit\hist.shp";
             * //			string	filename1 = @"f:\dima\shape\detroit\roads.shp";
             *
             *          OdbcConnection	con   =  new OdbcConnection( drv + Path.GetDirectoryName( filename1 ) );
             *          con.Open();
             *
             *          using( ShapeFileReader	sr = new ShapeFileReader( filename1, con ) )
             *          {
             *              string	str =  null;
             *              object[]	an = sr.GetAttrNames();
             *              object[]	tp = sr.GetAttrTypes();
             *              for( int i = 0; i < an.Length; i++ )
             *                  str += an[i] + " " + tp[i] + "\n";
             *              Console.WriteLine( str, "»мена полей и типов" );
             *
             *              int	cnt = 0;
             *              while( sr.Read() )
             *              {
             *                  if( sr.shapeType == ShapeUnit.Point )
             *                  {
             *                      object[]	attrs;
             *                      ShapePoint	sp = (ShapePoint)sr.Get( out attrs );
             *                      if( cnt < 2 )
             *                      {
             *                          Console.WriteLine( sp.point.X.ToString() + " " + sp.point.Y.ToString(), " оординаты" );
             *                          string	at = null;
             *                          for( int i = 0; i < attrs.Length; i++ )
             *                              at += attrs[i].ToString() + "\n";
             *                          Console.WriteLine( at, "јтрибуты" );
             *                      }
             *                  }
             *                  cnt++;
             *              }
             *              string	Bound = sr.shapeType.ToString()  + "\n " +
             *                  sr.bound.maxx.ToString() + "\n " +
             *                  sr.bound.maxy.ToString() + "\n " +
             *                  sr.bound.minx.ToString() + "\n " +
             *                  sr.bound.miny.ToString() + "\n " +
             *                  cnt.ToString();
             *              Console.WriteLine( Bound, "“ип, границы и количество объектов" );
             *
             *          }
             *          con.Close();
             */
            //			«десь конструктор без connection

            string filename2 = @"f:\dima\shape\bk_tb.shp";

            using (ShapeFileReader sr = new ShapeFileReader(filename2))
            {
                while (sr.Read())
                {
                    if (sr.shapeType == ShapeUnit.Polygon)
                    {
                        ShapePolygon sp = (ShapePolygon)sr.Get( );

                        for (int p = 0; p < sp.numParts; p++)
                        {
                            uint max = sp.numPoints;
                            if (p < sp.numParts - 1)
                            {
                                max = sp.parts[p + 1];
                            }

                            uint len = sp.numPoints;
                            if (p < sp.numParts - 1)
                            {
                                len = sp.parts[p + 1] - sp.parts[p];
                            }
                            else
                            {
                                len = sp.numPoints - sp.parts[p];
                            }

                            Point[] pnt = new Point[len];
                            int     j   = 0;
                            for (uint i = sp.parts[p]; i < max; i++)
                            {
                                pnt[j].X = (int)sp.points[i].X;
                                pnt[j].Y = (int)sp.points[i].Y;
                                j++;
                            }
                        }
                    }
                }
            }

            //		«десь конструктор с connection и таблицей

            /*			try
             *                      {
             * //				string	filename3 = @"f:\dima\shape\bk_st.shp";
             * //				string	filename3 = @"f:\dima\shape\detroit\roads.shp";
             *      string	filename3 = @"f:\dima\shape\detroit\hist.shp";
             *      string	drvJet	= @"Provider=Microsoft.Jet.OLEDB.4.0;";
             *      string	ext		= @";Extended Properties=dBASE IV;User ID=Admin;Password="******"Data Source=" + Path.GetDirectoryName( filename3 ) + ext;
             *      OleDbConnection	con3 = new OleDbConnection(  str2 );
             *      con3.Open();
             *
             *      DataTable	dt = new DataTable();
             *
             *      ShapeFileReader	sr1 = new ShapeFileReader( filename3, con3, dt );
             *      sr1.Dispose();
             *      con3.Close();
             *
             *      for( int i = 0; i < dt.Rows.Count; i++ )
             *      {
             *              string	str = null;
             *              for( int j = 0;  j < dt.Columns.Count; j++ )
             *                      str += dt.Columns[j].ColumnName + "\t = " + dt.Rows[i][j].ToString() + "\n";
             *              Console.WriteLine( str, dt.TableName + "[" + i + "]" );
             *
             *              if( i > 2 )
             *                      break;
             *      }
             *
             * }
             * catch( Exception e )
             * {
             *      Console.WriteLine( e.ToString() );
             * }
             */
        }