[gdal-dev] libkml driver get document name

Jan Vaillant jvail at gmx.de
Wed Feb 24 10:34:42 PST 2016


On 02/24/2016 06:30 PM, Damian Dixon wrote:
> Hi,
>
> I've been meaning to submit a patch for awhile to libkml driver but have
> not had time to do so.
>
> The fix is a one line change highlighted below:
>

Great! Thank you. I'll give it a try.

Jan

> int OGRLIBKMLDataSource::OpenKml (
>      const char *pszFilename,
>      int bUpdate )
> {
>      std::string oKmlKml;
>      char szBuffer[1024+1];
>
>      VSILFILE* fp = VSIFOpenL(pszFilename, "rb");
>      if (fp == NULL)
>      {
>          CPLError ( CE_Failure, CPLE_OpenFailed,
>                     "Cannot open %s", pszFilename );
>          return FALSE;
>      }
>      int nRead;
>      while ((nRead = VSIFReadL(szBuffer, 1, 1024, fp)) != 0)
>      {
>          try
>          {
>              oKmlKml.append(szBuffer, nRead);
>          }
>          catch(std::bad_alloc& e)
>          {
>              VSIFCloseL(fp);
>              return FALSE;
>          }
>      }
>      OGRLIBKMLPreProcessInput(oKmlKml);
>      VSIFCloseL(fp);
>
>      CPLLocaleC  oLocaleForcer;
>
>      /***** create a SRS *****/
>
>      OGRSpatialReference *poOgrSRS =
>          new OGRSpatialReference ( OGRLIBKMLSRSWKT );
>
>      /***** parse the kml into the DOM *****/
>
>      std::string oKmlErrors;
>
>      ElementPtr poKmlRoot = kmldom::Parse ( oKmlKml, &oKmlErrors );
>
>      if ( !poKmlRoot ) {
>          CPLError ( CE_Failure, CPLE_OpenFailed,
>                     "ERROR Parseing kml %s :%s",
>                     pszFilename, oKmlErrors.c_str (  ) );
>          delete poOgrSRS;
>
>          return FALSE;
>      }
>
>      /***** get the container from root  *****/
>
>      if ( !( m_poKmlDSContainer = GetContainerFromRoot ( m_poKmlFactory,
> poKmlRoot ) ) ) {
>          CPLError ( CE_Failure, CPLE_OpenFailed,
>                     "ERROR Parseing kml %s :%s %s",
>                     pszFilename, "This file does not fit the OGR model,",
>                     "there is no container element at the root." );
>          delete poOgrSRS;
>
>          return FALSE;
>      }
>
>      m_isKml = TRUE;
>
>      /***** get the styles *****/
>
>      ParseStyles ( AsDocument ( m_poKmlDSContainer ), &m_poStyleTable );
>
>      /***** parse for layers *****/
>
>      int nPlacemarks = ParseLayers ( m_poKmlDSContainer, poOgrSRS );
>
>      /***** if there is placemarks in the root its a layer *****/
>
> *    if ( nPlacemarks ) {*   //
> <------------------------------------------------------------------------------------------This
> line
>          AddLayer ( CPLGetBasename ( pszFilename ),
>                     poOgrSRS, wkbUnknown,
>                     this, poKmlRoot, m_poKmlDSContainer, pszFilename, FALSE,
> bUpdate, 1 );
>      }
>
>      delete poOgrSRS;
>
>      return TRUE;
> }
>
> Regards
> Damian
>
> On 24 February 2016 at 17:00, Jan Vaillant <jvail at gmx.de> wrote:
>
>> On 02/24/2016 12:53 AM, Even Rouault wrote:
>>
>>> Le mardi 23 février 2016 14:50:35, jan vaillant a écrit :
>>>
>>>> Hi list,
>>>>
>>>> I am reading a zipped kml with gdal's libkml driver. I am trying to
>>>> access
>>>> the name tag of the document:
>>>>
>>>> <kml>
>>>>     <Document>
>>>>       <name>a_name</name>
>>>>
>>>
>>> I did a quick testing It appears that the driver will report a <name> as
>>> the
>>> layer name only if it inside a <Folder> tag, but not as a direct child of
>>> <Document>
>>>
>>
>> Yes, thank you. I tried the kml driver as well and it stores the
>> Document/name and makes it available with layer.getName().
>>
>> Bye
>>
>> Jan
>> _______________________________________________
>> gdal-dev mailing list
>> gdal-dev at lists.osgeo.org
>> http://lists.osgeo.org/mailman/listinfo/gdal-dev
>>
>


More information about the gdal-dev mailing list