GithubHelp home page GithubHelp logo

Comments (9)

jzmiller1 avatar jzmiller1 commented on July 28, 2024

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.

apiszcz avatar apiszcz commented on July 28, 2024

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.

jzmiller1 avatar jzmiller1 commented on July 28, 2024

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.

apiszcz avatar apiszcz commented on July 28, 2024

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 driverESRI 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.

jzmiller1 avatar jzmiller1 commented on July 28, 2024

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.

apiszcz avatar apiszcz commented on July 28, 2024

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.

jzmiller1 avatar jzmiller1 commented on July 28, 2024

If I have time I will try to look over the docs and put together a pull request with another example.

from pyshp.

riggsd avatar riggsd commented on July 28, 2024

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:

#17

from pyshp.

karimbahgat avatar karimbahgat commented on July 28, 2024

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)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo 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.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.