[GRASS-user] How to import KML polygons in GRASS?

Maybe I am just loosin' my time struggling to import KML polygons in
GRASS but here is my attempt and my problems. I have read past threads
but I didn't really found the "SOLUTION"

I understand that it should be easy to play with xpath and xslt but
since KML's are XML's but, again, I have no idea about xpath and xslt
and I try do it the way I "know" more or less... ;-p

If there is already a solution... I would be more than greatful since I
am in the last phase of my current work.

Here is my attempt to import coordinates the "hard way":
---

1. Decompress KMZ and get the "KML"

# I was told, and I read, that KML are just XML's. So...
2. Replacing the KMLheader <kml xmlns="http://earth.google.com/kml/2.2&quot;&gt;

with

<?xml-stylesheet type="text/xsl" href="kml2html.xsl" ?>
<kml>

3. Saving the KML file in the same folder with an .xsl (in my case
kml2html.xsl given by a friend --- file attached)

4. Open with a browser and either (a) "copy-paste" the coordinates
for which I am interested or (b) "save as" txt file.

5. I choose (a) for my first attempt and save some coordinates in a txt
file and try to import them "sacrificing" the meta-data.

["example.xyz" with coordinates attached.]

The problem is that the coordinates are given in form of x,y,z but
continuously... that is one "x,y,z" field after another separated by
"space" and not in separate lines!

It looks like this:

x1,y1,z1 x2,y2,z2 x3,y3,z3 [...] xi,yi,zi

and NOT like this:

x1,y1,z1
x2,y2,z
x3,y3,z3
[...]
xi,yi,zi

6. I have studied v.out.ascii (since it is the reverse of v.in.ascii)
and I came up with this bash loop which WORKS in the command line. I can
import the desired coordinates in GRASS and I think it should be easy to
"clean" and convert in areas.

["example.grass-ready.xyz" attached]

The problem is that my "solution" DOES NOT work in a script due to an
error in "cut -d ' ' -f$i #### How can I use in -f a variable (stepwise
increasing number) instead of a constant number?

INPUT=example.xyz

OUTPUT= just a name

TYPE=B ### "B" for "boundaries" ...or "L" for lines better?

i=0

PRIMITIVES="`cat $INPUT | wc -w`"

echo "$TYPE $PRIMITIVES" > $OUTPUT.grass-ready; cat $OUTPUT.grass-ready
| echo "\nVERTI:">> $OUTPUT.grass-ready

until [ $i -gt $PRIMITIVES ]; do cat $INPUT | cut -d' ' -f$i | tr ',' '
' >> $OUTPUT.grass-ready; let i=i+1; done ### Note in tr ',' ' ' --
second character is a space

7. If this is going to work I was thinking to "grep" somehow the
metadata out of the "saved as txt file" and force it with a script in
the X.grass-ready ascii

...
Some more questions:

* ogr officially supports "KML" reading and writing. I can't read with
ogrinfo any KML file. I have read about "expat" support... but isn't
this activated by default in ogr coming with FWTools?

Sorry for the QGIS question:

* QGIS suppports GML reading?

(attachments)

kml2html.xsl (511 Bytes)
example.xyz (2.57 KB)
example.grass-ready.xyz (2.67 KB)

WOW!

Finally... I have got working using this
http://spanring.name/blog/wp-content/files/kml2gml.xsl

from here http://www.perrygeo.net/wordpress/?p=3

which most of you must be already aware I suppose!

Cool... it works since QGIS does read GML's.

I am still interested in my "SOLUTION" (especially in the "-f" parameter
in the "cur" command).

Does ogr2ogr convert from GML to GRASS vector files?

Oh... I am trying it out right away... :wink:

On Sun, 2008-02-24 at 16:05 +0100, Nikos Alexandris wrote:

Maybe I am just loosin' my time struggling to import KML polygons in
GRASS but here is my attempt and my problems. I have read past threads
but I didn't really found the "SOLUTION"

I understand that it should be easy to play with xpath and xslt but
since KML's are XML's but, again, I have no idea about xpath and xslt
and I try do it the way I "know" more or less... ;-p

