0

I am using QGIS to do some basic data processing.

I have a shapefile (originated from OSM data) that has a number of fields. One of those fields (tags) contains a bunch of tag data.

Example of a "tags" field record below:

"name"=>"Felice Island", "place"=>"island", "osm_uid"=>"14512", "osm_user"=>"DENelson83", "way_area"=>"1.94426e-05", "osm_version"=>"1", "osm_changeset"=>"59732201", "osm_timestamp"=>"2018-06-11T08:10:52Z"

What I want to do is create a brand new field that contains just the values of the "way_area" tags.

I've been trying to set this up using the QGIS attribute table but can't find a way to do it.

2

Your other_tags field is an HStore type.

In QGIS 3.4, you can populate your new field with the expression below:

map_get( hstore_to_map(  "other_tags" ), 'way_area' )

or another syntax in QGIS 3.6

hstore_to_map("other_tags")['way_area']

The hstore_to_map expression taks an HStore field and returns a dictionary with key/value.

In QGIS 3.6 (February 2019), you can use the Explode HStore Field algorithm and give the way_area parameter. Without specifying a list of field, the algorithm will create one new field for each key in your HStore field.

| improve this answer | |
0

How about something like:

replace( array_get( regexp_matches("your field name",'"way_area"=>"([^,]*)') ,0) ,'"','')

in the field calculator? Which will do it with a regex.

Not exactly elegant, but gets the job done as I don't think QGIS has support for JSON in fields.

| improve this answer | |
  • I tried this in the field calculator but the syntax did not work. A few manual replace's might do the trick but am not sure how to get a wildcard replace to work for the whole field. If I can whittle down the following (example record): "name"=>"Riley Island", "place"=>"island", "osm_uid"=>"14512", "osm_user"=>"DENelson83", "way_area"=>"5.6119e-06", "osm_version"=>"1", "osm_changeset"=>"59732201", "osm_timestamp"=>"2018-06-11T08:10:57Z" to "way_area"=>"5.6119e-06" then I think I can make this work. – Canadian Map Makers Jul 24 '18 at 20:46
  • what did you try? what was the result? – Stev_k Jul 24 '18 at 21:36
  • If I create a dataset with a field the same as that, and run the function above in field calculator on that field, I get the number 5.6119e-06. If you need it as text "way_area"=>.... then you probably need to modify your question – Stev_k Jul 25 '18 at 9:03
  • When I try this I get an invalid expression error: (Parser Error: syntax error, unexpected '(', expecting COMMA or ')' Eval Error: No root node! Parsing failed? Although I've found another way to do this, it would be nice to get this to work as it would be much more elegant. The floating point number BTW is exactly what I need. – Canadian Map Makers Jul 26 '18 at 19:04
  • which version of QGIS are you using? If not on 3, maybe try that? – Stev_k Jul 27 '18 at 8:21

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.