OgrProvider.cs
上传用户:sex100000
上传日期:2013-11-09
资源大小:1377k
文件大小:12k
源码类别:

GIS编程

开发平台:

C#

  1. /*
  2.  * Created by SharpDevelop.
  3.  * User: Christian
  4.  * Date: 29.04.2006
  5.  * Time: 10:06
  6.  * 
  7.  * To change this template use Tools | Options | Coding | Edit Standard Headers.
  8.  */
  9. using System;
  10. using System.Collections.ObjectModel;
  11. using OGR;
  12. namespace SharpMap.Data.Providers
  13. {
  14. /// <summary>
  15. /// Ogr provider for SharpMap
  16. /// using the C# SWIG wrapper of GDAL/OGR
  17.     /// <code>
  18.     /// SharpMap.Layers.VectorLayer vLayerOgr = new SharpMap.Layers.VectorLayer("MapInfoLayer");
  19.     /// vLayerOgr.DataSource = new SharpMap.Data.Providers.Ogr(@"D:GeoDatamyWorld.tab");
  20.     /// </code>
  21. /// </summary>
  22. public class Ogr : SharpMap.Data.Providers.IProvider, IDisposable
  23. {
  24. private SharpMap.Geometries.BoundingBox _bbox;
  25. private String m_Filename;
  26. private OGR.DataSource _OgrDataSource;
  27.         private OGR.Layer _OgrLayer;
  28.         /// <summary>
  29.         /// return the file name of the datasource
  30.         /// </summary>
  31. public string Filename
  32. get { return m_Filename; }
  33. set { m_Filename = value; }
  34. }
  35. #region constructor
  36.         /// <summary>
  37.         /// Loads a Ogr datasource with the specified layer
  38.         /// </summary>
  39.         /// <param name="Filename">datasource</param>
  40.         /// <param name="LayerName">name of layer</param>
  41.         public Ogr(string Filename, string LayerName)
  42.         {
  43.             this.Filename = Filename;
  44.             
  45.             OGR.ogr.RegisterAll();
  46.             _OgrDataSource =  OGR.ogr.Open(this.Filename, 1);
  47.             _OgrLayer = _OgrDataSource.GetLayerByName(LayerName);
  48.         }
  49.         
  50.         /// <summary>
  51.         /// Loads a Ogr datasource with the specified layer
  52.         /// </summary>
  53.         /// <param name="Filename">datasource</param>
  54.         /// <param name="LayerNum">number of layer</param>
  55.         public Ogr(string Filename, int LayerNum)
  56.         {
  57.          this.Filename = Filename;
  58.         
  59.             ogr.RegisterAll();
  60.             _OgrDataSource = OGR.ogr.Open(this.Filename, 0);
  61.             _OgrLayer = _OgrDataSource.GetLayerByIndex(LayerNum);
  62.         }
  63.         /// <summary>
  64.         /// Loads a Ogr datasource with the first layer
  65.         /// </summary>
  66.         /// <param name="Filename">datasource</param>
  67. public Ogr(string Filename)
  68.             : this(Filename, 0)
  69. {
  70. }
  71. #endregion
  72. #region IProvider Members
  73. /// <summary>
  74. /// Boundingbox of the dataset
  75. /// </summary>
  76. /// <returns>boundingbox</returns>
  77. public SharpMap.Geometries.BoundingBox GetExtents()
  78. {
  79. if(this._bbox==null)
  80. {
  81. OGR.Envelope _OgrEnvelope = new Envelope();
  82.                 int i = _OgrLayer.GetExtent(_OgrEnvelope, 1);
  83.                 this._bbox = new SharpMap.Geometries.BoundingBox(_OgrEnvelope.MinX,
  84.                                                                  _OgrEnvelope.MinY,
  85.                                                                  _OgrEnvelope.MaxX,
  86.                                                                  _OgrEnvelope.MaxY);
  87.             
  88. }
  89. return _bbox;
  90. }
  91. /// <summary>
  92. /// Returns the number of features in the dataset
  93. /// </summary>
  94. /// <returns>number of features</returns>
  95. public int GetFeatureCount()
  96. {
  97.             return _OgrLayer.GetFeatureCount(1);
  98. }
  99. /// <summary>
  100.         /// Returns the data associated with all the geometries that is within 'distance' of 'geom'
  101.         /// </summary>
  102.         /// <param name="geom"></param>
  103.         /// <param name="distance"></param>
  104.         /// <returns></returns>
  105.         [Obsolete("Use ExecuteIntersectionQuery instead")]
  106.         public FeatureDataTable QueryFeatures(SharpMap.Geometries.Geometry geom, double distance)
  107.         {
  108.          throw new NotImplementedException();
  109.         }
  110.         
  111.         /// <summary>
  112.         /// Returns the features that intersects with 'geom'
  113.         /// </summary>
  114.         /// <param name="geom">Geometry</param>
  115.         /// <returns>FeatureDataTable</returns>
  116.         public FeatureDataTable ExecuteIntersectionQuery(SharpMap.Geometries.Geometry geom)
  117.         {
  118. FeatureDataSet fds = new FeatureDataSet();
  119. ExecuteIntersectionQuery(geom, fds);
  120. return fds.Tables[0];
  121.         }
  122.         
  123. /// <summary>
  124.         /// Returns a FeatureDataRow based on a RowID
  125. /// </summary>
  126. /// <param name="RowID"></param>
  127.         /// <returns>FeatureDataRow</returns>
  128. public FeatureDataRow GetFeature(uint RowID)
  129. {
  130. throw new NotImplementedException();
  131. }
  132.         /// <summary>
  133.         /// Gets the connection ID of the datasource
  134.         /// </summary>
  135.         public string ConnectionID
  136.         {
  137.          get { throw new NotImplementedException(); }
  138.         }
  139.         
  140.         /// <summary>
  141.         /// Opens the datasource
  142.         /// </summary>
  143.         public void Open()
  144.         {
  145.             _IsOpen = true;
  146.         }
  147.         /// <summary>
  148.         /// Closes the datasource
  149.         /// </summary>
  150.         public void Close()
  151.         {
  152.             _IsOpen = false;
  153.         }    
  154.         
  155.         private bool _IsOpen;
  156.         /// <summary>
  157.         /// Returns true if the datasource is currently open
  158.         /// </summary>
  159.         public bool IsOpen
  160.         {
  161.             get { return _IsOpen; }
  162.         }
  163.         /// <summary>
  164.         /// Returns all features with the view box
  165.         /// </summary>
  166.         /// <param name="bbox">view box</param>
  167.         /// <param name="ds">FeatureDataSet to fill data into</param>
  168. [Obsolete("Use ExecuteIntersectionQuery(BoundingBox,FeatureDataSet) instead")]
  169. public void GetFeaturesInView(SharpMap.Geometries.BoundingBox bbox, FeatureDataSet ds)
  170.         {
  171. ExecuteIntersectionQuery(bbox, ds);
  172.         }
  173.         
  174.         /// <summary>
  175.         /// Returns geometry Object IDs whose bounding box intersects 'bbox'
  176.         /// </summary>
  177.         /// <param name="bbox"></param>
  178.         /// <returns></returns>
  179.         public Collection<uint> GetObjectIDsInView(SharpMap.Geometries.BoundingBox bbox)
  180.         {
  181.             _OgrLayer.SetSpatialFilterRect(bbox.Min.X, bbox.Min.Y, bbox.Max.X, bbox.Max.Y);
  182. OGR.Feature _OgrFeature = null;
  183. _OgrLayer.ResetReading();
  184.             Collection<uint> _ObjectIDs = new Collection<uint>();
  185. while ( (_OgrFeature = _OgrLayer.GetNextFeature()) != null)
  186. {
  187. _ObjectIDs.Add((uint)_OgrFeature.GetFID());
  188.          _OgrFeature.Dispose();
  189. }
  190. return _ObjectIDs;        
  191.         }
  192.         
  193.         /// <summary>
  194.         /// Returns the geometry corresponding to the Object ID
  195.         /// </summary>
  196.         /// <param name="oid">Object ID</param>
  197.         /// <returns>geometry</returns>
  198.         public SharpMap.Geometries.Geometry GetGeometryByID(uint oid)
  199. {
  200.          using (OGR.Feature _OgrFeature = _OgrLayer.GetFeature((int)oid))
  201. return this.ParseOgrGeometry(_OgrFeature.GetGeometryRef());
  202.         }
  203.         
  204.         /// <summary>
  205. /// Returns geometries within the specified bounding box
  206. /// </summary>
  207. /// <param name="bbox"></param>
  208. /// <returns></returns>
  209.         public Collection<SharpMap.Geometries.Geometry> GetGeometriesInView(SharpMap.Geometries.BoundingBox bbox)
  210.         {
  211.             Collection<SharpMap.Geometries.Geometry> geoms = new Collection<SharpMap.Geometries.Geometry>();        
  212.     
  213.          _OgrLayer.SetSpatialFilterRect(bbox.Left, bbox.Bottom, bbox.Right, bbox.Top);
  214. OGR.Feature _OgrFeature = null;
  215. _OgrLayer.ResetReading();
  216. while ( (_OgrFeature = _OgrLayer.GetNextFeature()) != null)
  217. {
  218. geoms.Add(this.ParseOgrGeometry(_OgrFeature.GetGeometryRef()));
  219. _OgrFeature.Dispose();
  220. }
  221. return geoms;
  222.         }
  223.         
  224.         private int _SRID = -1;
  225.         
  226.         /// <summary>
  227.         /// The spatial reference ID (CRS)
  228.         /// </summary>
  229.         public int SRID
  230.         {
  231.             get { return _SRID; }
  232.             set { _SRID = value; }
  233.         }       
  234.         
  235. /// <summary>
  236. /// Returns the data associated with all the geometries that are intersected by 'geom'
  237. /// </summary>
  238. /// <param name="bbox">Geometry to intersect with</param>
  239. /// <param name="ds">FeatureDataSet to fill data into</param>
  240. public void ExecuteIntersectionQuery(SharpMap.Geometries.BoundingBox bbox, FeatureDataSet ds)
  241. {
  242. FeatureDataTable myDt = new FeatureDataTable();
  243. _OgrLayer.SetSpatialFilterRect(bbox.Left, bbox.Bottom, bbox.Right, bbox.Top);
  244. //reads the column definition of the layer/feature
  245. this.ReadColumnDefinition(myDt, _OgrLayer);
  246. OGR.Feature _OgrFeature;
  247. _OgrLayer.ResetReading();
  248. while ((_OgrFeature = _OgrLayer.GetNextFeature()) != null)
  249. {
  250. FeatureDataRow _dr = myDt.NewRow();
  251. for (int iField = 0; iField < _OgrFeature.GetFieldCount(); iField++)
  252. {
  253. if (myDt.Columns[iField].DataType == System.Type.GetType("System.String"))
  254. _dr[iField] = _OgrFeature.GetFieldAsString(iField);
  255. else if (myDt.Columns[iField].GetType() == System.Type.GetType("System.Int32"))
  256. _dr[iField] = _OgrFeature.GetFieldAsInteger(iField);
  257. else if (myDt.Columns[iField].GetType() == System.Type.GetType("System.Double"))
  258. _dr[iField] = _OgrFeature.GetFieldAsDouble(iField);
  259. }
  260. _dr.Geometry = this.ParseOgrGeometry(_OgrFeature.GetGeometryRef());
  261. myDt.AddRow(_dr);
  262. }
  263. ds.Tables.Add(myDt);
  264. }
  265. /// <summary>
  266. /// Returns the data associated with all the geometries that are intersected by 'geom'
  267. /// </summary>
  268. /// <param name="geom">Geometry to intersect with</param>
  269. /// <param name="ds">FeatureDataSet to fill data into</param>
  270. public void ExecuteIntersectionQuery(SharpMap.Geometries.Geometry geom, FeatureDataSet ds)
  271. {
  272. throw new NotImplementedException();
  273. }
  274.         
  275. #endregion
  276. #region Disposers and finalizers
  277. private bool disposed = false;
  278. /// <summary>
  279. /// Disposes the object
  280. /// </summary>
  281. public void Dispose()
  282. {
  283. Dispose(true);
  284. GC.SuppressFinalize(this);
  285. }
  286. internal void Dispose(bool disposing)
  287. {
  288. if (!disposed)
  289. {
  290. if (disposing && _OgrDataSource!=null)
  291. {
  292. _OgrDataSource.Dispose();
  293. }
  294. disposed = true;
  295. }
  296. }
  297. /// <summary>
  298. /// Finalizer
  299. /// </summary>
  300. ~Ogr()
  301. {
  302.             Close();
  303. Dispose();
  304. }
  305. #endregion
  306.         #region private methods for data conversion sharpmap <--> ogr
  307.         /// <summary>
  308.         /// Reads the field types from the OgrFeatureDefinition -> OgrFieldDefinition
  309.         /// </summary>
  310.         /// <param name="fdt">FeatureDatatTable</param>
  311.         /// <param name="oLayer">OgrLayer</param>
  312.         private void ReadColumnDefinition(FeatureDataTable fdt, OGR.Layer oLayer)
  313.         {
  314.          using(OGR.FeatureDefn _OgrFeatureDefn = oLayer.GetLayerDefn())
  315.          {
  316.             int iField;           
  317.             
  318.             for (iField = 0; iField < _OgrFeatureDefn.GetFieldCount(); iField++)
  319.             {
  320.              using(OGR.FieldDefn _OgrFldDef = _OgrFeatureDefn.GetFieldDefn(iField))
  321.              {
  322.                 switch (_OgrFldDef.GetFieldType())
  323.                 {
  324.                     case 0:
  325.                  fdt.Columns.Add(_OgrFldDef.GetName(), System.Type.GetType("System.Int32"));
  326.                         break;
  327.                     case 2:
  328.                         fdt.Columns.Add(_OgrFldDef.GetName(), System.Type.GetType("System.Double"));
  329.                         break;
  330.                     case 4:
  331.                         fdt.Columns.Add(_OgrFldDef.GetName(), System.Type.GetType("System.String"));
  332.                         break;
  333.                     case 6:
  334.                         fdt.Columns.Add(_OgrFldDef.GetName(), System.Type.GetType("System.String"));
  335.                         break;
  336.                 }
  337.              }
  338.             }
  339.          }
  340.         }
  341.         
  342.         private SharpMap.Geometries.Geometry ParseOgrGeometry(OGR.Geometry OgrGeometry)
  343.         {
  344. byte[] wkbBuffer = new byte[OgrGeometry.WkbSize()];
  345. int i = OgrGeometry.ExportToWkb(wkbBuffer);
  346. return SharpMap.Converters.WellKnownBinary.GeometryFromWKB.Parse(wkbBuffer);
  347.         }
  348.         #endregion
  349. }
  350. }