GeoScript

Previous topic

layer

Next topic

Cursor

This Page

Layer

The layer.layer module provides the classes for data access and manipulation.

class geoscript.layer.layer.Layer

A source of spatial data.

add(o)

Adds a Feature to the layer.

o is the feature to add. It may be specified directly as a Feature object or alternatively as a dict or a list.

>>> from geoscript import geom
>>> from geoscript import feature
>>> l = Layer() 
>>> l.count()
0
>>> f = feature.Feature({'geom': geom.Point(1,2)})
>>> l.add(f)
>>> l.count()
1
>>> l = Layer()
>>> l.add({'geom': geom.Point(1,2)})
>>> l.add([geom.Point(1,2)])
>>> l.count()
2
bounds(filter=None)

The Bounds of the layer.

filter is an optional Filter to constrains the returned bounds.

>>> l = Layer()
>>> from geoscript import geom 
>>> l.add([geom.Point(1.0, 2.0)])
>>> l.add([geom.Point(3.0, 4.0)])
>>> l.bounds()
(1.0, 2.0, 3.0, 4.0, EPSG:4326)
>>> l.bounds('INTERSECTS(geom,POINT(3 4))')
(3.0, 4.0, 3.0, 4.0, EPSG:4326)
count(filter=None)

The number of features in the layer as an int.

filter is an optional Filter to constrains the counted set of features.

>>> l = Layer()
>>> l.count()
0
>>> from geoscript import geom
>>> l.add([geom.Point(1,2)])
>>> l.add([geom.Point(3,4)])
>>> l.count() 
2
>>> l.count('INTERSECTS(geom,POINT(3 4))')
1
cursor(filter=None, sort=None)

Returns a Cursor over the features of the layer.

filter is a optional Filter to constrain the features iterated over.

sort is an optional tuple or list of tuples that defined the order in which features are iterated over. The first value of each tuple is the name of a field to sort on. The second value is one of the strings ‘ASC’ or ‘DESC’, representing ascending and decending sort order respectively.

>>> l = Layer()
>>> from geoscript import geom
>>> l.add([geom.Point(1,2)])
>>> l.add([geom.Point(3,4)])
>>> l.add([geom.Point(5,6)])
>>> l.add([geom.Point(7,8)])
>>> l.add([geom.Point(9,10)])
>>> c = l.cursor()
>>> f = c.next() 
>>> f.geom
POINT (1 2)
>>> f = c.next() 
>>> f.geom
POINT (3 4)
>>> features = c.read(2)
>>> len(features)
2
>>> features[0].geom
POINT (5 6)
>>> features[1].geom
POINT (7 8)
>>> features = c.read(2)
>>> len(features)
1
>>> features[0].geom
POINT (9 10)
>>> c.close()
delete(filter)

Deletes features from the layer which match the specified constraint.

filter is a Filter that specifies which features are to be deleted.

>>> l = Layer()
>>> from geoscript import geom
>>> l.add([geom.Point(1,2)])
>>> l.add([geom.Point(3,4)])
>>> l.count()
2
>>> l.delete('INTERSECTS(geom, POINT(3 4))')
>>> l.count()
1
features(filter=None, transform=None, sort=None)

Generator over the Feature s of the layer.

filter is a optional Filter to constrain the features iterated over.

transform is an optional function to be executed to transform the features being iterated over. This function takes a single argument which is a Feature and returns a (possibly different) feature.

sort is an optional tuple or list of tuples that defined the order in which features are iterated over. The first value of each tuple is the name of a field to sort on. The second value is one of the strings ‘ASC’ or ‘DESC’, representing ascending and decending sort order respectively.

>>> l = Layer()
>>> from geoscript import geom
>>> l.add([geom.Point(1,2)])
>>> l.add([geom.Point(3,4)])
>>> [ str(f.geom) for f in l.features() ]
['POINT (1 2)', 'POINT (3 4)']
>>> [ str(f.geom) for f in l.features('INTERSECTS(geom,POINT(3 4))') ]
['POINT (3 4)']
>>> def tx (f):
...    f.geom = geom.Point(2*f.geom.x, 2*f.geom.y)
>>> [str(f.geom) for f in l.features(transform=tx)]
['POINT (2 4)', 'POINT (6 8)']
filter(fil, name=None)

Filters the layer.

fil is the Filter to apply.

name is the optional name to assign to the new filtered layer.

This method returns a newly filtered layer. The new layer is create within the containing workspace of the original layer.

>>> from geoscript.feature import Schema
>>> l = Layer(schema=Schema('original', [('name', str)]))
>>> l.add(['foo'])
>>> l.add(['bar'])
>>> l.add(['baz'])
>>> 
>>> l2 = l.filter("name = 'foo'", "filtered")
>>> l2.count()
1
>>> l3 = l.filter("name LIKE 'b%'", "filtered2")
>>> l3.count()
2
first(filter=None, sort=None)

Returns the first Feature that matches a query.

filter is a optional Filter to constrain the features iterated over.

sort is an optional tuple or list of tuples that defined the order in which features are iterated over. The first value of each tuple is the name of a field to sort on. The second value is one of the strings ‘ASC’ or ‘DESC’, representing ascending and decending sort order respectively.

>>> l = Layer()
>>> from geoscript import geom
>>> l.add([geom.Point(1,2)])
>>> l.add([geom.Point(3,4)])
>>> l.first().geom
POINT (1 2)
>>> l.first('INTERSECTS(geom,POINT(3 4))').geom
POINT (3 4)
histogram(att, classes=10, low=None, high=None)

Generates the histogram of values for an attribute of the layer.

att specifies the value to generate the histogram for and classes specifies the number of buckets to use.

This method returns a list of tuple, one tuple for each bucket. The first value of each tuple is another tuple representing the bucket range, the second value is the number of values within that range.

interpolate(att, classes=10, method='linear')

Generates a set of interpolated values for an attribute of the layer.

att specifies the attribute. classes specifies the number of values to generate.

The method parameter specifies the interpolation method. By default a linear method is used. The values ‘exp’ (exponential) and ‘log’ (logarithmic) methods are also supported.

minmax(att, low=None, high=None)

Calculates the minimum and maximum values for an attribute of the layer.

att specifies the attribute. low and high are used to constrain the value space.

reproject(prj, name=None, chunk=1000)

Reprojects a layer.

prj is the destination Projection

name is the optional name as a str to assign to the resulting reprojected layer.

This method returns a newly reprojected layer. The new layer is create within the containing workspace of the original layer.

>>> from geoscript import geom
>>> l = Layer()
>>> l.proj = 'epsg:4326'
>>> l.add([geom.Point(-111, 45.7)])
>>> 
>>> l2 = l.reproject('epsg:26912')
>>> l2.proj.id
'EPSG:26912'
>>> [f.geom.round() for f in l2.features()]
[POINT (500000 5060716)]
update(feature, fields=None)

Updates a Feature of the layer.

feature is the feature to update. fields is an optional list of field names (as str objects) to update.

>>> from geoscript import geom
>>> l = Layer()
>>> l.add([geom.Point(0,0)])
>>> f = l.first()
>>> f.geom
POINT (0 0)
>>> f.geom = geom.Point(1,1)
>>> l.update(f)
>>> f = l.first()
>>> f.geom
POINT (1 1)