If there is already a solution... I would be more than greatful since I
am in the last phase of my current work.

Here is my attempt to import coordinates the "hard way":
---

1. Decompress KMZ and get the "KML"

# I was told, and I read, that KML are just XML's. So...
2. Replacing the KMLheader <kml xmlns="http://earth.google.com/kml/2.2&quot;&gt;

with

<?xml-stylesheet type="text/xsl" href="kml2html.xsl" ?>
<kml>

3. Saving the KML file in the same folder with an .xsl (in my case
kml2html.xsl given by a friend --- file attached)

4. Open with a browser and either (a) "copy-paste" the coordinates
for which I am interested or (b) "save as" txt file.

5. I choose (a) for my first attempt and save some coordinates in a txt
file and try to import them "sacrificing" the meta-data.

["example.xyz" with coordinates attached.]

The problem is that the coordinates are given in form of x,y,z but
continuously... that is one "x,y,z" field after another separated by
"space" and not in separate lines!

It looks like this:

x1,y1,z1 x2,y2,z2 x3,y3,z3 [...] xi,yi,zi

and NOT like this:

x1,y1,z1
x2,y2,z
x3,y3,z3
[...]
xi,yi,zi

6. I have studied v.out.ascii (since it is the reverse of v.in.ascii)
and I came up with this bash loop which WORKS in the command line. I can
import the desired coordinates in GRASS and I think it should be easy to
"clean" and convert in areas.

["example.grass-ready.xyz" attached]

The problem is that my "solution" DOES NOT work in a script due to an
error in "cut -d ' ' -f$i #### How can I use in -f a variable (stepwise
increasing number) instead of a constant number?

INPUT=example.xyz

OUTPUT= just a name

TYPE=B ### "B" for "boundaries" ...or "L" for lines better?

i=0

PRIMITIVES="`cat $INPUT | wc -w`"

echo "$TYPE $PRIMITIVES" > $OUTPUT.grass-ready; cat $OUTPUT.grass-ready
| echo "\nVERTI:">> $OUTPUT.grass-ready

until [ $i -gt $PRIMITIVES ]; do cat $INPUT | cut -d' ' -f$i | tr ',' '
' >> $OUTPUT.grass-ready; let i=i+1; done ### Note in tr ',' ' ' --
second character is a space

7. If this is going to work I was thinking to "grep" somehow the
metadata out of the "saved as txt file" and force it with a script in
the X.grass-ready ascii

...
Some more questions:

* ogr officially supports "KML" reading and writing. I can't read with
ogrinfo any KML file. I have read about "expat" support... but isn't
this activated by default in ogr coming with FWTools?

Sorry for the QGIS question:

* QGIS suppports GML reading?
_______________________________________________
grass-user mailing list
grass-user@lists.osgeo.org
http://lists.osgeo.org/mailman/listinfo/grass-user

--
Nikos Alexandris
.
Department of Remote Sensing & Landscape Information Systems
Faculty of Forestry & Environmental Sciences, Albert-Ludwigs-University Freiburg
.
Tel. +49 (0) 761 203 3697 / Fax. +49 (0) 761 203 3701 / Skype: Nikos.Alexandris
.
Address: Tennenbacher str. 4, D-79106 Freiburg i. Br., Germany

On Sun, Feb 24, Nikos Alexandris wrote:

Does ogr2ogr convert from GML to GRASS vector files?

No, but it does convert KML to whatever else, and you can import ogr
datasources (=KML for instance) directly into GRASS.

\\kristian
--
... et nemo ex vobis interrogat me: »Quo vadis?«

I did try that in the past without success. I will try again...

Do you have a practical example maybe?

Cheers,

Nikos

On Sun, 2008-02-24 at 19:35 +0100, Kristian Thy wrote:

On Sun, Feb 24, Nikos Alexandris wrote:
> Does ogr2ogr convert from GML to GRASS vector files?

No, but it does convert KML to whatever else, and you can import ogr
datasources (=KML for instance) directly into GRASS.

\\kristian

On Sun, Feb 24, Nikos Alexandris wrote:

Do you have a practical example maybe?

