3

I am trying to read a .kml file (here is an example of the input):

<name>99</name>
    <Style><LineStyle><color>ff0000ff</color></LineStyle><PolyStyle><fill>0</fill></PolyStyle></Style>
    <ExtendedData><SchemaData schemaUrl="#COMPARABLES_APPARTS">
        <SimpleData name="tessellate">-1</SimpleData>
        <SimpleData name="extrude">0</SimpleData>
        <SimpleData name="visibility">-1</SimpleData>
        <SimpleData name="Titref">T5610/71()</SimpleData>
        <SimpleData name="Mappe">28-17-a-16</SimpleData>
        <SimpleData name="Nature">T</SimpleData>            
        <SimpleData name="Carnet">31</SimpleData>
        <SimpleData name="Bon">14</SimpleData>
        <SimpleData name="DateCB">04-07-2018</SimpleData>
        <SimpleData name="Archive">T</SimpleData>
        <SimpleData name="NUM_TITRE">T/5610/71/</SimpleData>
        <SimpleData name="Titre">T/18403/71/</SimpleData>
        <SimpleData name="Montant">1344000.000000000000000</SimpleData>
        <SimpleData name="Superficie">56</SimpleData>
        <SimpleData name="Prix_unita">24000</SimpleData>
        <SimpleData name="Formalité">بيع كلي</SimpleData>
        <SimpleData name="Date_Acte">2019/02/12</SimpleData>
        <SimpleData name="Type Acte">عقد موثق</SimpleData>
        <SimpleData name="Consista_1">شقة</SimpleData>
        <SimpleData name="Origine">T/5610/71/</SimpleData>
        <SimpleData name="Adresse">البيضاء- حي راسين</SimpleData>
        <SimpleData name="Pdite">نورماندي ايطا36/1</SimpleData>
    </SchemaData></ExtendedData>
      <MultiGeometry><Polygon><outerBoundaryIs><LinearRing><coordinates>-7.64047383421453,33.5879835701317 -7.64066743631093,33.5884293463282 -7.64012881378832,33.5885680296946 -7.64007947841229,33.5884516406187 -7.64008802819036,33.5884487806223 -7.64003671338741,33.5882972444107 -7.64007756187161,33.5882929019069 -7.64007969352916,33.5882919614817 -7.64018375142164,33.5882305516264 -7.64031826698891,33.5881379222242 -7.64023531103129,33.5880555158574 -7.6402575429815,33.5880397802828 -7.64047383421453,33.5879835701317</coordinates></LinearRing></outerBoundaryIs></Polygon></MultiGeometry>
  </Placemark>

I found a suggestion on StackOverflow which I believe only reads the first part of the KML

Here is the code and the output

import geopandas as gpd
gpd.io.file.fiona.drvsupport.supported_drivers['KML'] = 'rw'

df = gpd.read_file(path_to_data+transac_file+'.kml', driver='KML')

the output is as follows:

   Name Description                                           geometry
0     93              MULTIPOLYGON (((-7.64047 33.58798, -7.64067 33...
1     99              MULTIPOLYGON (((-7.64047 33.58798, -7.64067 33...
2     81              MULTIPOLYGON (((-7.64047 33.58798, -7.64067 33...
3     87              MULTIPOLYGON (((-7.64047 33.58798, -7.64067 33...
4      9              MULTIPOLYGON (((-7.64047 33.58798, -7.64067 33..

df.columns

[134 rows x 3 columns]
Index(['Name', 'Description', 'geometry'], dtype='object')

I can see from the output that the features have not been read.

I tried another way as follows:

gpd.io.file.fiona.drvsupport.supported_drivers['KML'] = 'rw'    
fp=path_to_data+transac_file+'.kml'  
gdf_list = []
for layer in fiona.listlayers(fp):    
    gdf = gpd.read_file(fp, driver='KML', layer=layer)
    gdf_list.append(gdf)

gdf = gpd.GeoDataFrame(pd.concat(gdf_list, ignore_index=True))

But I am still getiing the same result as before.

How can I extract all features and put them into a dataframe?

1 Answer 1

4

This solved my problem. I can see all features in the dataframe.

I just changed the driver from KML to LIBKML

gpd.io.file.fiona.drvsupport.supported_drivers['LIBKML'] = 'rw'

fp=path_to_data+transac_file+'.kml'

gdf_list = []
for layer in fiona.listlayers(fp):    
    gdf = gpd.read_file(fp, driver='LIBKML', layer=layer)
    gdf_list.append(gdf)

gdf = gpd.GeoDataFrame(pd.concat(gdf_list, ignore_index=True))

The output is:

    Name description timestamp begin end altitudeMode  tessellate  extrude  \
0     93        None       NaT   NaT NaT         None          -1        0   
1     99        None       NaT   NaT NaT         None          -1        0   
2     81        None       NaT   NaT NaT         None          -1        0   
3     87        None       NaT   NaT NaT         None          -1        0   
4      9        None       NaT   NaT NaT         None          -1        0   
..   ...         ...       ...   ...  ..          ...         ...      ...   
129   59        None       NaT   NaT NaT         None          -1        0   
130  117        None       NaT   NaT NaT         None          -1        0   
131  118        None       NaT   NaT NaT         None          -1        0   
132  108        None       NaT   NaT NaT         None          -1        0   
133  116        None       NaT   NaT NaT         None          -1        0 
2
  • Does this example still works? I am trying to reproduce it, but I get an error that 'LIBKML' is an unsupported driver Commented Oct 16, 2023 at 19:11
  • 1
    @RicardoBarrosLourenço try using this method
    – Binx
    Commented Jan 24 at 22:13

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.