Skip to content

African Surveyors Connect | GeoConnect

The geospatial platform for visionaries and those who dare to dream

Menu
  • Posts
    • News
    • General
    • Educational
    • Spotlight
  • Jobs
  • Events
  • More
    • Learn-GIS
    • Magazine
    • Academy
Menu

Converting KML Files to Shapefiles using Python: A Step-by-Step Guide

Posted on February 10, 2023February 10, 2023 by KUMBIRAI MATINGO

KML (Keyhole Markup Language) and shapefiles are two different file formats used for storing geographical data. While how to spot fake nike dunks KML is used for representing spatial information for display in Google Earth, shapefiles are commonly used for GIS applications. There might be a need Женские кроссовки adidas campus новинка! — цена 2599 грн в каталоге Кроссовки ✓ Купить женские вещи по доступной цене на Шафе , adidas krampon predator black people women ugly , Украина #161888213 to convert KML files to shapefiles for use in a GIS application or vice versa. This can be easily achieved using python programming language.

  • Air Jordan XXX Revealed
  • air jordan 13 wheat release information

In this article, we will demonstrate how to convert a KML file to a shapefile using Python.

Prerequisites

Before we begin, make sure that you have the following python libraries installed:

  • Fiona
  • geopandas
  • pyproj

If you don’t have these libraries installed, you can install them by running the following command in your terminal:

pip install fiona geopandas pyproj

Converting KML to shapefile

The process of converting a KML file to a shapefile involves the following steps:

  1. Read the KML file into a GeoDataFrame.
  2. Write the GeoDataFrame to a shapefile.

Here is the code that implements these steps:

import geopandas as gpd

# Read the KML file into a GeoDataFrame
kml_file = "path/to/kml/file.kml"
gdf = gpd.read_file(kml_file, driver='KML')

# Write the GeoDataFrame to a shapefile
shp_file = "path/to/shapefile/file.shp"
gdf.to_file(shp_file, driver='ESRI Shapefile')

That’s it! Now you have successfully converted a KML file to a shapefile using python.

Conclusion

In this article, we demonstrated how to convert a KML file to a shapefile using Python. The process involves reading the KML file into a GeoDataFrame and writing the GeoDataFrame to a shapefile. By using the geopandas and fiona libraries, the process is straightforward and easy to implement. With these tools, you can now integrate KML data into your GIS workflow or vice versa.

KUMBIRAI MATINGO
KUMBIRAI MATINGO

Kumbirai is a GIS & MEAL specialist using geospatial analytics to advance global health and social impact. A certified Data Protection Officer (DPO), an open-data advocate and self-taught software developer, he builds web GIS tools that turn field data into decisions. He lectures in GIS/Remote Sensing and mentors emerging practitioners. Founder of a geospatial startup and nonprofit, he believes, “Real geospatial innovation happens when we empower communities with the right tools and knowledge.” Open to consulting and collaborations.

Share this:

  • Tweet
  • Click to share on Telegram (Opens in new window) Telegram
  • Click to share on WhatsApp (Opens in new window) WhatsApp
  • Click to share on Reddit (Opens in new window) Reddit
  • Click to print (Opens in new window) Print

Related Articles You Might Like

