[gdal-dev] How to read CSV with GeoJSON correctly?

Previous Topic Next Topic
 
classic Classic list List threaded Threaded
5 messages Options
Reply | Threaded
Open this post in threaded view
|

[gdal-dev] How to read CSV with GeoJSON correctly?

jratike80
Hi,

I was thinking about this question on gis.stackexchange https://gis.stackexchange.com/questions/254536/how-to-convert-txt-containing-two-columns-id-and-geojson-geometry

I copy-pasted the CSV example and edited it to use semicolon as field separator. Then I read http://www.gdal.org/drv_csv.html and had a try with ogrinfo

ogrinfo geojson_csv.csv -al -oo GEOM_POSSIBLE_NAMES=geoJSON
INFO: Open of `geojson_csv.csv'
      using driver `CSV' successful.

Layer name: geojson_csv
Geometry: Unknown (any)
Feature Count: 1
Layer SRS WKT:
(unknown)
Geometry Column = geoJSON
Id: String (0.0)
geoJSON: String (0.0)
OGRFeature(geojson_csv):1
  Id (String) = 49518
  geoJSON (String) = {type:Polygon,coordinates:[[[29.9596,-2.3272],[29.9189,-2.7
9],[29.3706,-2.8401],[29.1497,-2.5917],[29.0405,-2.7447],[28.8619,-2.5309],[29.1
1,-1.8602],[29.3616,-1.5092],[29.8237,-1.3089],[29.9149,-1.4825],[30.0524,-1.431
,[30.8394,-1.6514],[30.8567,-2.3151],[30.1356,-2.4382],[29.9596,-2.3272]]]}

The GeoJSON geometry is not recognized but ogrinfo converts it into a string, with double quetes removed. Is there something wrong in the CSV or in my syntax?

-Jukka Rahkonen-
_______________________________________________
gdal-dev mailing list
[hidden email]
https://lists.osgeo.org/mailman/listinfo/gdal-dev
Reply | Threaded
Open this post in threaded view
|

Re: How to read CSV with GeoJSON correctly?

Ian Turton
As i said in my answer to the question the GeoJSON is invalid due to the ring order, but I'm not sure if OGR checks for that.

Ian

On 7 September 2017 at 07:38, Rahkonen Jukka (MML) <[hidden email]> wrote:
Hi,

I was thinking about this question on gis.stackexchange https://gis.stackexchange.com/questions/254536/how-to-convert-txt-containing-two-columns-id-and-geojson-geometry

I copy-pasted the CSV example and edited it to use semicolon as field separator. Then I read http://www.gdal.org/drv_csv.html and had a try with ogrinfo

ogrinfo geojson_csv.csv -al -oo GEOM_POSSIBLE_NAMES=geoJSON
INFO: Open of `geojson_csv.csv'
      using driver `CSV' successful.

Layer name: geojson_csv
Geometry: Unknown (any)
Feature Count: 1
Layer SRS WKT:
(unknown)
Geometry Column = geoJSON
Id: String (0.0)
geoJSON: String (0.0)
OGRFeature(geojson_csv):1
  Id (String) = 49518
  geoJSON (String) = {type:Polygon,coordinates:[[[29.9596,-2.3272],[29.9189,-2.7
9],[29.3706,-2.8401],[29.1497,-2.5917],[29.0405,-2.7447],[28.8619,-2.5309],[29.1
1,-1.8602],[29.3616,-1.5092],[29.8237,-1.3089],[29.9149,-1.4825],[30.0524,-1.431
,[30.8394,-1.6514],[30.8567,-2.3151],[30.1356,-2.4382],[29.9596,-2.3272]]]}

The GeoJSON geometry is not recognized but ogrinfo converts it into a string, with double quetes removed. Is there something wrong in the CSV or in my syntax?

-Jukka Rahkonen-
_______________________________________________
gdal-dev mailing list
[hidden email]
https://lists.osgeo.org/mailman/listinfo/gdal-dev



--
Ian Turton

_______________________________________________
gdal-dev mailing list
[hidden email]
https://lists.osgeo.org/mailman/listinfo/gdal-dev
Reply | Threaded
Open this post in threaded view
|

Re: How to read CSV with GeoJSON correctly?

Even Rouault-2
In reply to this post by jratike80

Hi Jukka,

 

>

> I copy-pasted the CSV example and edited it to use semicolon as field

> separator.

 

That's not enough. As there are quoted strings in the GeoJSON content, they

confuse the CSV reader. So you need to surround the whole GeoJSON content

by double-quotes, and escape the inside double quotes with a repeated double quote character

(You can the use regular comma for separator, but semicolon is fine too)

 

Id;geoJSON

49518;"{""type"":""Polygon"",""coordinates"":[[[29.9596,-2.3272],[29.9189,-2.7028],[29.7235,-2.8189],[29.3706,-2.8401],[29.1497,-2.5917],[29.0405,-2.7447],[28.8619,-2.5309],[29.1194,-2.2488],[29.1361,-1.8602],[29.3616,-1.5092],[29.8237,-1.3089],[29.9149,-1.4825],[30.0524,-1.4314],[30.4704,-1.0525],[30.8394,-1.6514],[30.8567,-2.3151],[30.1356,-2.4382],[29.9596,-2.3272]]]}"

 

 

$ ogrinfo test.csv -al -oo GEOM_POSSIBLE_NAMES=geoJSON -fields=no -q

 

Layer name: test

OGRFeature(test):1

POLYGON ((29.9596 -2.3272,29.9189 -2.7028,29.7235 -2.8189,29.3706 -2.8401,29.1497 -2.5917,29.0405 -2.7447,28.8619 -2.5309,29.1194 -2.2488,29.1361 -1.8602,29.3616 -1.5092,29.8237 -1.3089,29.9149 -1.4825,30.0524 -1.4314,30.4704 -1.0525,30.8394 -1.6514,30.8567 -2.3151,30.1356 -2.4382,29.9596 -2.3272))

 

Even

 

--

Spatialys - Geospatial professional services

http://www.spatialys.com


_______________________________________________
gdal-dev mailing list
[hidden email]
https://lists.osgeo.org/mailman/listinfo/gdal-dev
Reply | Threaded
Open this post in threaded view
|

Re: How to read CSV with GeoJSON correctly?

jratike80
Even Rouault-2 wrote
> Hi Jukka,
> ...
> That's not enough. As there are quoted strings in the GeoJSON content,
> they
> confuse the CSV reader. So you need to surround the whole GeoJSON content
> by double-quotes, and escape the inside double quotes with a repeated
> double quote character
> (You can the use regular comma for separator, but semicolon is fine too)
> ...

Thanks. It is actually well documented in one place of
http://www.gdal.org/drv_csv.html
"Complex attribute values (such as those containing commas, quotes or
newlines) may be placed in double quotes. Any occurrences of double quotes
within the quoted string should be doubled up to "escape" them."

However, it contradicts with what is said about reading spatial information
from CSV
"...Otherwise, if one or several columns contain a geometry definition
encoded as WKT, WKB (encoded in hexadecimal) or GeoJSON, the name of such
column(s) the GEOM_POSSIBLE_NAMES open option."

Adding extra double quotes inside GeoJSON make it invalid as GeoJSON and
JSON as well. I would say that CSV driver does not support GeoJSON encoding
at the moment.

-Jukka-






--
Sent from: http://osgeo-org.1560.x6.nabble.com/GDAL-Dev-f3742093.html
_______________________________________________
gdal-dev mailing list
[hidden email]
https://lists.osgeo.org/mailman/listinfo/gdal-dev
Reply | Threaded
Open this post in threaded view
|

Re: How to read CSV with GeoJSON correctly?

Even Rouault-2

On jeudi 7 septembre 2017 03:16:03 CEST jratike80 wrote:

> Even Rouault-2 wrote

>

> > Hi Jukka,

> > ...

> > That's not enough. As there are quoted strings in the GeoJSON content,

> > they

> > confuse the CSV reader. So you need to surround the whole GeoJSON content

> > by double-quotes, and escape the inside double quotes with a repeated

> > double quote character

> > (You can the use regular comma for separator, but semicolon is fine too)

> > ...

>

> Thanks. It is actually well documented in one place of

> http://www.gdal.org/drv_csv.html

> "Complex attribute values (such as those containing commas, quotes or

> newlines) may be placed in double quotes. Any occurrences of double quotes

> within the quoted string should be doubled up to "escape" them."

>

> However, it contradicts with what is said about reading spatial information

> from CSV

> "...Otherwise, if one or several columns contain a geometry definition

> encoded as WKT, WKB (encoded in hexadecimal) or GeoJSON, the name of such

> column(s) the GEOM_POSSIBLE_NAMES open option."

>

> Adding extra double quotes inside GeoJSON make it invalid as GeoJSON and

> JSON as well. I would say that CSV driver does not support GeoJSON encoding

> at the moment.

 

Hard to support 2 unrelated/incompatible formats at the same time. It supports GeoJSON serialized as a valid CSV string, with CSV quoting and escaping rules (I've just edited the doc to mention that). If you use a graphical editor / spreadsheet and paste the GeoJSON content and save as CSV it should work (which I just did with LibreOffice calc). It is just when you edit directly the format at hand that things get more tricky ;-)

 

--

Spatialys - Geospatial professional services

http://www.spatialys.com


_______________________________________________
gdal-dev mailing list
[hidden email]
https://lists.osgeo.org/mailman/listinfo/gdal-dev