![]() |
VOOZH | about |
dotnet add package NetTopologySuite.IO.Esri.Shapefile --version 1.2.0
NuGet\Install-Package NetTopologySuite.IO.Esri.Shapefile -Version 1.2.0
<PackageReference Include="NetTopologySuite.IO.Esri.Shapefile" Version="1.2.0" />
<PackageVersion Include="NetTopologySuite.IO.Esri.Shapefile" Version="1.2.0" />Directory.Packages.props
<PackageReference Include="NetTopologySuite.IO.Esri.Shapefile" />Project file
paket add NetTopologySuite.IO.Esri.Shapefile --version 1.2.0
#r "nuget: NetTopologySuite.IO.Esri.Shapefile, 1.2.0"
#:package NetTopologySuite.IO.Esri.Shapefile@1.2.0
#addin nuget:?package=NetTopologySuite.IO.Esri.Shapefile&version=1.2.0Install as a Cake Addin
#tool nuget:?package=NetTopologySuite.IO.Esri.Shapefile&version=1.2.0Install as a Cake Tool
This library provides forward-only readers and writers for Esri shapefiles.
Shapefile feature attributes are held in a (.dbf extension). Each attribute record
has a one-to-one relationship with the associated shape record. Classes whose name starts
with Dbf (eg. DbfReader) provide direct access to dBASE files.
using var dbf = new DbfReader(dbfPath);
foreach (var record in dbf)
{
foreach (var fieldName in record.GetNames())
{
Console.WriteLine($"{fieldName,10} {record[fieldName]}");
}
Console.WriteLine();
}
The main file (.shp extension) is a variable-record-length file in which each record describes
a shape with a list of its vertices. Classes whose name starts with Shp (eg. ShpPointReader)
provide direct access to main file.
foreach (var geometry in Shapefile.ReadAllGeometries(shpPath))
{
Console.WriteLine(geometry);
}
The index file (.shx extension) stores the offset and content length for each record in SHP file.
As there is no additional value, this file is ignored during reading shapefiles.
Writing SHX data is handled directly by ShpWriter classes.
All three files described above form a shapefile. Unified access to shapefile triplet
is provided through classes whose name starts with Shapefile (eg. ShapefilePointReader).
Under the hood they are decorators wrapping Dbf and Shp classes.
foreach (var feature in Shapefile.ReadAllFeatures(shpPath))
{
foreach (var attrName in feature.Attributes.GetNames())
{
Console.WriteLine($"{attrName,10}: {feature.Attributes[attrName]}");
}
Console.WriteLine($" SHAPE: {feature.Geometry}");
Console.WriteLine();
}
The most common variant of writing shapefiles is to use Shapefile.WriteAllFeatures method.
var features = new List<Feature>();
for (int i = 1; i < 5; i++)
{
var lineCoords = new List<CoordinateZ>
{
new CoordinateZ(i, i + 1, i),
new CoordinateZ(i, i, i),
new CoordinateZ(i + 1, i, i)
};
var line = new LineString(lineCoords.ToArray());
var mline = new MultiLineString(new LineString[] { line });
var attributes = new AttributesTable
{
{ "date", new DateTime(2000, 1, i + 1) },
{ "float", i * 0.1 },
{ "int", i },
{ "logical", i % 2 == 0 },
{ "text", i.ToString("0.00") }
};
var feature = new Feature(mline, attributes);
features.Add(feature);
}
Shapefile.WriteAllFeatures(features, shpPath);
The most efficient way to write large shapefiles is to use ShapefileWriter class.
This variant should also be used when you need to write a shapefile with a attributes containing null values.
var fields = new List<DbfField>();
var dateField = fields.AddDateField("date");
var floatField = fields.AddFloatField("float");
var intField = fields.AddNumericInt32Field("int");
var logicalField = fields.AddLogicalField("logical");
var textField = fields.AddCharacterField("text");
var options = new ShapefileWriterOptions(ShapeType.PolyLine, fields.ToArray());
using (var shpWriter = Shapefile.OpenWrite(shpPath, options))
{
for (var i = 1; i < 5; i++)
{
var lineCoords = new List<Coordinate>
{
new(i, i + 1),
new(i, i),
new(i + 1, i)
};
var line = new LineString(lineCoords.ToArray());
var mline = new MultiLineString(new LineString[] { line });
int? nullIntValue = null;
shpWriter.Geometry = mline;
dateField.DateValue = DateTime.Now;
floatField.NumericValue = i * 0.1;
intField.NumericValue = nullIntValue;
logicalField.LogicalValue = i % 2 == 0;
textField.StringValue = i.ToString("0.00");
shpWriter.Write();
}
}
The .NET Framework supports a large number of character encodings and code pages. On the other hand, .NET Core only supports limited list of encodings. To retrieve an encoding that is present in the .NET Framework on the Windows desktop but not in .NET Core, you need to do the following:
Encoding.RegisterProvider(CodePagesEncodingProvider.Instance);Stable releases are hosted on the default NuGet feed. You can install them using the following command on the package manager command line
PM> NuGet\Install-Package NetTopologySuite.IO.Esri.Shapefile
| Product | Versions Compatible and additional computed target framework versions. |
|---|---|
| .NET | net5.0 net5.0 was computed. net5.0-windows net5.0-windows was computed. net6.0 net6.0 was computed. net6.0-android net6.0-android was computed. net6.0-ios net6.0-ios was computed. net6.0-maccatalyst net6.0-maccatalyst was computed. net6.0-macos net6.0-macos was computed. net6.0-tvos net6.0-tvos was computed. net6.0-windows net6.0-windows was computed. net7.0 net7.0 was computed. net7.0-android net7.0-android was computed. net7.0-ios net7.0-ios was computed. net7.0-maccatalyst net7.0-maccatalyst was computed. net7.0-macos net7.0-macos was computed. net7.0-tvos net7.0-tvos was computed. net7.0-windows net7.0-windows was computed. net8.0 net8.0 was computed. net8.0-android net8.0-android was computed. net8.0-browser net8.0-browser was computed. net8.0-ios net8.0-ios was computed. net8.0-maccatalyst net8.0-maccatalyst was computed. net8.0-macos net8.0-macos was computed. net8.0-tvos net8.0-tvos was computed. net8.0-windows net8.0-windows was computed. net9.0 net9.0 was computed. net9.0-android net9.0-android was computed. net9.0-browser net9.0-browser was computed. net9.0-ios net9.0-ios was computed. net9.0-maccatalyst net9.0-maccatalyst was computed. net9.0-macos net9.0-macos was computed. net9.0-tvos net9.0-tvos was computed. net9.0-windows net9.0-windows was computed. net10.0 net10.0 was computed. net10.0-android net10.0-android was computed. net10.0-browser net10.0-browser was computed. net10.0-ios net10.0-ios was computed. net10.0-maccatalyst net10.0-maccatalyst was computed. net10.0-macos net10.0-macos was computed. net10.0-tvos net10.0-tvos was computed. net10.0-windows net10.0-windows was computed. |
| .NET Core | netcoreapp2.0 netcoreapp2.0 was computed. netcoreapp2.1 netcoreapp2.1 was computed. netcoreapp2.2 netcoreapp2.2 was computed. netcoreapp3.0 netcoreapp3.0 was computed. netcoreapp3.1 netcoreapp3.1 was computed. |
| .NET Standard | netstandard2.0 netstandard2.0 is compatible. netstandard2.1 netstandard2.1 was computed. |
| .NET Framework | net461 net461 was computed. net462 net462 was computed. net463 net463 was computed. net47 net47 was computed. net471 net471 was computed. net472 net472 was computed. net48 net48 was computed. net481 net481 was computed. |
| MonoAndroid | monoandroid monoandroid was computed. |
| MonoMac | monomac monomac was computed. |
| MonoTouch | monotouch monotouch was computed. |
| Tizen | tizen40 tizen40 was computed. tizen60 tizen60 was computed. |
| Xamarin.iOS | xamarinios xamarinios was computed. |
| Xamarin.Mac | xamarinmac xamarinmac was computed. |
| Xamarin.TVOS | xamarintvos xamarintvos was computed. |
| Xamarin.WatchOS | xamarinwatchos xamarinwatchos was computed. |
Showing the top 5 NuGet packages that depend on NetTopologySuite.IO.Esri.Shapefile:
| Package | Downloads |
|---|---|
|
DHI.Services.Shapefile
DHI Domain Services shapefile provider for GIS. |
|
|
BitToLife.Miscellaneous.Spatial
Package Description |
|
|
LasUtility
A toolset for laz and shp files |
|
|
Hanson.Common.GeoTimeZone
Package Description |
|
|
KAbpCommonBase
Package Description |
Showing the top 3 popular GitHub repositories that depend on NetTopologySuite.IO.Esri.Shapefile:
| Repository | Stars |
|---|---|
|
mattjohnsonpint/GeoTimeZone
Provides an IANA time zone identifier from latitude and longitude coordinates.
|
|
|
f-shake/ArchiveMaster
一套协助用户对文件进行处理、管理、备份、同步的工具集;A toolkit designed to assist users in processing, managing, backing up, and synchronizing files.
|
|
|
surveysolutions/surveysolutions
Survey Solutions is a survey management and data collection system developed by the World Bank.
|