📄 geometryprovider.cs
字号:
// Copyright 2006 - Morten Nielsen (www.iter.dk)
//
// This file is part of SharpMap.
// SharpMap is free software; you can redistribute it and/or modify
// it under the terms of the GNU Lesser General Public License as published by
// the Free Software Foundation; either version 2 of the License, or
// (at your option) any later version.
//
// SharpMap is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
// GNU Lesser General Public License for more details.
//
// You should have received a copy of the GNU Lesser General Public License
// along with SharpMap; if not, write to the Free Software
// Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
using System;
using System.Collections.Generic;
using System.Collections.ObjectModel;
using System.Text;
using SharpMap.Geometries;
using SharpMap.Data;
namespace SharpMap.Data.Providers
{
/// <summary>
/// Datasource for storing a limited set of geometries.
/// </summary>
/// <remarks>
/// <para>The GeometryProvider doesn抰 utilize performance optimizations of spatial indexing,
/// and thus is primarily meant for rendering a limited set of Geometries.</para>
/// <para>A common use of the GeometryProvider is for highlighting a set of selected features.</para>
/// <example>
/// The following example gets data within a BoundingBox of another datasource and adds it to the map.
/// <code lang="C#">
/// List<Geometry> geometries = myMap.Layers[0].DataSource.GetGeometriesInView(myBox);
/// VectorLayer laySelected = new VectorLayer("Selected Features");
/// laySelected.DataSource = new GeometryProvider(geometries);
/// laySelected.Style.Outline = new Pen(Color.Magenta, 3f);
/// laySelected.Style.EnableOutline = true;
/// myMap.Layers.Add(laySelected);
/// </code>
/// </example>
/// <example>
/// Adding points of interest to the map. This is useful for vehicle tracking etc.
/// <code lang="C#">
/// List<SharpMap.Geometries.Geometry> geometries = new List<SharpMap.Geometries.Geometry>();
/// //Add two points
/// geometries.Add(new SharpMap.Geometries.Point(23.345,64.325));
/// geometries.Add(new SharpMap.Geometries.Point(23.879,64.194));
/// SharpMap.Layers.VectorLayer layerVehicles = new SharpMap.Layers.VectorLayer("Vechicles");
/// layerVehicles.DataSource = new SharpMap.Data.Providers.GeometryProvider(geometries);
/// layerVehicles.Style.Symbol = Bitmap.FromFile(@"C:\data\car.gif");
/// myMap.Layers.Add(layerVehicles);
/// </code>
/// </example>
/// </remarks>
public class GeometryProvider : SharpMap.Data.Providers.IProvider, IDisposable
{
private Collection<Geometry> _Geometries;
/// <summary>
/// Gets or sets the geometries this datasource contains
/// </summary>
public Collection<Geometry> Geometries
{
get { return _Geometries; }
set { _Geometries = value; }
}
#region constructors
/// <summary>
/// Initializes a new instance of the <see cref="GeometryProvider"/>
/// </summary>
/// <param name="geometries">Set of geometries that this datasource should contain</param>
public GeometryProvider(Collection<Geometry> geometries)
{
_Geometries = geometries;
}
/// <summary>
/// Initializes a new instance of the <see cref="GeometryProvider"/>
/// </summary>
/// <param name="feature">Feature to be in this datasource</param>
public GeometryProvider(FeatureDataRow feature)
{
_Geometries = new Collection<Geometry>();
_Geometries.Add(feature.Geometry);
}
/// <summary>
/// Initializes a new instance of the <see cref="GeometryProvider"/>
/// </summary>
/// <param name="features">Features to be included in this datasource</param>
public GeometryProvider(FeatureDataTable features)
{
_Geometries = new Collection<Geometry>();
for (int i = 0; i < features.Count;i++ )
_Geometries.Add(features[i].Geometry);
}
/// <summary>
/// Initializes a new instance of the <see cref="GeometryProvider"/>
/// </summary>
/// <param name="geometry">Geometry to be in this datasource</param>
public GeometryProvider(Geometry geometry)
{
_Geometries = new Collection<Geometry>();
_Geometries.Add(geometry);
}
/// <summary>
/// Initializes a new instance of the <see cref="GeometryProvider"/>
/// </summary>
/// <param name="wellKnownBinaryGeometry"><see cref="SharpMap.Geometries.Geometry"/> as Well-known Binary to be included in this datasource</param>
public GeometryProvider(byte[] wellKnownBinaryGeometry) : this(SharpMap.Converters.WellKnownBinary.GeometryFromWKB.Parse(wellKnownBinaryGeometry))
{
}
/// <summary>
/// Initializes a new instance of the <see cref="GeometryProvider"/>
/// </summary>
/// <param name="wellKnownTextGeometry"><see cref="SharpMap.Geometries.Geometry"/> as Well-known Text to be included in this datasource</param>
public GeometryProvider(string wellKnownTextGeometry) : this(SharpMap.Converters.WellKnownText.GeometryFromWKT.Parse(wellKnownTextGeometry))
{
}
#endregion
#region IProvider Members
/// <summary>
/// Returns features within the specified bounding box
/// </summary>
/// <param name="bbox"></param>
/// <returns></returns>
public Collection<Geometry> GetGeometriesInView(BoundingBox bbox)
{
Collection<Geometry> list = new Collection<Geometry>();
for (int i = 0; i < _Geometries.Count; i++)
if(!_Geometries[i].IsEmpty())
if (_Geometries[i].GetBoundingBox().Intersects(bbox))
list.Add(_Geometries[i]);
return list;
}
/// <summary>
/// Returns all objects whose boundingbox intersects 'bbox'.
/// </summary>
/// <param name="bbox"></param>
/// <returns></returns>
public Collection<uint> GetObjectIDsInView(BoundingBox bbox)
{
Collection<uint> list = new Collection<uint>();
for (int i = 0; i < _Geometries.Count; i++)
if(_Geometries[i].GetBoundingBox().Intersects(bbox))
list.Add((uint)i);
return list;
}
/// <summary>
/// Returns the geometry corresponding to the Object ID
/// </summary>
/// <param name="oid">Object ID</param>
/// <returns>geometry</returns>
public Geometry GetGeometryByID(uint oid)
{
return _Geometries[(int)oid];
}
/// <summary>
/// Throws an NotSupportedException. Attribute data is not supported by this datasource
/// </summary>
/// <param name="geom"></param>
/// <param name="ds">FeatureDataSet to fill data into</param>
public void ExecuteIntersectionQuery(Geometry geom, FeatureDataSet ds)
{
throw new NotSupportedException("Attribute data is not supported by the GeometryProvider.");
}
/// <summary>
/// Throws an NotSupportedException. Attribute data is not supported by this datasource
/// </summary>
/// <param name="box"></param>
/// <param name="ds">FeatureDataSet to fill data into</param>
public void ExecuteIntersectionQuery(SharpMap.Geometries.BoundingBox box, FeatureDataSet ds)
{
throw new NotSupportedException("Attribute data is not supported by the GeometryProvider.");
}
/// <summary>
/// Returns the number of features in the dataset
/// </summary>
/// <returns>number of features</returns>
public int GetFeatureCount()
{
return _Geometries.Count;
}
/// <summary>
/// Throws an NotSupportedException. Attribute data is not supported by this datasource
/// </summary>
/// <param name="RowID"></param>
/// <returns></returns>
public FeatureDataRow GetFeature(uint RowID)
{
throw new NotSupportedException("Attribute data is not supported by the GeometryProvider.");
}
/// <summary>
/// Boundingbox of dataset
/// </summary>
/// <returns>boundingbox</returns>
public BoundingBox GetExtents()
{
if (_Geometries.Count == 0)
return null;
BoundingBox box = null;// _Geometries[0].GetBoundingBox();
for (int i = 0; i < _Geometries.Count; i++)
if (!_Geometries[i].IsEmpty())
box = box==null ? _Geometries[i].GetBoundingBox() : box.Join(_Geometries[i].GetBoundingBox());
return box;
}
/// <summary>
/// Gets the connection ID of the datasource
/// </summary>
/// <remarks>
/// The ConnectionID is meant for Connection Pooling which doesn't apply to this datasource. Instead
/// <c>String.Empty</c> is returned.
/// </remarks>
public string ConnectionID
{
get { return String.Empty; }
}
/// <summary>
/// Opens the datasource
/// </summary>
public void Open()
{
//Do nothing;
}
/// <summary>
/// Closes the datasource
/// </summary>
public void Close()
{
//Do nothing;
}
/// <summary>
/// Returns true if the datasource is currently open
/// </summary>
public bool IsOpen
{
get { return true; }
}
#endregion
private int _SRID = -1;
/// <summary>
/// The spatial reference ID (CRS)
/// </summary>
public int SRID
{
get { return _SRID; }
set { _SRID = value; }
}
#region IDisposable Members
/// <summary>
/// Disposes the object
/// </summary>
public void Dispose()
{
_Geometries = null;
}
#endregion
}
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -