KML to SHP Shapefile – Convert

Convert kml to shp shapefile. Have you extracted and downloaded kml file from Google Earth or Google map and want to render it with your geo library which support shapefile? Do you have KML data given from third party and want to give a proper representation of data over map by converting the same in shapefile? If so, here is an article to convert kml to shp using command line tool ogr2ogr or using php code. While if you use QGIS tool, you can look over kml to shp convert using QGIS or else continue using this article.

Here is the online Tool To Convert KML To SHP Shapefile

KML (Keyhole Markup Language) is very popular GIS format. It is available in text format containing tags such as Placemark, SimpleField, SimpleData and ExtendedData etc. It contains whole information in tag. But when you need to see the geometry you may require Shapefile. This conversion can be done using Ogr2ogr utility from GDAL (Geospatial Data Abstraction Library) and other GIS software such as QGIS, ArcGIS etc.

Convert kml to shp – Look Details For KML (Keyhole Markup Language) structure with Line feature-

<Schema name="India_roads" id="India_roads">
 <SimpleField name="MED_DESCRI" type="string"></SimpleField>
 <SimpleField name="RTT_DESCRI" type="string"></SimpleField>
 <SimpleField name="F_CODE_DES" type="string"></SimpleField>
 <SimpleField name="ISO" type="string"></SimpleField>
 <SimpleField name="ISOCOUNTRY" type="string"></SimpleField>
 <SimpleField name="Length_km" type="float"></SimpleField>
 <SimpleField name="Latitude" type="string"></SimpleField>
 </Schema>
 <Folder><name>India_roads</name>
 <Placemark>
 <Style><LineStyle><color>ff0000ff</color></LineStyle><PolyStyle><fill>0</fill></PolyStyle></Style>
 <ExtendedData><SchemaData schemaUrl="#India_raods">
 <SimpleData name="MED_DESCRI">Without Median</SimpleData>
 <SimpleData name="RTT_DESCRI">Secondary Route</SimpleData>
 <SimpleData name="F_CODE_DES">Road</SimpleData>
 <SimpleData name="ISO">IND</SimpleData>
 <SimpleData name="ISOCOUNTRY">INDIA</SimpleData>
 <SimpleData name="Length_km">0.3</SimpleData>
 </SchemaData></ExtendedData>
 <LineString><coordinates>77.8277403975188,35.5012774951028 77.8257523065223,35.4995003281062</coordinates></LineString>
 </Placemark>
 <Placemark>

In the above given example, attribute fields are given with their data type. In style tag geometry style shown with color. Extended Data encloses attribute field name with values. In the last coordinates are given for particular line string. In similar way other lines, point and polygon are given in KML.

You may also look over converting Shapefile to kml using QGIS or shp to kml using ogr2ogr tool.

KML to SHP Shapefile – Convert

    • Go to MapOG Tool  and login with email id or if you are new user then register with a valid email id.
    • After login next step is to  Tap on Switch To and select Converter.
    • Next screen will be opened. Upload your data from system or drag & drop.
    • After uploading your data select the output data. For KML to Shapefile select SHP.
    • If you want your output in different coordinate reference system then click on change CRS.
    • Your converted file is directly published on the map. Download your converted file by clicking download icon.

For step by step approach check the video given below:

Convert from Keyhole Markup Language KML to SHP Shapefile using ogr2ogr utility-

For using this utility you need to install it in your system. To install this utility you can follow given commands,

KML to SHP Shapefile - Convert
KML to SHP Shapefile – Convert

If you already have install this utility or you want to check availability of this utility you can type ogr2ogr in terminal.

KML to SHP Shapefile - Convert

For conversion the given command need to be executed on terminal-

--- ogr2ogr -f 'ESRI Shapefile' Output_sahpefile.shp Input_KML.kml

Here -f defines the output file format and ‘ESRI shapefile’ is driver available.

PHP code for converting KML to SHP shapefile-

If you want the conversion using programing or don’t want to use terminal then this can also be done using PHP. You can create a function that defined this command as string and can give this string in shell_exec() method to execute. Shell_exec() actually execute the command and returns output as string.

