1

I have a CSV that has the Jsonata ( $string($eval(Location).coordinates) ) representation of the feature in a column. It is not stored in a .json file.

Can we import this into QGIS so that it has the attribution and spatial representation?

1   [Line]  [[145.44718131423,-17.2122806274458],[145.447615496814,-17.2116429871501],[145.448055714369,-17.2121269022115]] Test Line
2   [Polygon]       [[[145.443768203259,-17.2042553589467],[145.447121970356,-17.202962405607],[145.447320118546,-17.2054797649401],[145.443768203259,-17.2042553589467]]]  Test Polygon

I have tried to import using Delimited Text:WKT but get an error as this isn't valid WKT.

2 Answers 2

5

Your CSV file contains GeoJSON coordinates. You need to preprocess the file to GeoJSON. I've created a sample, a Python script you can run within PyQGIS console or Python (no dependencies to QGIS in my code). It will output a GeoJSON file you can open as a vector file. I catch when Line, Polygon otherwise coordinates are considered as points

Here the CSV file input sample in.csv (tab separated)

identifier  typegeom    coordinatesjson title
1   [Line]  [[145.44718131423,-17.2122806274458],[145.447615496814,-17.2116429871501],[145.448055714369,-17.2121269022115]] Test Line
2   [Polygon]   [[[145.443768203259,-17.2042553589467],[145.447121970356,-17.202962405607],[145.447320118546,-17.2054797649401],[145.443768203259,-17.2042553589467]]]  Test Polygon

Here the code to process the file to GeoJSON

import csv
import json

features = []
with open('in.csv') as inputfile:
    reader = csv.DictReader(inputfile, delimiter='\t')
    for row in reader:
        geom = {"coordinates": json.loads(row['coordinatesjson'])}
        if row['typegeom'] == '[Polygon]':
            geom["type"] =  "Polygon"
        elif row['typegeom'] == '[Line]':
            geom["type"] = "LineString"
        else:
            geom["type"] = "Point"
        row.pop('coordinatesjson')
        feature = {
           "type": "Feature",
           "properties": row,
           "geometry": geom
        }
        features.append(feature)

with open('out.geojson', 'w') as outfile:
    json.dump({"type": "FeatureCollection", "features": features}, outfile)

The output file out.geojson look like below

{"type": "FeatureCollection", "features": [{"type": "Feature", "properties": {"identifier": "1", "typegeom": "[Line]", "title": "Test Line"}, "geometry": {"coordinates": [[145.44718131423, -17.2122806274458], [145.447615496814, -17.2116429871501], [145.448055714369, -17.2121269022115]], "type": "LineString"}}, {"type": "Feature", "properties": {"identifier": "2", "typegeom": "[Polygon]", "title": "Test Polygon"}, "geometry": {"coordinates": [[[145.443768203259, -17.2042553589467], [145.447121970356, -17.202962405607], [145.447320118546, -17.2054797649401], [145.443768203259, -17.2042553589467]]], "type": "Polygon"}}]}
0

The content of the column is not geometry in wkt format. The right example string is: POLYGON ((coordinates)).

Depending on the size of the file, you can edit it manually, or use, for example, notepad ++ and [POLYGON] to convert to POLYGON and parentheses. [] on ().

If you have three brackets [[[that means MULTIPOLYGON.

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.