2

I am exporting a layer from PostgreSQL to KML using ogr2ogr.

I get a workable KML with a series of features (Placemarks). Here is a simplified example of a feature. (I took out the long list of polygon points to simplify the length of the geometry).

<Placemark>
    <name>FirstFeature</name>
    <description>A map feature</description>
    <Style>
      <LineStyle>
          <color>ff0000ff</color>
      </LineStyle>
      <PolyStyle>
          <fill>0</fill>
      </PolyStyle>
    </Style>
    <ExtendedData>
        <SimpleData name="MapUnit">myunit</SimpleData>
        <SimpleData name="hexcolor">ffe9e9e9</SimpleData>
    </ExtendedData>
    <MultiGeometry>
         <Polygon><outerBoundaryIs><LinearRing>
             <coordinates>[array of coordinates]</coordinates>
         </LinearRing></outerBoundaryIs></Polygon>
    </MultiGeometry>
</Placemark>

I would like to set the layer creation options such that it will refer to the 8bit hex-code provided by the hexcolor attribute. By default, ogr2ogr seems to be styling all polygon features as red lines with transparent fill (see above <Style></Style>). I instead want the style such that the <Style></Style> section becomes...

<Style>
    <LineStyle>
        <color>ff0000ff</color>
    </LineStyle>
    <PolyStyle>
        <color>ffe9e9e9</color> <!--Taken from the hexcolor attribute-->
        <fill>1</fill>
    </PolyStyle>
</Style>

EDIT: Added R code per @Spacedman's request.

EDIT: Removed R code as I realised that @Spacedman was incorrect and this is indeed a question about the KML driver options in OGR. Also dramatically simplified the text to make it easier to read.

  • I think the "R" tag is a bit more important than the "libkml" tag so I've replaced it. – Spacedman Sep 30 '18 at 19:01
  • 1
    Can you add some R code showing how you currently get the data from PG and out to KML? That gives us a starting point. – Spacedman Sep 30 '18 at 19:03
2
+100

Your secret weapon is to create a field in your layer called OGR_STYLE. The ogr LIBKML driver then interprets this to style the output <placemark>s. The relevant documentation is at https://www.gdal.org/ogr_feature_style.html, https://www.gdal.org/drv_libkml.html, and of course https://www.gdal.org/ogr2ogr.html for invocation (though it works from Python too).

If OGR_STYLE is of the form "@stylename", it will get converted into <styleUrl>#stylename</styleUrl> in KML. You can than define the styling for it in KML as you wish.

Alternately, if OGR_STYLE uses the language described in the first link above, it is (reasonably) translated into <lineStyle>,<polyStyle> etc. For instance, for a lineString layer, I just tested (with an export from QGIS desktop, actually) and OGR_STYLE set to PEN(c:#0000ff,w:5px) turns into

 <LineStyle>
     <color>ffff0000</color>
     <width>5</width>
 </LineStyle>

For polygons, you'll use BRUSH(fc:...) for the fill in addition to PEN(c:...) for the border.

You don't mention it, but if you are displaying your layer in e.g. QGIS desktop, you might wonder if you can carry over the QGIS displayed styling directly. That styling is not stored in the layer data, so you can't using ogr2ogr. With the right settings, you can try to do so if exporting from QGIS or using PyQGIS calls. However, the styling capabilities of different visualization engines (QGIS, Google Earth, etc.) are sufficiently incompatible that this works well only in the simplest of cases. I've found it best to either create the target styling in OGR-style language in the OGR_STYLE field, or to do it as a named <styleUrl> in KML.

  • This looks extremely promising! Give me a chance to test it and I will be sure to award your bounty. – Andy Mar 4 at 16:26
  • I was unable to get this to work, presumably because I don't have the correct sub-version of libkml installed. ogr2ogr keeps saying it cannot find libkml - trying to upgrade gdal with brew and I'll see how it goes. Nevertheless, the documentation provided in the answer does seem to be the correct track, so I am marking this as correct. Thanks a ton. – Andy Mar 4 at 17:59
  • 1
    Happy to help. Yes, I understand libkml is an alternative kml driver that may not be compiled in to GDAL standalone. It is in the version with QGIS 3.4 and 3.6, and I've tested what I wrote on it there. According to ogrinfo --version I'm running GDAL 2.4.0. You probably know this, but ogringo --formats should verify if libkml is installed and ogr2ogr -f libkml should ensure it is being used, as opposed to the base kml driver, if it is. – Houska Mar 4 at 18:46
  • Got it to work! Problem was I couldn't get the libkml driver installed on my local machine that runs OSx, but ran fine on our dev server which is Ubuntu and everything looks great. – Andy Mar 4 at 19:59

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.