2

I have GeoJSON data which has an integer list and I'd like to query that list in QGIS via GUI (select by expression, visualization) and probably later with PyQGIS.

The problem is that QGIS recognizes the field as an "IntergerList" and returns it as a "QString". That wouldn't be much of the problem but QGIS somehow transforms the original value ('[13, 35]') to something very strange ('(2:13,35)'). So QGIS somehow recognizes a List, but instead of returning a String that could be easily converted back to an array via string_to_array you have to process the data to be able to get the values, e.g.:

string_to_array(substr("long_list", strpos("long_list", ':')+1, -1))

So why is QGIS doing this, what are the advantages?

And is there an easy way to process the data in PyQGIS?

Here is the example data:

{
  "type": "FeatureCollection",
  "features": [
    {
      "geometry": {
        "type": "Point",
        "coordinates": [
          12.0,
          52.0
        ]
      },
      "properties": {
        "long_list": [
          13,
          35
        ]
      },
      "type": "Feature"
    }
  ]
}
  • Have you read the GeoJSON driver documentation gdal.org/drivers/vector/geojson.html? Does it answer to any of you questions? The right way to parse (2:13,35) is "list with length 2, values 13 and 35. – user30184 Nov 11 at 13:44
  • Thanks for the link, I read most of it now but couldn't find anything related to how QGIS does deal with IntergerLists, but there seem to be interesting options for opening such files, e.g. FLATTEN_NESTED_ATTRIBUTES and ARRAY_AS_STRING but I can't change how QGIS opens the GeoJSON, do I? – Meibes Nov 11 at 14:07
  • With -oo array_as_string=yes you'll get long_list (String) = [ 13, 35 ], would that make you happy? I don't know how to make QGIS to use the open option directly. It should work to go through OGR VRT gdal.org/drivers/vector/vrt.html. – user30184 Nov 11 at 14:07
  • In the end I want to write a PyQGIS plugin and for that I need to know if the QGIS output of the IntegerList is like it's supposed to be or if no one has cared about it yet or if it is a bug maybe. Because it'll be double work for me if I implement it the way you suggested (thanks so far!) and in the future someone changes the output for a reason I don't know yet and I have to adjust my code accordingly – Meibes Nov 11 at 14:22
  • I believe that the text formatting is tied to GetFieldAsIntegerList in the GDAL API gdal.org/api/ogrfeature_cpp.html and it is not supposed to change often. – user30184 Nov 11 at 18:53
2

With PyQGIS, field in your GeoJSON data is interpreted directly as string and easily converted to an array splitting it by using re module (regular expressions) with multiple delimiters. I named your data as point.geojson and included three additional elements in "long_list" as follows:

{
  "type": "FeatureCollection",
  "features": [
    {
      "geometry": {
        "type": "Point",
        "coordinates": [
          12.0,
          52.0
        ]
      },
      "properties": {
        "long_list": [
          13,
          35,
          17,
          73,
          25
        ]
      },
      "type": "Feature"
    }
  ]
}

It was used in following code:

import re

layer = QgsVectorLayer('point.geojson', 'point', 'ogr')

for feat in layer.getFeatures():
    string = feat.attributes()[0]
    string = re.split(',|:|\)', string)

print(string[1:-1])

where in resulting list:

['13', '35', '17', '73', '25']

were excluded unnecessary first and last elements.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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