1

I have a VectorLayer layer with a feature collection in GeoJSON. The GeoJSON format is as following:

{
    "type": "FeatureCollection",
    "totalFeatures": "unknown",
    "features": [
        {
            "type": "Feature",
            "id": "ES.RRTN.CP.1010001",
            "geometry": {
                "type": "MultiPolygon",
                "coordinates": [
                    [
                        [
                            [
                                570389.865,
                                4722149.567
                            ],
                            [
                                570391.945,
                                4722149.769
                            ],
                            [
                                570394.939,
                                4722150.04
                            ],
                            [
                                570395.084,
                                4722144.205
                            ]
                        ]
                    ]
                ]
            },
            "properties": {
                "validTo": {
                    "simpleContent": null
                },
                "validFrom": {
                    "simpleContent": null
                },
                "inspireId": {
                    "Identifier": {
                        "localId": "1010001",
                        "namespace": "ES.RRTN.CP"
                    }
                },
                "label": "1",
                "nationalCadastralReference": "1010001",
                "areaValue": 101.2779,
                "beginLifespanVersion": {
                    "simpleContent": null
                }
            }
        }
    ]
 }

I know the feature id=1010001, so I can identify a feature by its id, localid and nationalCadastralReference.

Is there a way to get the Feature object from the VectorLayer/VectorSource/Map with one of this parameters?

7
  • 1
    source.getFeatureById('ES.RRTN.CP.1010001')
    – Mike
    Commented Sep 2, 2019 at 8:52
  • I would say that the only way is to loop through features with forEachFeature method of vector source (see openlayers.org/en/latest/apidoc/…) and check for desired value of desired property.
    – TomazicM
    Commented Sep 2, 2019 at 9:01
  • @Mike, you overlooked that desired search key is not feature ID but value of some property.
    – TomazicM
    Commented Sep 2, 2019 at 9:04
  • Thanks @TomazicM, but source.getFeatureById also returns the desired feature via ID so I will keep the code simple and use that.
    – Iñigo
    Commented Sep 2, 2019 at 9:26
  • 1
    Then I misunderstood your question, since you do not mention feature ID as one of the desired search properties.
    – TomazicM
    Commented Sep 2, 2019 at 9:29

1 Answer 1

2

The solution given in the comments finally was:

source.getFeatureById('ES.RRTN.CP.1010001')

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.