Comments (9)
Could you post more of your code and a zip of your shapefile somewhere if possible?
I'm not having any trouble reading or writing doubles
Here is my shapefile: https://dl.dropboxusercontent.com/u/77727653/points.zip
Here is some example code:
import shapefile
f = shapefile.Editor('shapefiles/points.shp')
for r in f.records:
if r[1] == 0.0:
r[1] = 9.9
for r in f.records:
print(r)
f.save('shapefiles/junk')
from pyshp.
import sys
sys.path.append('lib')
import shapefile
w=shapefile.Writer(shapefile.POINT)
w.point(1,1)
w.field('DOUBLETEST','N','8')
w.record('DOUBLETEST',1234567890123.0)
w.save('doubletest')
Traceback (most recent call last):
File "C:\p\test\pyshptest.py", line 8, in
w.save('doubletest')
File "lib\shapefile.py", line 1032, in save
self.saveDbf(target)
File "lib\shapefile.py", line 1004, in saveDbf
self.__dbfRecords()
File "lib\shapefile.py", line 891, in __dbfRecords
assert len(value) == size
AssertionError
from pyshp.
Looks like you are giving a double larger than the size you allowed in the field creation. You specified 8 but the double is larger. Try this instead:
import sys
sys.path.append('lib')
import shapefile
w=shapefile.Writer(shapefile.POINT)
w.point(1,1)
w.field('DOUBLETEST','N','14')
w.record('DOUBLETEST',1234567890123.0)
w.save('doubletest')
from pyshp.
That corrects the initial stack trace however the value is 0.
The 2nd line from the bottom DOUBLETEST (Real) = 0
import sys
sys.path.append('lib')
import shapefile
w=shapefile.Writer(shapefile.POINT)
w.point(1,1)
w.field('DOUBLETEST','N','14')
w.record('DOUBLETEST',1234567890123.0)
w.save('doubletest')
C:\p\test>ogrinfo -al doubletest.shp
INFO: Open of doubletest.shp' using driver
ESRI Shapefile' successful.
Layer name: doubletest
Geometry: Point
Feature Count: 1
Extent: (1.000000, 1.000000) - (1.000000, 1.000000)
Layer SRS WKT:
(unknown)
DOUBLETEST: Real (16.0)
OGRFeature(doubletest):0
DOUBLETEST (Real) = 0
POINT (1 1)
On Wed, Sep 3, 2014 at 2:37 PM, Zac Miller [email protected] wrote:
Looks like you are giving a double larger than the size you allowed in the
field creation. You specified 8 but the double is larger. Try this instead:import sys
sys.path.append('lib')
import shapefile
w=shapefile.Writer(shapefile.POINT)
w.point(1,1)
w.field('DOUBLETEST','N','14')
w.record('DOUBLETEST',1234567890123.0)
w.save('doubletest')—
Reply to this email directly or view it on GitHub
#7 (comment)
.
from pyshp.
Try this:
w = shapefile.Writer(shapefile.POINT)
w.point(1,1)
w.field('DOUBLETEST', 'N', '17', 2)
w.record(DOUBLETEST=1234567890123.0)
w.save('doubletest')
Or this:
w = shapefile.Writer(shapefile.POINT)
w.point(1,1)
w.field('DOUBLETEST', 'N', '17', 2)
w.record(1234567890123.0)
w.save('doubletest')
If you do it the first way your data gets collected into recordDict, if you do it the second way it gets collected into recordList of the Editor record method:
def record(self, *recordList, **recordDict):
"""Creates a dbf attribute record. You can submit either a sequence of
field values or keyword arguments of field names and values. Before
adding records you must add fields for the record values using the
fields() method. If the record values exceed the number of fields the
extra ones won't be added. In the case of using keyword arguments to specify
field/value pairs only fields matching the already registered fields
will be added."""
record = []
fieldCount = len(self.fields)
# Compensate for deletion flag
if self.fields[0][0].startswith("Deletion"): fieldCount -= 1
if recordList:
[record.append(recordList[i]) for i in range(fieldCount)]
elif recordDict:
for field in self.fields:
if field[0] in recordDict:
val = recordDict[field[0]]
if val is None:
record.append("")
else:
record.append(val)
if record:
self.records.append(record)
from pyshp.
Both work, any way those examples can be added to the documentation (if
needed), or did I miss something. Thank you.
On Wed, Sep 3, 2014 at 3:09 PM, Zac Miller [email protected] wrote:
Try this:
w = shapefile.Writer(shapefile.POINT)
w.point(1,1)
w.field('DOUBLETEST', 'N', '17', 2)
w.record(DOUBLETEST=1234567890123.0)
w.save('doubletest')Or this:
w = shapefile.Writer(shapefile.POINT)
w.point(1,1)
w.field('DOUBLETEST', 'N', '17', 2)
w.record(1234567890123.0)
w.save('doubletest')If you do it the first way your data gets collected into recordDict, if
you do it the second way it gets collected into recordList of the Editor
record method:def record(self, _recordList, *_recordDict):
"""Creates a dbf attribute record. You can submit either a sequence of
field values or keyword arguments of field names and values. Before
adding records you must add fields for the record values using the
fields() method. If the record values exceed the number of fields the
extra ones won't be added. In the case of using keyword arguments to specify
field/value pairs only fields matching the already registered fields
will be added."""
record = []
fieldCount = len(self.fields)
# Compensate for deletion flag
if self.fields[0][0].startswith("Deletion"): fieldCount -= 1
if recordList:
[record.append(recordList[i]) for i in range(fieldCount)]
elif recordDict:
for field in self.fields:
if field[0] in recordDict:
val = recordDict[field[0]]
if val is None:
record.append("")
else:
record.append(val)
if record:
self.records.append(record)—
Reply to this email directly or view it on GitHub
#7 (comment)
.
from pyshp.
If I have time I will try to look over the docs and put together a pull request with another example.
from pyshp.
Whether the user is doing something wrong or not, I think that this isn't an appropriate use of assert()
in library code. Here's a PR to throw an exception instead:
from pyshp.
So the original issue here was caused by the field length being too short for the value.
Another issue was that w.record(DOUBLETEST=1234567890123.0)
was mistyped as w.record('DOUBLETEST',1234567890123.0)
.
As for the doc additions, I didn't fully understand which parts were desired, but record keyword assignment are already featured in the docs, and #73 suggests adding more examples on data types.
And the assert has been changed to a more informative exception.
Considering this resolved.
from pyshp.
Related Issues (20)
- ValueError when reading shapefile from ZIP archive HOT 3
- logging should use named logger rather than root logger HOT 1
- Document the reason for first field always being a DeletionFlag HOT 2
- "README: Testing" section seems out of date HOT 1
- add/modify test routine for running pytest / ship test file with installation HOT 2
- pyshp 2.3 closing bytesio objects with Writer HOT 5
- Add read geojson capability HOT 1
- Support for getting encoding from .cpg files HOT 2
- Enhance combination with shapely HOT 2
- Why was Writer.save() removed? HOT 1
- How to modify the shape value HOT 1
- struct.error: unpack requires a buffer of 32 bytes HOT 2
- How to get the field name for data? HOT 1
- Inconsistent bounding box filtering HOT 1
- check if polygon is closed fails HOT 1
- Build marked as failing. Tests are not running in CI. Github /actions/setup-python no longer supports Python 2.7. HOT 1
- PyShp's own Example from README.md fails under Doctest with Python 2.7. .__geo_interface__ = behaves differently in Python 2? HOT 1
- Doc test in README.md fails due to undefined variable HOT 1
- Run tests on Python 3.10 and higher versions (as well as 2.7, 3.5, ..., 3.9)
- Interest in I/O methods interfacing geopandas objects? HOT 3
Recommend Projects
-
React
A declarative, efficient, and flexible JavaScript library for building user interfaces.
-
Vue.js
🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
-
Typescript
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
-
TensorFlow
An Open Source Machine Learning Framework for Everyone
-
Django
The Web framework for perfectionists with deadlines.
-
Laravel
A PHP framework for web artisans
-
D3
Bring data to life with SVG, Canvas and HTML. 📊📈🎉
-
Recommend Topics
-
javascript
JavaScript (JS) is a lightweight interpreted programming language with first-class functions.
-
web
Some thing interesting about web. New door for the world.
-
server
A server is a program made to process requests and deliver data to clients.
-
Machine learning
Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.
-
Visualization
Some thing interesting about visualization, use data art
-
Game
Some thing interesting about game, make everyone happy.
Recommend Org
-
Facebook
We are working to build community through open source technology. NB: members must have two-factor auth.
-
Microsoft
Open source projects and samples from Microsoft.
-
Google
Google ❤️ Open Source for everyone.
-
Alibaba
Alibaba Open Source for everyone
-
D3
Data-Driven Documents codes.
-
Tencent
China tencent open source team.
from pyshp.