public function KML_to_shp($KMLfilepath,$output){
 $query="ogr2ogr -f 'ESRI Shapefile'  $output.gml  $KMLfilepath";
 shell_exec($query);
 }

Generated output in QGIS software-

KML to SHP Shapefile - Convert
KML to SHP Shapefile – Convert

Change Coordinate System of shapefile generated from kml-

Changing coordinate system of output file can be done with this same ogr2ogr utility. It has various option for SRS (Spatial reference system), given below

-a_srs srs_def:

Assign an output SRS

-t_srs srs_def:

Reproject/transform to this SRS on output

-s_srs srs_def:

Override source SRS

These option can be used in command as-

— ogr2ogr -f ‘ESRI Shapefile’ -t_srs EPSG:32643 (new EPSG) -s_srs EPSG:4326 (old EPSG) shpchange.shp output1.kml

Generated shapefile from kml after changing coordinate system in QGIS software-

Convert- KML to Shapefile - Keyhole Markup Language

Hence, we get our shapefile. Furthermore, try our tool MAPOG Tool if you want to conversion in single click.

You also can visit Shapefile to GeoJSON, Shapefile to TopoJSON, Shapfile to geopackage, KML to shapefile conversion using QGIS etc.

If you face any problem during implementing this tutorial, please let us know. Feel free to comment in given comment box.

Even more try your hands on our Conversion Tool

Convert KML to Shapefile in QGIS

Convert KML to Shapefile in QGIS. KML or Keyhole markup language file which store and models geographic features for displaying in Google earth or Google maps, which is not common in use for Map makers and GIS programmers. ESRI made a standard Vector file i.e Shapefile which is most commonly adopted in the field of GIS (Geographic Information System), and this made me to convert KML to shapefile, created for learning purpose from Google map. QGIS (Qunatum GIS) is an open source GIS tool, which provide you a function to convert the KML file to Shapefile, and Vice versa i.e Shapefile to kml file.

You might be looking for converting KML to shapefile when you get the data from third party or from website which download free GIS map data but in different formats which can also be converted to.

If you still don’t prefer using QGIS or GDAL utility to convert the file from kml to shp, then you should also look for KML to Shapefile Conversion using Online Tool.

Convert KML to Shapefile in QGIS
Convert KML to Shapefile in QGIS

You might be interested in seeing QGIS tutorials.

Instead of QGIS you can also try Online Converter tool here MAPOG Online converter tool

Convert KML to Shapefile in QGIS:

1.) Open up the installed QGIS.
2.) Select Layer in the menu bar and press Add Vector Layer or select the shortcut icon. Browse the KML file, in the Source DataSet type, in the pop up window opened.(While browsing, select either all files or Key Hole Markup language in file type). And press Open.

Convert KML to Shapefile in QGIS
Convert KML to Shapefile in QGIS

Select the coordinate reference type system if coordinate reference window pop ups and press ok.

3.) In the Layer file appearing in the left upper side, right click on it, and select save as:

or

you can either select layer from the menu and click on save as.

Convert KML to Shapefile in QGIS`
Convert KML to Shapefile in QGIS

4.) In the Save vector layer as window, Select the format from the drop down box as ESRI Shapefile(OGR) and browse the folder in which you want to save the shapefile. Name it and Press OK.

Convert KML to Shapefile in QGIS
Convert KML to Shapefile in QGIS

Try our tool for conversion: MAPOG Online converter tool

Your Shapefile i.e .shp, .shx and .dbf file will be created in the directed folder. Upload your KML file in your map browser tool, or try our tool, to verify whether the tool preserves all attributes and elements or not. You may also see how to merge two shapefile in QGIS. Or if you are looking to convert that shapefile to kml to Topojson then you can look for GeoJSON to Topojson and Shapefile to GeoJSON and shapefile to TopoJSON. Do comment below if you are getting any problem in converting the file.

QGIS 3.2.1 for Beginners