Пример #1
0
 /// <summary>
 /// Returns an ShapefileDataReader representing the data in a shapefile.
 /// </summary>
 /// <param name="filename">The filename (minus the . and extension) to read.</param>
 /// <param name="geometryFactory">The geometry factory to use when creating the objects.</param>
 /// <returns>An ShapefileDataReader representing the data in the shape file.</returns>
 public static ShapefileDataReader CreateDataReader(string filename, GeometryFactory geometryFactory)
 {
     if (filename == null)
         throw new ArgumentNullException("filename");
     if (geometryFactory == null)
         throw new ArgumentNullException("geometryFactory");
     ShapefileDataReader shpDataReader= new ShapefileDataReader(filename,geometryFactory);
     return shpDataReader;
 }
Пример #2
0
        /// <summary>
        /// Initializes a new instance of the <see cref="T:ShapefileDataWriter"/> class.
        /// </summary>
        /// <param name="fileName">File path without any extension</param>
        /// <param name="geometryFactory"></param>
        public ShapefileDataWriter(string fileName, GeometryFactory geometryFactory)
        {
            this.geometryFactory = geometryFactory;

            // Files
            shpFile = fileName;
            dbfFile = fileName + ".dbf";

            // Writers
            shapeWriter = new ShapefileWriter(geometryFactory);
            dbaseWriter = new DbaseFileWriter(dbfFile);
        }
Пример #3
0
        /// <summary>
        /// Creates a DataTable representing the information in a shape file.
        /// </summary>
        /// <param name="filename">The filename (minus the . and extension) to read.</param>
        /// <param name="tableName">The name to give to the table.</param>
        /// <param name="geometryFactory">The geometry factory to use when creating the objects.</param>
        /// <returns>DataTable representing the data </returns>
        public static DataTable CreateDataTable(string filename, string tableName, GeometryFactory geometryFactory)
        {
            if (filename == null)
                throw new ArgumentNullException("filename");
            if (tableName == null)
                throw new ArgumentNullException("tableName");
            if (geometryFactory == null)
                throw new ArgumentNullException("geometryFactory");

            ShapefileDataReader shpfileDataReader= new ShapefileDataReader(filename, geometryFactory);
            DataTable table = new DataTable(tableName);

            // use ICustomTypeDescriptor to get the properies/ fields. This way we can get the
            // length of the dbase char fields. Because the dbase char field is translated into a string
            // property, we lost the length of the field. We need to know the length of the
            // field when creating the table in the database.

            IEnumerator enumerator = shpfileDataReader.GetEnumerator();
            bool moreRecords = enumerator.MoveNext();
            ICustomTypeDescriptor typeDescriptor  = (ICustomTypeDescriptor) enumerator.Current;
            foreach (PropertyDescriptor property in typeDescriptor.GetProperties())
            {
                ColumnStructure column = (ColumnStructure) property;
                Type fieldType = column.PropertyType;
                DataColumn datacolumn = new DataColumn(column.Name, fieldType);
                if (fieldType== typeof(string))
                    // use MaxLength to pass the length of the field in the dbase file
                    datacolumn.MaxLength=column.Length;
                table.Columns.Add( datacolumn );
            }

            // add the rows - need a do-while loop because we read one row in order to determine the fields
            int iRecordCount = 0;
            object[] values = new object[shpfileDataReader.FieldCount];
            do
            {
                iRecordCount++;
                shpfileDataReader.GetValues(values);
                table.Rows.Add(values);
                moreRecords = enumerator.MoveNext();
            }
            while (moreRecords);
            return table;
        }
Пример #4
0
 /// <summary>
 /// Initialize reader with the given <c>GeometryFactory</c>.
 /// </summary>
 /// <param name="factory"></param>
 public GMLReader(GeometryFactory factory)
 {
     this.factory = factory;
 }