I haven't used KML for much, but here:
http://quovadis.dk/index.cgi/grass_is_greener%3A2008-02-17%3Ageo%2Cgrass
is a general example showing reading an ogr datasource into GRASS. If
you got the KML > GML working, you should be able to use the GML file as
the ogr dsn.

\\kristian
--
... et nemo ex vobis interrogat me: »Quo vadis?«

Nikos Alexandris pisze:

If there is already a solution

http://www.gdal.org/ogr/drv_kml.html

Maciek

Thanks for your feedback Kristian.

I got the KML > GML indirectly using a kml2gml.xsl.

I didn't get it directly.

Cheers,

Nikos

On Sun, 2008-02-24 at 19:50 +0100, Kristian Thy wrote:

On Sun, Feb 24, Nikos Alexandris wrote:
> Do you have a practical example maybe?

I haven't used KML for much, but here:
http://quovadis.dk/index.cgi/grass_is_greener%3A2008-02-17%3Ageo%2Cgrass
is a general example showing reading an ogr datasource into GRASS. If
you got the KML > GML working, you should be able to use the GML file as
the ogr dsn.

\\kristian

Nikos Alexandris pisze:

http://www.gdal.org/ogr/drv_kml.html

$ ogrinfo -ro Korinthia_2007.kml
FAILURE:
Unable to open datasource `Korinthia_2007.kml' with the following
drivers.

"ML reading is only available if GDAL/OGR is built with the Expat XML Parser, otherwise only KML writing will be supported." [1]

[1]http://www.gdal.org/ogr/drv_kml.html

Yes,

I read that. But I read that this is enabled with FWTools!

Sorry if I am wrong... I am doanloading and trying again.

Many thanks for your time,

Nikos :wink:

On Sun, 2008-02-24 at 20:29 +0100, Maciej Sieczka wrote:

Nikos Alexandris pisze:
>> http://www.gdal.org/ogr/drv_kml.html

> $ ogrinfo -ro Korinthia_2007.kml
> FAILURE:
> Unable to open datasource `Korinthia_2007.kml' with the following
> drivers.

"ML reading is only available if GDAL/OGR is built with the Expat XML
Parser, otherwise only KML writing will be supported." [1]

[1]http://www.gdal.org/ogr/drv_kml.html

On Sun, Feb 24, Nikos Alexandris wrote:

I read that. But I read that this is enabled with FWTools!

$ ogr2ogr --help

should show you a list of supported drivers.

\\kristian
--
... et nemo ex vobis interrogat me: »Quo vadis?«

First of all:

I am sorry for the mail traffic.

I know it's bothering... but I am really stuck and confused.

I got KML and GML read/write support.

But is Expat an additional driver or just a library?

I can locate an "expat" library in my FWTools folder!

FYI:

$ ogrinfo --formats
Supported Formats:
  -> "GRASS" (readonly)
  -> "ESRI Shapefile" (read/write)
  -> "MapInfo File" (read/write)
  -> "UK .NTF" (readonly)
  -> "SDTS" (readonly)
  -> "TIGER" (read/write)
  -> "S57" (read/write)
  -> "DGN" (read/write)
  -> "VRT" (readonly)
  -> "AVCBin" (readonly)
  -> "REC" (readonly)
  -> "Memory" (read/write)
  -> "CSV" (read/write)
  -> "GML" (read/write)
  -> "KML" (read/write)
  -> "Interlis 1" (read/write)
  -> "Interlis 2" (read/write)
  -> "SQLite" (read/write)
  -> "ODBC" (read/write)
  -> "PGeo" (readonly)
  -> "OGDI" (readonly)
  -> "PostgreSQL" (read/write)
  -> "MySQL" (read/write)

On Sun, 2008-02-24 at 20:36 +0100, Kristian Thy wrote:

On Sun, Feb 24, Nikos Alexandris wrote:
> I read that. But I read that this is enabled with FWTools!

$ ogr2ogr --help

should show you a list of supported drivers.

\\kristian

