nettopologysuite / nettopologysuite.io.esri Goto Github PK
View Code? Open in Web Editor NEWLicense: BSD 3-Clause "New" or "Revised" License
License: BSD 3-Clause "New" or "Revised" License
Hi,
I was passed a simple shp file made by QGIS. It errors out on reading in. A quick debug finds a string of * in the numeric ID field. We added the following quick fix:
I wonder if you have a better approach that could be folded in to the repo? File attached.
WallaceMonument.zip
PS is there a nuget for this repo? couldn't locate it on last look.
thanks in advance, and cheers for the work, Matt
The ShapeWriter can produce shp files that are over 2gb (int.MaxValue
bytes). Unfortunately, ArcGis Pro (and possibly other tools), cannot actually correctly read such large files.
I had some shapefiles that I created that had produced slightly more than 2gb of shape data, and I originally thought it was some sort of arcgis bug that my 300,000 row shapefile was stopping at 269,861 rows. Apparently that was the cut off point where my file was over the 2gb limit. While my dbf was small enough to show the rows, the remaining feature polygons could not be read.
If the SHP length field exceeds int.MaxValue
an invalid operation exception should be thrown as the file is likely unusable.
Hi all,
I am applying your library in order to adjust attribute values in an existing polygon shapefile.
What is the best way to approach this? Here is my code so far. However it does not work and returns an error saying the file is already in use by another process. Also if I change the results filename I get an error: "value cannot be null, parameter name: Geometry"
Dim ShapeReader As New NetTopologySuite.IO.Esri.Shapefiles.Readers.ShapefilePolygonReader("full.shp")
Dim myFeature As NetTopologySuite.Features.Feature
Dim Deleted As Boolean
While ShapeReader.Read(Deleted, myFeature)
myFeature.Attributes.Item("GFEIDENT") = "new value"
End While
Dim options As New ShapefileWriterOptions(ShapeReader)
Using myWriter As New NetTopologySuite.IO.Esri.Shapefiles.Writers.ShapefilePolygonWriter("full.shp", options)
myWriter.Write()
End Using
Thanks in advance for your help!
Siebe Bosch
We're in process of migrating to IO.ESRI
from IO.ShapeFile
& we have noticed a change in behavior when saving PointZM geometries where Z=NaN
The old version was exporting these NaN
values, while the new version converts them to 0
.
Checking the code it mentions spec conformance. On the other hand, according to the spec:
Positive infinity, negative infinity, and Not-a-Number (NaN) values are not allowed in shapefiles.
& the library saves X & Y coordinates without any conversion. (Sure, the code mentions "// Avoid performance costs (if you trying to pas NaN as X,Y then you're wrong).")
Unfortunately our software is relying on the behavior of the old version: we need to round-trip coordinates with NaN
values.
Before sending a PR I'd be interested in your opinion & if this is something you consider supporting at all.
I was thinking of introducing some kind of "Legacy" or "Loose" behavior switch for exports, similar to the GeometryBuilderMode
when importing, but open to other ideas.
Another option would be making the Shapefile
static class a bit more extensible & reusable, so I can reproduce the WriteAllFeatures
without copying half of the class, by injecting a custom ShapefilePointWriter
. This way I could write out-of-spec files without making the library to support out-of-spec files. This would be a bigger refactoring, however. (Esp. that the other writers need to be split, too, e.g. ShpMultiPartBuilder
)
How do I read a prj and us it with the shape file(s)?
Hello Esri Team.
First of all thanks for the hard work for the whole NetTopologySuite working with Polygons is so much easier than it should be thanks to your package.
Now to my current issue:
We recently got a bunch of shapefiles with duplicate attribute-names.
I was able to implement my own workaround so i can at least open these files (otherwise NuGet Version 1.0.0 would throw an exception when trying to open it due to trying to Add duplicate Values to a Dictionary)
However now we got shapefiles that actually contain needed Information in some of those dupllicate Attribute-Names.
I absolutely understand that this is not supported because I myself think it's in the exporters responsibility that all attributes have distinct names. However the Shapefiles are sent from government instituts and it's next to impossible to change anything in the near future there no matter how hard we would complain.
I've already pulled the Repository and created my own Version of it which can open shapefiles with duplicate attribute-names. So I'd be willing to implement the change for my version as well, however I'm not that familiar with the repository and if I could get an explanation on what I need to change I could do it myself. (And also pull the code back into the main-repository)
I was thinking of a similar solution to QGIS which imports all duplicate attributes with a trailing "_1" counting upwards for each found duplicate attribute-name.
I would appreciate any help. Even if it's just hints about what I have to look out for when implementing this change.
Thanks :)
Antragsschläge 2024 (4).zip
I've also attached a zip-file with this exact issue
For ShapeType.NullShape
the ShapefileReader
class returns empty geometries. Using <SpecificGeometryClass>.Empty
field in this case does not respect ShapefileReaderOptions.Factory
setting.
Hi all,
I've been testing out reading in shapefiles with your library, to look at replacing another library that we currently use that doesn't handle text encoding very well (https://github.com/abfo/shapefile).
However, I've noticed that reading in multi-polygon shapefiles with your library, in a few examples, seems(?) to read in unexpected polygons.
I don't think I've described the issue very well, but below I've got 2 screen shots, rendering the shapefile in question, using the Microsoft Bing Map control, which hopefully showcases what I'm describing.
The first image is from the other shapefile library
The 2nd image is from your library, which has some additional polygons being read in and rendered.
I'm not overly familiar with shapefiles, so this may not be an issue with your library, but maybe with the shapefile itself(?), or it could be something that we need to handle ourselves, after we've read in the shapefiles?
I thought I would raise this, in case anyone had some ideas as to what would cause this, and whether it's something that could be fixed up somehow.
The Geometries are read in using: foreach (var feature in Shapefile.ReadAllFeatures(filePath)){ ... }
The Shapefile I used for this example:
Example Shape file.zip
Apologies if this isn't an issue,
Thanks in advance,
Emma
Hi!
first of all, thank you for your library.
I'm inexperienced with ESRI shapefile.
I'm using your library to try to open an ESRI shapefile, modify its attributes and save a new one.
I have found this issue with a MultiPolygon ESRI shapefile and at the moment I'm trying to understand what it is wrong..
I'm calling Shapefile.ReadAllFeatures on the shapefile attached below..
MultiPolygon ReadGeometry(Stream shapeBinary) throws an exception.
If you have any suggestions you are more then welcome.
Thank you,
bye
Francesca
tmpA6A6.zip
I have added tests from NetTopologySuite.IO.ShapeFile repository. Can someone take a look on it, please?
Hi all,
I've been using your library recently, it's really nice and easy to use!
I have noticed however, that there are a few shapefiles that are unable to be read in, and thought best to raise this as a potential issue.
An exception is thrown when this line is executed in my project:
foreach (var feature in Shapefile.ReadAllFeatures(filePath)) { ... }
The exception thrown:
NetTopologySuite.IO.Esri.ShapefileException : {"Invalid polygon geometry.":null}
StackTrace: at NetTopologySuite.IO.Esri.Shp.Readers.ShpPolygonReader.BuildMultiPolygon(ShpMultiPartBuilder parts)
at NetTopologySuite.IO.Esri.Shp.Readers.ShpPolygonReader.ReadGeometry(Stream stream, MultiPolygon& geometry)
at NetTopologySuite.IO.Esri.Shp.Readers.ShpReader1.ReadCore(Int32& skippedCount) at NetTopologySuite.IO.Esri.Shapefiles.Readers.ShapefileReader
1.Read(Boolean& deleted)
at NetTopologySuite.IO.Esri.Shapefiles.Readers.ShapefileReader1.Read(Boolean& deleted, Feature& feature) at NetTopologySuite.IO.Esri.Shapefiles.Readers.ShapefileReader.FeatureEnumerator.MoveNext() at System.Linq.Buffer
1..ctor(IEnumerable1 source) at System.Linq.Enumerable.ToArray[TSource](IEnumerable
1 source)
at NetTopologySuite.IO.Esri.Shapefile.ReadAllFeatures(String shpPath, ShapefileReaderOptions options)
I've attached a couple of the shapefile examples, that cause the same exception to be thrown just below,
Example Shapefiles.zip
FYI: Shapefiles are from here: https://hub.arcgis.com/documents/NSTAUTHORITY::-nsta-offshore-zipped-shapefiles-wgs84/about
Thanks in advance for your help,
Emma
Based on @anthea-wu question on Stack overflow:
I want to create a shapefile from wkt by using NetTopologySuite.IO.Esri, but get this error like this:
Unhandled exception. System.InvalidCastException: Unable to cast object of type 'NetTopologySuite.Geometries.Polygon' to type 'NetTopologySuite.Geo
metries.MultiPolygon'.
at NetTopologySuite.IO.Esri.Shapefiles.Writers.ShapefileWriter`1.Write(IFeature feature)
at NetTopologySuite.IO.Esri.Shapefiles.Writers.ShapefileWriter.Write(IEnumerable`1 features)
at NetTopologySuite.IO.Esri.Shapefile.WriteAllFeatures(IEnumerable`1 features, String shpPath, Encoding encoding, String projection)
Here is my code:
using System.Text;
using NetTopologySuite.Features;
using NetTopologySuite.IO;
using NetTopologySuite.IO.Esri;
var features = new List<Feature>();
var wktReader = new WKTReader();
// I tried wkt with EPSG:4326 and EPSG:3826, but no one work.
var geometry = wktReader.Read($"POLYGON((229884.458362927 2698919.1790506,229878.266318657 2698913.05244554,229872.637726088 2698914.0245398,229870.132848978 2698912.76299454,229868.585854503 2698911.60799823,229862.417875893 2698913.56799102,229859.466886255 2698913.2179918,229854.168905161 2698909.4280045,229832.918531065 2698900.75121021,229826.266555421 2698903.87487001,229815.242942057 2698911.30138916,229814.158096991 2698912.0322406,229802.736136414 2698917.5934203,229787.107090625 2698922.43900198,229770.41744862 2698926.49898628,229758.597689508 2698931.31876844,229754.622568952 2698933.63004125,229756.82934315 2698935.98290305,229758.154520219 2698937.39368305,229761.467578897 2698937.10864875,229772.427440716 2698935.38875568,229785.926793536 2698934.84915899,229801.796838123 2698933.66916467,229811.707603543 2698932.67086896,229818.576179578 2698931.9792722,229834.383317175 2698930.1646309,229840.615502711 2698929.44918301,229859.255237777 2698926.54949485,229875.612191821 2698922.46243583,229879.34506797 2698921.52971422,229884.458362927 2698919.1790506))");
// Also tried this but not work too.
// var geometry = wktReader.Read(wkt) as Polygon;
var attributes = new AttributesTable
{
{ "Date", new DateTime(2022, 1, 1) },
{ "Content", $"I am No. 1" }
};
var feature = new Feature(geometry, attributes);
features.Add(feature);
Shapefile.WriteAllFeatures(features, @"C:\Users\user\Downloads\test.shp");
Hi,
Thanks for the great project. The latest fixes to help skip invalid geometry and cope with poor dates are really useful. Can I ask that you publish a fresh nuget so we can refrence it (much nicer than copying your code locally).
Thanks again, Matt
Hi all,
I think this may be a non-issue, but I am getting an exception when trying to read in this shapefile,
The shapefile:
Example Shapefile.zip
The exception:
{"The DateTime represented by the string is not supported in calendar System.Globalization.GregorianCalendar."}
at System.DateTimeParse.ParseExact(String s, String format, DateTimeFormatInfo dtfi, DateTimeStyles style)
\r\n at NetTopologySuite.IO.Esri.Dbf.Fields.DbfDateField.ReadValue(Stream stream)
\r\n at NetTopologySuite.IO.Esri.Dbf.DbfReader.Read(Boolean& deleted)
\r\n at NetTopologySuite.IO.Esri.Shapefiles.Readers.ShapefileReader1.Read(Boolean& deleted) \r\n at NetTopologySuite.IO.Esri.Shapefiles.Readers.ShapefileReader
1.Read(Boolean& deleted, Feature& feature)
\r\n at NetTopologySuite.IO.Esri.Shapefiles.Readers.ShapefileReader.FeatureEnumerator.MoveNext()
\r\n at System.Linq.Buffer1..ctor(IEnumerable
1 source)
\r\n at System.Linq.Enumerable.ToArray[TSource](IEnumerable`1 source)
\r\n at NetTopologySuite.IO.Esri.Shapefile.ReadAllFeatures(String shpPath, ShapefileReaderOptions options)
I think this may be an issue with the shapefile itself, as the field MAPDATE has dates stored as 00000000 in the attached .dbf file, (which could be causing this issue perhaps?).
But I wanted to confirm whether this is an error case that your library should be handling, or whether it's an issue with the shapefile, that should be fixed up, before reading in.
The shapefile is read in by: foreach (var feature in Shapefile.ReadAllFeatures(filePath))
Thanks in advance,
Emma
Hi.
thank you guys for this library.
I am inexperienced with shape files but I want to export shapefile with it's WKT, when I do export shape file by using the library
with the following code.
List<Feature> features = new List<Feature>();
var geoReader = new GeoJsonReader();
var feature = geoReader.Read<Feature>(jsonData);
List<Feature> features = new List<Feature>(){feature};
Shapefile.WriteAllFeatures(features, "C:\\Logs\\test.shp");
it works fine and I got the following files
test.dbf
test.shp
test.shx
test.cpg
but when I try to add these shape file to tool like ArcGIS Pro, I got the following warning
What I need to do is to export WKT files with the shapefile.
I except files like test.prj
which holds the projects coordinate system
Thanks
No matter what is supplied for shpPath the files are always written to the same directory the executable is in.
I'm trying to upgrade a project that use to use the older NetTopologySuite.IO.ShapeFile. When I use to export a shape, I would get the esri generated Feature ID as the property "FeatureId". I've tried using GetOptionalId("FID") and GetOptionalId("FeatureID") and neither work.
Hi!
Sometimes, character fields in dbf file (inside ShapeFile) has 255 chars length. This one generates error during reading.
a posible solution:
--- a/src/NetTopologySuite.IO.Esri.Shapefile/Dbf/Fields/DbfCharacterField.cs
+++ b/src/NetTopologySuite.IO.Esri.Shapefile/Dbf/Fields/DbfCharacterField.cs
@@ -22,7 +22,7 @@ public DbfCharacterField(string name, int length = MaxFieldLength) : this(name,
}
internal DbfCharacterField(string name, int length, Encoding encoding)
: base(name, DbfType.Character, Math.Min(length, MaxFieldLength), 0)
: base(name, DbfType.Character, Math.Min(length, MaxFieldLength+1), 0)
{
_encoding = encoding;
}
TIA,
regards
Hello, I have two datasets now: one is LineString data and the other is Polygon data. I added these two datasets to a FeatureCollection and tried to generate a shapefile using Shapefile.WriteAllFeatures. However, I encountered an error message saying "Invalid geometry type provided (Polygon). Expected: Multilinestring." I would like to know if this is because a FeatureCollection cannot contain different types of geometries. How can I solve this problem? Thank you.
code:
var ntsService = new NtsGeometryServices(new PrecisionModel(), 4326);
var wktReader = new WKTReader(ntsService);
var featureCollection = new List<IFeature>();
foreach (var item in wktArrays)
{
var geometry = wktReader.Read(item.Wkt);
foreach (var name in item.Attributes.GetNames())
{
if (item.Attributes.GetType(name) == typeof(object))
{
item.Attributes.DeleteAttribute(name);
}
}
var feature = new Feature { Geometry = geometry, Attributes = item.Attributes };
featureCollection.Add(feature);
}
var now = DateTime.Now;
var folderDate = now.ToString("yyyy") + now.ToString("MM") + now.ToString("dd") + now.ToString("HH")
+ now.ToString("mm") + now.ToString("ss");
var shapeFilePath = Path.Combine(App.WebHostEnvironment.ContentRootPath, Options.ShapeFilePath, fileName, folderDate);
if (!Directory.Exists(shapeFilePath))
{
Directory.CreateDirectory(shapeFilePath);
}
var savePath = Path.Combine(shapeFilePath, fileName);
Shapefile.WriteAllFeatures(featureCollection, savePath);
Before I get started I would like clarify proposed repository structure.
Array
and then copied to NTS' CoordinateSequence
. It would be more efficient to load them directly from binary into CoordinateSequence
. Furthermore the Core project doesn't contain any geometry logic (line length, polygon area, ...). Because of that some issues will be very hard to fix directly in Core project. For example fixing NetTopologySuite/NetTopologySuite.IO.ShapeFile#70 will be much easier having NTS' LinearRing.IsCCW
property. For that reasons I would like to combine those two projects into one project which is tightly integrated with NTS library.What do you think, @FObermaier?
I'm trying to write a shapefile that may contain a mix of LineStrings and MultiLineStrings. I'm getting the writer like so:
var options = new ShapefileWriterOptions(ShapeType.PolyLine, fields)
{
Projection = GeographicCoordinateSystem.WGS84.WKT
};
using var writer = Shapefile.OpenWrite(shpPath, options);
... code to create DbfFields and set geometry
writer.Write();
When I try to write the file I get a "Cannot cast LineString to MultiLineString" error stemming from the ShapefileWriter class. The override of the Geometry property is trying to cast the Geometry to T.
The Shapefile.OpenWrite() method will return a PolyLineWriter. The PolyLineWriter is a ShapeFileWriter. Hence the error when trying to write a LineString.
If I use Features (as opposed to directly constructing DbfFields) and use writer.Write(Features)
it works because Write(Feature)
will turn the LineString into a MultiLineString for me in ShpPolyLineWriter.GetShapeGeometry().
But features don't handle null values, due to the issue described in "Null value" (can't determine type). It would be nice if either features handled null values, or writer.Write()
could handle LineStrings.
Hello,
I see that this package is the continuation of this package
However I see that at some stage the GDBWriter and GDBReader classes were dropped.
Just wondering if there is a technical reason why they were dropped, and if you would accept PR's to create new versions of these classes?
I want to create a shapefile from wkt, but get this error like this:
Unhandled exception. System.InvalidCastException: Unable to cast object of type 'NetTopologySuite.Geometries.Polygon' to type 'NetTopologySuite.Geo
metries.MultiPolygon'.
at NetTopologySuite.IO.Esri.Shapefiles.Writers.ShapefileWriter`1.Write(IFeature feature)
at NetTopologySuite.IO.Esri.Shapefiles.Writers.ShapefileWriter.Write(IEnumerable`1 features)
at NetTopologySuite.IO.Esri.Shapefile.WriteAllFeatures(IEnumerable`1 features, String shpPath, Encoding encoding, String projection)
I try to open the debug mode and tracking code, I found that in Shapefile.cs
line 185, there are trying to get the parameter's geometry type like this:
var shapeType = features.FindNonEmptyGeometry().GetShapeType();
And in my situation, it always return PolygonZM
😭
I try to search like NetTopologySuite create shp
but no answer now. Could anyone give me a point to solve this question? Thanks!!
( I'm not a native English speaker, please bear with me for any grammatical errors, thanks! )
Here is my code:
using System.Text;
using NetTopologySuite.Features;
using NetTopologySuite.IO;
using NetTopologySuite.IO.Esri;
var features = new List<Feature>();
var wktReader = new WKTReader();
var geometry = wktReader.Read($"POLYGON((229884.458362927 2698919.1790506,229878.266318657 2698913.05244554,229872.637726088 2698914.0245398,229870.132848978 2698912.76299454,229868.585854503 2698911.60799823,229862.417875893 2698913.56799102,229859.466886255 2698913.2179918,229854.168905161 2698909.4280045,229832.918531065 2698900.75121021,229826.266555421 2698903.87487001,229815.242942057 2698911.30138916,229814.158096991 2698912.0322406,229802.736136414 2698917.5934203,229787.107090625 2698922.43900198,229770.41744862 2698926.49898628,229758.597689508 2698931.31876844,229754.622568952 2698933.63004125,229756.82934315 2698935.98290305,229758.154520219 2698937.39368305,229761.467578897 2698937.10864875,229772.427440716 2698935.38875568,229785.926793536 2698934.84915899,229801.796838123 2698933.66916467,229811.707603543 2698932.67086896,229818.576179578 2698931.9792722,229834.383317175 2698930.1646309,229840.615502711 2698929.44918301,229859.255237777 2698926.54949485,229875.612191821 2698922.46243583,229879.34506797 2698921.52971422,229884.458362927 2698919.1790506))");
var attributes = new AttributesTable
{
{ "Date", new DateTime(2022, 1, 1) },
{ "Content", $"I am No. 1" }
};
var feature = new Feature(geometry, attributes);
features.Add(feature);
Shapefile.WriteAllFeatures(features, @"C:\Users\user\Downloads\test.shp");
when I want to create a shapefile and insert a null value in it, I get a system object not supported error.
Fine, but the value is an int?
I have the NetTopologySuite.IO.Esri.Shapefile.WriteAllFeatures(features, path); working in my code. I have a requirement to set the Shapefile Header with file code, file length, version, shape type, Xmin, Ymin, Xmax, Ymax. Is there documentation on setting the file header with NetTopologySuite.IO.Esri?
I'm trying to save shape file after reading it, but I receive exception
System.NotSupportedException: Unsupported dBASE field type: RuntimeType (System.Object)
From my investigation it looks like that problem is when field has null value
Sample code and shp file used to generate error attached
sample.zip
When reading a PointZ shapefile (example file), the Z coordinates of the Point geometries remain NaN.
Is there any way to reproject the geometries to a new coordinate system once they've been read in from the shape file? This reader doesn't seem to take into account the .prj file. Do I need to use an additional nuget package?
Add helper methods to open ShapefileReader
and ShapefileWriterStream
using streams. This will be an alternative to existing methods based on file path:
Shapefile.OpenRead(string shpPath, ShapefileReaderOptions options = null)
ShapefileShapefileWriter OpenWrite(string shpPath, ShapefileWriterOptions options)
Is there any status or ETA on this project first release?
Hello, in testing the stream enhancements, I found a problem. Unlike the other streams, which are left open, the projection stream is closed. We should leave all streams open after the write operation. The problem is the use of a using statement, and a StreamWriter constructor. The StreamWriter constructor has a parameter called "leaveOpen", when set to true, will leave the stream open when the StreamWriter is disposed. We should set this to true.
internal ShapefileWriter(Stream shpStream, Stream shxStream, Stream dbfStream, Stream prjStream, ShapefileWriterOptions options)
: base(new DbfWriter(dbfStream, options?.Fields, options?.Encoding))
{
try
{
options = options ?? throw new ArgumentNullException(nameof(options));
ShapeType = options.ShapeType;
ShpWriter = CreateShpWriter(shpStream, shxStream);
if (!string.IsNullOrWhiteSpace(options.Projection) && prjStream != null)
{
using (var writer = new StreamWriter(prjStream))
{
writer.Write(options.Projection);
}
}
}
catch
{
DisposeManagedResources();
throw;
}
}
Originally posted by @FrankHileman in #36 (comment)
Dbf has a limit of 10 characters in fieldnames, but when I view some shapefiles in arcgis pro, I can see the real field name like RECORDED_1
and then an Alias for the field like Recorded department
I would like to be able to view the aliases.
Is there interest to provide support for Shapfile's spatial index (SBN) with this package?
A declarative, efficient, and flexible JavaScript library for building user interfaces.
🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
An Open Source Machine Learning Framework for Everyone
The Web framework for perfectionists with deadlines.
A PHP framework for web artisans
Bring data to life with SVG, Canvas and HTML. 📊📈🎉
JavaScript (JS) is a lightweight interpreted programming language with first-class functions.
Some thing interesting about web. New door for the world.
A server is a program made to process requests and deliver data to clients.
Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.
Some thing interesting about visualization, use data art
Some thing interesting about game, make everyone happy.
We are working to build community through open source technology. NB: members must have two-factor auth.
Open source projects and samples from Microsoft.
Google ❤️ Open Source for everyone.
Alibaba Open Source for everyone
Data-Driven Documents codes.
China tencent open source team.