Пример #5
0
        /// <summary>
        /// Imports a shapefile into a database table.
        /// </summary>
        /// <remarks>
        /// This method assumes a table has already been crated in the database.
        /// Calling this method does not close the connection that is passed in.
        /// </remarks>
        /// <param name="filename"></param>
        /// <param name="connectionstring"></param>
        /// <param name="tableName"></param>
        /// <returns></returns>
        public static int ImportShapefile(string filename, string connectionstring, string tableName)
        {
            using (SqlConnection connection = new SqlConnection(connectionstring))
            {
                int rowsAdded = -1;
                PrecisionModel pm = new PrecisionModel();
                GeometryFactory geometryFactory = new GeometryFactory(pm, -1);

                DataTable shpDataTable = Topology.IO.Shapefile.CreateDataTable(filename, tableName, geometryFactory);
                string createTableSql = CreateDbTable(shpDataTable, true);

                SqlCommand createTableCommand = new SqlCommand(createTableSql, connection);
                connection.Open();
                createTableCommand.ExecuteNonQuery();

                string sqlSelect = String.Format("select * from {0}", tableName);
                SqlDataAdapter selectCommand = new SqlDataAdapter(sqlSelect, connection);

                // use a data adaptor - saves donig the inserts ourselves
                SqlDataAdapter dataAdapter = new SqlDataAdapter();
                dataAdapter.SelectCommand = new SqlCommand(sqlSelect, connection);
                SqlCommandBuilder custCB = new SqlCommandBuilder(dataAdapter);
                DataSet ds = new DataSet();

                // fill dataset
                dataAdapter.Fill(ds, shpDataTable.TableName);

                // copy rows from our datatable to the empty table in the DataSet
                int i = 0;
                foreach (DataRow row in shpDataTable.Rows)
                {
                    DataRow newRow = ds.Tables[0].NewRow();
                    newRow.ItemArray = row.ItemArray;
                    //gotcha! - new row still needs to be added to the table.
                    //NewRow() just creates a new row with the same schema as the table. It does
                    //not add it to the table.
                    ds.Tables[0].Rows.Add(newRow);
                    i++;
                }

                // update all the rows in batch
                rowsAdded = dataAdapter.Update(ds, shpDataTable.TableName);
                int iRows = shpDataTable.Rows.Count;
                Debug.Assert(rowsAdded != iRows, String.Format("{0} of {1] rows were added to the database.", rowsAdded, shpDataTable.Rows.Count));
                return rowsAdded;
            }
        }
Пример #6
0
 /// <summary>
 /// 
 /// </summary>
 /// <param name="factory"></param>
 public MyShapeFileReader(GeometryFactory factory)
 {
     shapeReader = new ShapeReader(factory);
 }
Пример #7
0
        /// <summary>
        /// Writes a Geometry to the given binary wirter.
        /// </summary>
        /// <param name="geometry">The geometry to write.</param>
        /// <param name="file">The file stream to write to.</param>
        /// <param name="geometryFactory">The geometry factory to use.</param>
        public override void Write(IGeometry geometry, System.IO.BinaryWriter file, IGeometryFactory geometryFactory)
        {
            // This check seems to be not useful and slow the operations...
            // if (!geometry.IsValid)
            // Trace.WriteLine("Invalid polygon being written.");

            IGeometryCollection multi = null;
            if (geometry is IGeometryCollection)
                multi = (IGeometryCollection) geometry;
            else
            {
                GeometryFactory gf = new GeometryFactory(geometry.PrecisionModel);
                multi = gf.CreateMultiPolygon(new IPolygon[] { (IPolygon) geometry, } );
            }

            file.Write(int.Parse(Enum.Format(typeof(ShapeGeometryTypes), this.ShapeType, "d")));

            IEnvelope box = multi.EnvelopeInternal;
            IEnvelope bounds = ShapeHandler.GetEnvelopeExternal(geometryFactory.PrecisionModel,  box);
            file.Write(bounds.MinX);
            file.Write(bounds.MinY);
            file.Write(bounds.MaxX);
            file.Write(bounds.MaxY);

            int numParts = GetNumParts(multi);
            int numPoints = multi.NumPoints;
            file.Write(numParts);
            file.Write(numPoints);

            // write the offsets to the points
            int offset=0;
            for (int part = 0; part < multi.NumGeometries; part++)
            {
                // offset to the shell points
                IPolygon polygon = (IPolygon) multi.Geometries[part];
                file.Write(offset);
                offset = offset + polygon.ExteriorRing.NumPoints;

                // offstes to the holes
                foreach (ILinearRing ring in polygon.InteriorRings)
                {
                    file.Write(offset);
                    offset = offset + ring.NumPoints;
                }
            }

            // write the points
            for (int part = 0; part < multi.NumGeometries; part++)
            {
                IPolygon poly = (IPolygon) multi.Geometries[part];
                ICoordinate[] points = poly.ExteriorRing.Coordinates;
                WriteCoords(new CoordinateList(points), file, geometryFactory);
                foreach(ILinearRing ring in poly.InteriorRings)
                {
                    ICoordinate[] points2 = ring.Coordinates;
                    WriteCoords(new CoordinateList(points2), file, geometryFactory);
                }
            }
        }