--
Nikos Alexandris
.
Department of Remote Sensing & Landscape Information Systems
Faculty of Forestry & Environmental Sciences, Albert-Ludwigs-University Freiburg
.
Tel. +49 (0) 761 203 3697 / Fax. +49 (0) 761 203 3701 / Skype: Nikos.Alexandris
.
Address: Tennenbacher str. 4, D-79106 Freiburg i. Br., Germany

OK,

I can open correctly the kml and the gml file with OpenEV which means I
got support for both.

But I wonder why can't I read/ wrtie successfully in the command line or
within GRASS (v.in.ogr)

Am I maybe using another GDAL/OGR version... since I compiled GRASS
manually! I don't remember defining GDAL/OGR when I compiled GRASS
sources...

Maybe I need to sort out some things... in my machine :stuck_out_tongue_winking_eye:

Once again sorry for the mail-bombing!

Nikos Alexandris wrote:

Maybe I am just loosin' my time struggling to import KML polygons in
GRASS but here is my attempt and my problems.

You might try using v.in.gpsbabel,
  http://www.gpsbabel.org/htmldoc-1.3.0/fmt_kml.html
  http://www.gpsbabel.org/htmldoc-1.3.0/fmt_google.html

The problem is that the coordinates are given in form of x,y,z but
continuously... that is one "x,y,z" field after another separated by
"space" and not in separate lines!

It looks like this:

x1,y1,z1 x2,y2,z2 x3,y3,z3 [...] xi,yi,zi

and NOT like this:

x1,y1,z1
x2,y2,z
x3,y3,z3
[...]
xi,yi,zi

perhaps:

cat infile | tr ' ' '\n' > outfile
edit outfile, add "L "
v.in.ascii

or search & replace in nedit or some nice editor.
?

see v.in.garmin, v.in.gpsbabel for awk magic to add "L number" to lines
automatically.

7. If this is going to work I was thinking to "grep" somehow the
metadata out of the "saved as txt file" and force it with a script in
the X.grass-ready ascii

see v.in.garmin, v.in.gpsbabel for ideas on creating a DB table and
linking it to lines by cat number.

* ogr officially supports "KML" reading and writing. I can't read
with ogrinfo any KML file. I have read about "expat" support...
but isn't this activated by default in ogr coming with FWTools?

fyi expat:
$ apt-cache show libexpat1
....
Description: XML parsing C library - runtime library
This package contains the runtime, shared library of expat, the C
library for parsing XML.

If you find a nice solution could you add some instructions on the
wiki?
Including all the links people have mentioned.

Hamish

      ____________________________________________________________________________________
Be a better friend, newshound, and
know-it-all with Yahoo! Mobile. Try it now. http://mobile.yahoo.com/;_ylt=Ahu06i62sR8HDtDypao8Wcj9tAcJ

Hamish,

thank you. You are always there... :wink:

On Sun, 2008-02-24 at 20:53 -0800, Hamish wrote:

Nikos Alexandris wrote:
> Maybe I am just loosin' my time struggling to import KML polygons in
> GRASS but here is my attempt and my problems.

You might try using v.in.gpsbabel,
  http://www.gpsbabel.org/htmldoc-1.3.0/fmt_kml.html
  http://www.gpsbabel.org/htmldoc-1.3.0/fmt_google.html

I tried that in the past. I will re-check it.

> The problem is that the coordinates are given in form of x,y,z but
> continuously... that is one "x,y,z" field after another separated by
> "space" and not in separate lines!
>
> It looks like this:
>
> x1,y1,z1 x2,y2,z2 x3,y3,z3 [...] xi,yi,zi
>
> and NOT like this:
>
> x1,y1,z1
> x2,y2,z
> x3,y3,z3
> [...]
> xi,yi,zi

perhaps:

cat infile | tr ' ' '\n' > outfile
edit outfile, add "L "
v.in.ascii

or search & replace in nedit or some nice editor.
?

see v.in.garmin, v.in.gpsbabel for awk magic to add "L number" to lines
automatically.

Checking that as well...

> 7. If this is going to work I was thinking to "grep" somehow the
> metadata out of the "saved as txt file" and force it with a script in
> the X.grass-ready ascii

