5

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>
0

1 Answer 1

8
+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.

6
  • 1
    This looks extremely promising! Give me a chance to test it and I will be sure to award your bounty.
    – Andy
    Commented Mar 4, 2019 at 16:26
  • 1
    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
    Commented Mar 4, 2019 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
    Commented Mar 4, 2019 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
    Commented Mar 4, 2019 at 19:59
  • A few years later, but let's try: NOTE I cross-posted this question on one of your related Comments - Why can't QGIS 3.16 & KML Tools export point symbology for KML/KMZ. However, it doesn't work with Point geometry. Are you familiar with an OGR2OGR conversion or QGIS export option to utilize the OGR_STYLE field to symbolize points? Note that I tested OGR2OGR data_out.kml data_in.shp with Line geometry, and it worked. Thanks. Commented May 13 at 19:22

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.