Here is a quick guide to creating KML files for use in Qlik Sense based upon Shape files. This can be a quick way to get a usable KML for a Qlik Sense project. We are going to
- Convert the Shape file to KML using the GDAL libraries
- Simplify the KML file to provide a useful level of detail and performance for Sense
- Link the KML file to some useful data
A Shape file is in fact a group of files which act together. You will need to make sure you have the .shp .shx and .dbf files as these are all mandatory. There might also be additional files that come with these three to provide extra information for different use cases.We are going to be using the GDAL library to work with our shape files. Other tools are available, including graphical tools, but I find the very simple approach using GDAL to be very nice.
Install the GDAL Library
First, let's go and grab GDAL from http://www.gisinternals.com/release.php. This will be a stable Windows binary to make life nice and simple, just choose from win32 or x64 builds depending on hardware.
- Browse to http://www.gisinternals.com/release.php
- Download an appropriate binary for your computer
- Install the application
OK, that was nice and easy. If you go to your Start menu you will now find a GDAL Command Prompt application. Launching this will give you a specific command prompt with easy access to the tools in the GDAL library.
Create the KML
- Using the Start menu, launch GDAL Command Prompt
- using the "cd" command, change to the directory where you have saved your Shape files
- e.g. cd "\Users\abh\Desktop\Working\OS Sources\Boundary Line\Data"
- Note if you have spaces in your file names then you will need to enclose the path in double quote marks
Now we can run the commands to convert our shape files into KML. We will be using the ogr2ogr command to perform the conversion. This has a syntax similar to ogr2ogr -f "[Target file type]" [Target file name] [Source File name]There are other options but we'll get to those in a second. Let's run our Shape file through the script to see what we get.
- In the GDAL command prompt use the ogr2ogr command to convert your file
- e.g. ogr2ogr -f "KML" county_region.kml county_region.shp
- Wait for the command to run until you are returned to the command prompt
- Inspect your new KML file, either by dropping it into a new Qlik Sense application or using another tool, such as Google Earth which can display the KML
Hopefully everything worked out nicely and you now have a KML file to use in Qlik Sense. However you might notice that the new file is quite large, especially if you are dealing with boundaries on a national scale.
Simplify the KML
For our example file, we have gone from a 9MB Shape file to a 20MB KML. This is going to cause Qlik Sense to lag a bit when in operation. But why is it doing this? Essentially the level of detail in our KML is far too high. As an example, look at this screenshot of our output KML. I don't think many people care which county that particular rock is in!
We need to simplify our KML which can be done while converting it from the original Shape file with the addition of the "-simplify" flag to our ogr2ogr command
- Re-run the ogr2ogr script with the simplify flag applied
- e.g. ogr2ogr -f "KML" -simplify "10" county_region.kml county_region.shp
- Review the newly created KML file to make sure the file size and accuracy are still fit for your purpose. Adjust the simplify factor as required
As some examples of the different levels here are some sample outputs
|Simplify||File Size||Fine Detail||Larger Detail|
As you can see, we can get down to quite a small file size without losing too much detail. If we go too far we start to miss out larger portions of the shape until we get the gaps between boundaries, as can be seen at the 1000 level Larger Detail image. This might look very messy on a map in Qlik Sense so be careful with higher simplification levels.
If you have a lot of files to convert then you can run the following command to take all of the shape files in a folder and convert them to KML:
for %f in (*.shp) do ogr2ogr -f "KML" -simplify "100" %~nf.kml %f
For each .shp file in this folder, this will run the ogr2ogr command with these settings and save the file with the original file name and the .kml extension.
Link to Other Data in Qlik Sense
Now we have our nice KML file we can load it into Qlik Sense in the normal ways. You should receive a new table with the Name and polygon fields imported so we can link the new KML to our data model through the name. There is other information stored in the KML but by default Qlik Sense will only bring in the minimum two fields required. Hopefully there can be a more complete handling of KML in future versions of Qlik Sense but for now you might be able to access this data if you load the document as XML, and do some mucking around.