see v.in.garmin, v.in.gpsbabel for ideas on creating a DB table and
linking it to lines by cat number.

> * ogr officially supports "KML" reading and writing. I can't read
> with ogrinfo any KML file. I have read about "expat" support...
> but isn't this activated by default in ogr coming with FWTools?

fyi expat:
$ apt-cache show libexpat1
....
Description: XML parsing C library - runtime library
This package contains the runtime, shared library of expat, the C
library for parsing XML.

hmmm... I see. -- yes I have it.

If you find a nice solution could you add some instructions on the
wiki?
Including all the links people have mentioned.

If I make it "merging" all this advices I will definitely do that!

Hamish

Cheers,

Nikos

      ____________________________________________________________________________________
Be a better friend, newshound, and
know-it-all with Yahoo! Mobile. Try it now. http://mobile.yahoo.com/;_ylt=Ahu06i62sR8HDtDypao8Wcj9tAcJ

--
Nikos Alexandris
.
Department of Remote Sensing & Landscape Information Systems
Faculty of Forestry & Environmental Sciences, Albert-Ludwigs-University Freiburg
.
Tel. +49 (0) 761 203 3697 / Fax. +49 (0) 761 203 3701 / Skype: Nikos.Alexandris
.
Address: Tennenbacher str. 4, D-79106 Freiburg i. Br., Germany

You can try what I do, which is to convert the kml to a shapefile with whatever transformation one wants. Then import the shapefile with v.in.ogr.

The ogr2ogr command can look something like this:

ogr2ogr -s_srs EPSG:4326 -t_srs EPSG:26911 my_outfile.shp my_infile.kml

Best,
John

On Feb 24, 2008, at 12:03 PM, Nikos Alexandris wrote:

OK,

I can open correctly the kml and the gml file with OpenEV which means I
got support for both.

But I wonder why can't I read/ wrtie successfully in the command line or
within GRASS (v.in.ogr)

Am I maybe using another GDAL/OGR version... since I compiled GRASS
manually! I don't remember defining GDAL/OGR when I compiled GRASS
sources...

Maybe I need to sort out some things... in my machine :stuck_out_tongue_winking_eye:

Once again sorry for the mail-bombing!
_______________________________________________
grass-user mailing list
grass-user@lists.osgeo.org
http://lists.osgeo.org/mailman/listinfo/grass-user

(Replying partially to myself)

Well,

afterall it works!

I removed gdal-bin (installed from Ubuntu repo's) and build gdal from
source.

With a simple ./configure - make - sudo make install I get gdal/ogr
almost working

( got expat support but lost something else ;-( )

I needed to add in /etc/ld.so.conf the line
"/usr/local/grass-6.3.svn/lib"

Now all works fine:

ogrinfo Korinthia_2007.kml

and

ogr2ogr -s_srs "EPSG:4326" -t_srs "EPSG:2100" test.shp
Korinthia_2007.kml
---

But I am still interested in the idea of grabbing x,y,z and make a
grass-ready-to-import ascii.

Or is it not wothwile to pursue?

Just for the records:

a. kml2html

             1. Rename "Korinthia_estimate_2007.kml" to
                "Korinthia_estimate_2007.xml" and save on the same
                directory with "kml2html.xsl." [1]
             2. Edit the xml file and replace
                
                 <kml xmlns="http://earth.google.com/kml/2.2&quot;&gt;
                
                with
                
                 <?xml-stylesheet type="text/xsl" href="kml2html.xsl" ?>
                 <kml>
                
             3. Save and open with a browser to read content

b. kml2gml

     1. (Same as above but this time save with kml2gml [2])
     2. (Same as above but this time replace with
        
         <?xml-stylesheet type="text/xsl" href="kml2gml.xsl" ?>
         <kml>
        
     3. Open xml with browser and save as gml file
     4. Open with... QGIS for example

c. Of course the easiest way is to get kml read/write support for OGR
and open directly kml files with OpenEV, QGIS or convert directly
with ogr2ogr in other formats.

[1] http://tilaphos.googlepages.com/kml2html.xsl

[2] http://spanring.name/blog/wp-content/files/kml2gml.xsl