Пример #8
0
 /// <summary>
 /// Initialize reader with the given <c>GeometryFactory</c>.
 /// </summary>
 /// <param name="factory"></param>
 public ShapeReader(GeometryFactory factory)
 {
     this.factory = factory;
 }
Пример #9
0
        /* BEGIN ADDED BY MPAUL42: monoGIS team */

        /// <summary>
        /// Constructs a <c>Polygon</c> with the given exterior boundary.
        /// </summary>
        /// <param name="shell">
        /// The outer boundary of the new <c>Polygon</c>,
        /// or <c>null</c> or an empty <c>LinearRing</c> if the empty
        /// polygon is to be created.
        /// </param>
        /// <param name="factory"></param>
        public Polygon(LinearRing shell, GeometryFactory factory) : this(shell, null, factory)
        {
        }
 /// <summary>
 /// 
 /// </summary>
 /// <param name="geom"></param>
 /// <returns></returns>
 public IGeometry Reduce(IGeometry geom)
 {
     GeometryEditor geomEdit;
     if (changePrecisionModel)
     {
         GeometryFactory newFactory = new GeometryFactory(newPrecisionModel);
         geomEdit = new GeometryEditor(newFactory);
     }
     else
     // don't change point factory
     geomEdit = new GeometryEditor();
     return geomEdit.Edit(geom, new PrecisionReducerCoordinateOperation(this));
 }
Пример #11
0
 /* BEGIN ADDED BY MPAUL42: monoGIS team */
 /// <summary>
 /// Constructs a <c>Polygon</c> with the given exterior boundary.
 /// </summary>
 /// <param name="shell">
 /// The outer boundary of the new <c>Polygon</c>,
 /// or <c>null</c> or an empty <c>LinearRing</c> if the empty
 /// polygon is to be created.
 /// </param>
 /// <param name="factory"></param>
 public Polygon(LinearRing shell, GeometryFactory factory)
     : this(shell, null, factory)
 {
 }
Пример #12
0
 /// <summary>  
 /// Creates a <coordinate>GDBReader</coordinate> that creates objects using the given
 /// <coordinate>GeometryFactory</coordinate>.
 /// </summary>
 /// <param name="factory">The factory used to create <coordinate>Geometry</coordinate>s.</param>
 public GDBReader(GeometryFactory factory)
     : base(factory)
 {
 }
Пример #13
0
 /// <summary>
 /// Create a shape factory which will create shapes using the given GeometryFactory.
 /// </summary>
 /// <param name="geomFact">The factory to use.</param>
 public GeometricShapeFactory(GeometryFactory geomFact)
 {
     this.geomFact = geomFact;
 }