geoscript / geoscript-groovy

A Groovy implementation of GeoScript.
https://geoscript.net/groovy
MIT License
46 stars 22 forks source link

Why does a Feature's id change when its added to a Layer? #37

Closed timkeane closed 7 years ago

timkeane commented 7 years ago

https://github.com/timkeane/geoscript-test/blob/master/src/test/groovy/nyc/FeatureIdTest.groovy

package nyc

import spock.lang.*

import geoscript.feature.*
import geoscript.layer.Layer
import geoscript.geom.*

class ProximityTest extends Specification {

    def 'test that feature ids persist'(){
        given:
            Schema schema = new Schema('mySchema', [['geom', 'Point'], ['name', 'string'], ['address', 'string']])
            def attrs = [geom: new Point(1, 10), name: 'myName', address: 'myAddress']
            Feature inFeature = new Feature(attrs, 'myFID', schema)
            Layer layer = new Layer('myLayer', schema)
            layer.add(inFeature)
        when:
            Feature outFeature = layer.getFeatures()[0]
        then:
            inFeature.getId() == 'myFID' 
            inFeature.getId() == outFeature.getId() 
    }
}
jericks commented 7 years ago

The Feature ID is controlled by the Workspace (GeoTools DataStore). The way you are creating Layers is actually using a Memory Workspace behind the scenes. It creates ids like 'fid-4a2c899615b3b3e6f5b-7ffc'. If you use a Directory Workspace (which creates Shapefiles) you get an id like 'myLayer.1'.

The GeoTools docs have a good explanation: http://docs.geotools.org/stable/userguide/library/data/featuresource.html (look for Handling of FeatureID).

I hope that helps.