3 thoughts on “Converting KML Files to Shapefiles using Python: A Step-by-Step Guide”

  1. Huzaifa Muhammad Mukhtiar says:
    January 16, 2024 at 12:41 pm

    I am receiving this error, when I run your code:
    Traceback (most recent call last):
    File “c:\Users\Social Media\Desktop\python\Desktop\python\.ipynb_checkpoints\kml2shp”, line 7, in
    gdf = gpd.read_file(kml_file, driver=’KML’)
    ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
    File “C:\Users\Social Media\Desktop\python\charting\Lib\site-packages\geopandas\io\file.py”, line 297, in _read_file
    return _read_file_fiona(
    ^^^^^^^^^^^^^^^^^
    File “C:\Users\Social Media\Desktop\python\charting\Lib\site-packages\geopandas\io\file.py”, line 338, in _read_file_fiona
    with reader(path_or_bytes, **kwargs) as features:
    ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
    File “C:\Users\Social Media\Desktop\python\charting\Lib\site-packages\fiona\env.py”, line 457, in wrapper
    return f(*args, **kwds)
    ^^^^^^^^^^^^^^^^
    File “C:\Users\Social Media\Desktop\python\charting\Lib\site-packages\fiona\__init__.py”, line 305, in open
    colxn = Collection(
    ^^^^^^^^^^^
    File “C:\Users\Social Media\Desktop\python\charting\Lib\site-packages\fiona\collection.py”, line 252, in __init__
    self.guard_driver_mode()
    File “C:\Users\Social Media\Desktop\python\charting\Lib\site-packages\fiona\collection.py”, line 273, in guard_driver_mode
    raise DriverError(“unsupported driver: %r” % driver)
    fiona.errors.DriverError: unsupported driver: ‘KML’

    1. KUMBIRAI MATINGO says:
      January 23, 2024 at 3:32 pm

      Hi Huzaifa, I am sorry to hear about your experience. After the article was published there have been a number of changes to Python libraries and versions. Can you try out the following if the issue has not yet been resolved:

      pip install libkml

      After installing ‘libkml’, modify your code to use the ‘libkml’ driver for reading KML files. Here’s the updated code:


      import geopandas as gpd

      # Read the KML file into a GeoDataFrame using libkml driver
      kml_file = "path/to/kml/file.kml"
      gdf = gpd.read_file(kml_file, driver='libkml')

      # Write the GeoDataFrame to a shapefile
      shp_file = "path/to/shapefile/file.shp"
      gdf.to_file(shp_file, driver='ESRI Shapefile')

      I hope this will resolve your issue.

      1. Ernie says:
        February 8, 2024 at 9:10 pm

        I am having the same problem using Python 3.11. Fiona no longer has a KML driver. When I tried to pip install libkml in the Spyder IDE I got this error:

        ERROR: Could not find a version that satisfies the requirement libkml (from versions: none)
        ERROR: No matching distribution found for libkml

        These are the current fiona drivers:
        print(fiona.supported_drivers)
        {‘DXF’: ‘rw’, ‘CSV’: ‘raw’, ‘OpenFileGDB’: ‘raw’, ‘ESRIJSON’: ‘r’, ‘ESRI Shapefile’: ‘raw’, ‘FlatGeobuf’: ‘raw’, ‘GeoJSON’: ‘raw’, ‘GeoJSONSeq’: ‘raw’, ‘GPKG’: ‘raw’, ‘GML’: ‘rw’, ‘OGR_GMT’: ‘rw’, ‘GPX’: ‘rw’, ‘Idrisi’: ‘r’, ‘MapInfo File’: ‘raw’, ‘DGN’: ‘raw’, ‘PCIDSK’: ‘raw’, ‘OGR_PDS’: ‘r’, ‘S57’: ‘r’, ‘SQLite’: ‘raw’, ‘TopoJSON’: ‘r’}

Comments are closed.

Search for Articles

Latest Posts

  • Deploying a GeoDjango App on Heroku
  • The Truth About Self-Taught Skills No One Tells You
  • Transformative Mapping Event Launches in Chinhoyi: A Testament to Youth Engagement and Community Collaboration 
  • GeoConnect Awarded Grant for Transformative OpenStreetMap Project in Chinhoyi, Zimbabwe
  • Unlocking Data Treasure Troves: A Guide to Accessing Zimbabwe’s Geospatial Datasets

In today’s digital age, a website is the face of your brand, and having a well-designed and functional website is essential for businesses to succeed online. Our web development services combine creativity and technical expertise to build custom websites that not only look great but also deliver a seamless user experience across all devices.

From responsive design to e-commerce integration, our team of web developers has the skills and experience to bring your vision to life and help your business thrive in the online world. Let us help you create a website that showcases your brand and drives growth for your business. Get in touch via WhatsApp on +263-77-6-887-606 or send me an Email on matingonk@gmail.com

While we have renamed and re-branded to GeoConnect, we have dedicated this platform to continue our work in sharing and exchanging knowledge with the community that has helped us become what we are today.

The journey and legacy continues. Learn more about Geo-Connect.

Quick Links / Resources

  • Legal
  • About Us
© 2025 African Surveyors Connect | GeoConnect | Developed for the profession with support from KM-Spatial