Class ShapeFile
Shapefile dataprovider
Inheritance
Namespace: Mapsui.Desktop.Shapefile
Assembly: Mapsui.Desktop.dll
Syntax
public class ShapeFile : object, IProvider, IDisposable
Remarks
The ShapeFile provider is used for accessing ESRI ShapeFiles. The ShapeFile should at least contain the [filename].shp, [filename].idx, and if feature-data is to be used, also [filename].dbf file.
The first time the ShapeFile is accessed, Mapsui will automatically create a spatial index of the shp-file, and save it as [filename].shp.sidx. If you change or update the contents of the .shp file, delete the .sidx file to force Mapsui to rebuilt it. In web applications, the index will automatically be cached to memory for faster access, so to reload the index, you will need to restart the web application as well.
M and Z values in a shapefile is ignored by Mapsui.
Examples
Adding a datasource to a layer:
Mapsui.Layers.VectorLayer myLayer = new Mapsui.Layers.VectorLayer("My layer");
myLayer.DataSource = new Mapsui.Data.Providers.ShapeFile(@"C:\data\MyShapeData.shp");
Constructors
| Improve this Doc View SourceShapeFile(String, Boolean)
Initializes a ShapeFile DataProvider.
Declaration
public ShapeFile(string filename, bool fileBasedIndex = false)
Parameters
Type | Name | Description |
---|---|---|
System.String | filename | Path to shape file |
System.Boolean | fileBasedIndex | Use file-based spatial index |
Remarks
If FileBasedIndex is true, the spatial index will be read from a local copy. If it doesn't exist, it will be generated and saved to [filename] + '.sidx'.
Using a file-based index is especially recommended for ASP.NET applications which will speed up start-up time when the cache has been emptied.
Properties
| Improve this Doc View SourceCRS
Gets or sets the spatial reference ID (CRS)
Declaration
public string CRS { get; set; }
Property Value
Type | Description |
---|---|
System.String |
Encoding
Gets or sets the encoding used for parsing strings from the DBase DBF file.
Declaration
public Encoding Encoding { get; set; }
Property Value
Type | Description |
---|---|
Encoding |
Remarks
The DBase default encoding is
Filename
Gets or sets the filename of the shapefile
Declaration
public string Filename { get; set; }
Property Value
Type | Description |
---|---|
System.String |
Remarks
If the filename changes, indexes will be rebuilt
FilterDelegate
Filter Delegate Method for limiting the datasource
Declaration
public ShapeFile.FilterMethod FilterDelegate { get; set; }
Property Value
Type | Description |
---|---|
ShapeFile.FilterMethod |
Remarks
myShapeDataSource.FilterDelegate = new Mapsui.Data.Providers.ShapeFile.FilterMethod(delegate(Mapsui.Data.FeatureDataRow row) { return (!row["NAME"].ToString().StartsWith("S")); });
myShapeDataSource.FilterDelegate = CountryFilter;
[...]
public static bool CountryFilter(Mapsui.Data.FeatureDataRow row)
{
if(row.Geometry.GetType()==typeof(Mapsui.Geometries.Polygon))
return ((row.Geometry as Mapsui.Geometries.Polygon).Area>5);
if (row.Geometry.GetType() == typeof(Mapsui.Geometries.MultiPolygon))
return ((row.Geometry as Mapsui.Geometries.MultiPolygon).Area > 5);
else return true;
}
See Also
| Improve this Doc View SourceShapeType
Gets the ShapeType in this shapefile.
Declaration
public ShapeType ShapeType { get; }
Property Value
Type | Description |
---|---|
ShapeType |
Remarks
The property isn't set until the first time the datasource has been opened, and will throw an exception if this property has been called since initialization.
All the non-Null shapes in a shapefile are required to be of the same shape type.
Methods
| Improve this Doc View SourceDispose()
Disposes the object
Declaration
public void Dispose()
Finalize()
Finalizes the object
Declaration
protected void Finalize()
GetExtents()
Returns the extents of the datasource
Declaration
public BoundingBox GetExtents()
Returns
Type | Description |
---|---|
BoundingBox |
GetFeature(UInt32, IFeatures)
Gets a datarow from the datasource at the specified index belonging to the specified datatable
Declaration
public IFeature GetFeature(uint rowId, IFeatures feature = null)
Parameters
Type | Name | Description |
---|---|---|
System.UInt32 | rowId | |
IFeatures | feature | Datatable to feature should belong to. |
Returns
Type | Description |
---|---|
IFeature |
GetFeatureCount()
Returns the total number of features in the datasource (without any filter applied)
Declaration
public int GetFeatureCount()
Returns
Type | Description |
---|---|
System.Int32 |
GetFeaturesInView(BoundingBox, Double)
Declaration
public IEnumerable<IFeature> GetFeaturesInView(BoundingBox box, double resolution)
Parameters
Type | Name | Description |
---|---|---|
BoundingBox | box | |
System.Double | resolution |
Returns
Type | Description |
---|---|
IEnumerable<IFeature> |
GetGeometriesInView(BoundingBox)
Returns geometries whose bounding box intersects 'bbox'
Declaration
public Collection<IGeometry> GetGeometriesInView(BoundingBox bbox)
Parameters
Type | Name | Description |
---|---|---|
BoundingBox | bbox |
Returns
Type | Description |
---|---|
Collection<IGeometry> |
Remarks
Please note that this method doesn't guarantee that the geometries returned actually intersect 'bbox', but only that their boundingbox intersects 'bbox'.
This method is much faster than the QueryFeatures method, because intersection tests are performed on objects simplifed by their boundingbox, and using the Spatial Index.
GetGeometry(UInt32)
Returns the geometry corresponding to the Object ID
Declaration
public IGeometry GetGeometry(uint oid)
Parameters
Type | Name | Description |
---|---|---|
System.UInt32 | oid | Object ID |
Returns
Type | Description |
---|---|
IGeometry | geometry |
GetObjectIDsInView(BoundingBox)
Returns geometry Object IDs whose bounding box intersects 'bbox'
Declaration
public Collection<uint> GetObjectIDsInView(BoundingBox bbox)
Parameters
Type | Name | Description |
---|---|---|
BoundingBox | bbox |
Returns
Type | Description |
---|---|
Collection<System.UInt32> |
RebuildSpatialIndex()
Forces a rebuild of the spatial index. If the instance of the ShapeFile provider uses a file-based index the file is rewritten to disk.
Declaration
public void RebuildSpatialIndex()