Shp to KML KMZ – Convert Shapefile to Keyhole Markup Language

shp to kml

Shapefile Shp to KML KMZ Convert. Many times you may want to render your shapefile directly in Google Map and check the view. Or you may want to convert the shapefile shp to KML so that it becomes easy to program with Google Map API. You can also check the file with Google Earth by loading KML converted from Shapefile.

KML is keyhole markup language file format, used to display geographic data on earth browser such as google earth and ArcGIS Explorer etc. This is XML tag-based structure. It is a highly portable single file that can contain all of a layer or map’s elements, such as feature geometry, imagery, symbology, descriptions, attributes, imagery, and other related content. Similarly .kmz  is spatial format, which is a compressed or zipped file of KML extension. In GIS vector and raster data formats are saved differently where as KML composed of including elements as line point polygon and imagery. While shapefile is a Vector file, comprised of three files i.e .shp, .shx and .dbf extension.

KML File Format To Know Before Converting from Shapefile or SHP

Before taking a look on Converting Shapefile to KML or KMZ file, lets first look over how KML file look like. This is essential as if kml file is corrupted then conversion is not possible. And if you know file format, you can correct the same as we can edit the kml in text editor. If you already know the file format of KML, you may skip this step and look directly for Convert Shapefile shp to KML using ogr2ogr tool. Every geographic element such as point line or polygon is specified by different tags.

SHP to KML Conversion IGIS Map Tool

  • Go to IGIS Map Tool
  • Login to the tool if you are already have a account on IGIS Map Tool  otherwise Sign up.
  • Click on Converter

shp to kml

  • You can directly upload from your database or from Google drive or else from dropbox.

shp to kml

  • We are using google drive.  Once file upload is complete, select the output format. We tap on KML for output.

shp to kml

  • Click on Convert file button.

shp to kml

  • Here is your Converted Shp to Kml file. Tap on published button.

Download World Shapefile for project

Point Data KML file format – shp to kml

It contains geographic location, title and description as latitude and longitude.

 <name>Simple placemark</name>
 <description>Attached to the ground. Intelligently places itself
 at the height of the underlying terrain.</description>

Line Data KML file format – shp to kml

Using style tag you can specify the color and width for lines.

<Style id="yellowLineGreenPoly">
 <name>Absolute Extruded</name>
 <description>Transparent green wall with yellow outlines</description>
 <coordinates> -112.2550785337791,36.07954952145647,2357

The <tessellate> tag breaks the line up into smaller chunks, and the <extrude> tag extends the line down to the ground.

Polygon Data KML file format – shp to kml

 <name>The Pentagon</name>

There are many option tags in KML.

Convert Shapefile SHP to KML KMZ

In this article we will have a look on how shapefile can be converted in KML. For conversion ogr2ogr command has used which should be written on terminal (command promt). You need to install ogr2ogr tool, which is open source tool and can be installed in Windows, Ubuntu, Mac or any other linux system.

Note that for converting shapefile to KML you must have shp along with shx file and dbf file. As shx file contains the positional index of the feature geometry whereas shp format contains geometry itself and dbf contains the data associated with the feature.

Command Format for using ogr2ogr tool to convert to kml :

---- ogr2ogr -f KML outputfilename.kml inputfilename.shp

Here is an example command line, to show how i had use ogr2ogr tool to convert the shp to kml file.

convert-shapefile to kml/kmz

Similarly you can convert shapefile to KML using QGIS tool.

Convert SHP to KML or KMZ using coding-

To convert in KMZ format you can just change the extension of output file from .kml to .kmz format. The option -f (output file format) remain same for both.

In PHP you can use this command using shell_exec() method. Here a function is created in which shapefile and Output file name is taken from user.

public function shptokml($shpfilepath,$output){
 $query= "ogr2ogr -f KML $output.kml $shpfilepath ";

Similarly for KMZ, you can have a function as given below-

public function shptokmz($shpfilepath,$output) {
 $query="ogr2ogr -f KML output.kmz $shpfilepath";

The converted output will contain all attribute details with their geographic location in latitude longitude form.

convert-shapefile to kml/kmz


KML only supports EPSG: 4326 i.e. +proj=longlat +datum=WGS84 . So you can not change the coordinate system of KML. But you can change the coordinate system of shapefile before converting it in KML.

Similarly you may also look for converting shapefile to GeoJSON, Shapefile to Topojson, Shapefile to Geopackage, Shapefile to Postgresql, shapefile to CSV etc. Also you can look for reverse convert the same i.e from KML to shapefile using QGIS or ogr2ogr tool.

Hope you enjoyed this article and successfully converted shapefile into KML. If you find any difficulty during execution of this article please let us know by commenting below in the box provided.

Author: Akshay Upadhyay

Owner and Director of a Private Limited company which serves individual to large scale industries in the field of Maps and GIS. He is a Gold Medalist in M.Tech(Spatial Information Technology) and owns some famous Technology blogs and website... Know more