How do I create custom boundaries and use them with QlikMaps?
In this walk through we will join a State-to-Region file with a State shapefile that has geographic boundary data. We will need to join on a common field, so it may be necessary to massage your data before joining. The State-to-Region file used in this tutorial was created with the State.qvd file available on QlikMaps.com/download. You may choose to use any file you would like, but if following along with this tutorial, copy the following code in to a QlikView script.
State: LOAD DivisionID, RegionID, Country, CountryISO3Abbrev, CountryISO2Abbrev, CountryISONum, StateID, StateAbbre, State, StateBoundary, StateCenterLat, StateCenterLng, Region, Region as RegionNoSimplified, Division FROM [..\04 - QVD\State.qvd] (qvd); STORE StateAbbre, State, Region from State into [..\05 - Data\State-to-Region.csv] (txt);
Update the file paths and reload. The State-to-Region.csv will serve as the crosswalk, or mapping file, for our custom Regions.
NOTE: This tutorial uses one of our provided shapefiles to provide geometry, but you can also use your own geometry from your own shapefiles or KML files. Shapefiles provided by us are available at our discretion, upon request.
Install the Encoded Polyline Exporter plugin
In QGIS, search the official repository for the Analytics8 Encoded Polyline Exporter plugin. For a detailed walkthrough, read the How do I install the Encoded Polyline Exporter plugin for QGIS? page.
Add base layer
Add the unsimplified State.shp file by going to Layer -> Add Layer -> Add Vector Layer.
NOTE: It is important that you use the large, unsimplified version found on the download site so no fidelity is lost in later steps. Simplification may cause anomalies or "dirty" polygons upon dissolving.
NOTE: You can also add your own shapefiles or KML files instead.
Create Spatial Index
NOTE: Before you do any geocoding, you should always create a spatial index for each layer you are going to work with. Do this by right-clicking on each layer, going to Properties -> General -> Create spatial index. This will greatly increase the speed of your geocoding processing.
Add crosswalk file
Add the crosswalk file by going to Layer -> Add Layer -> Add text delimited layer.
NOTE: Select 'No Geometry (attribute only table)'
Issues with a crosswalk file
This section may be skipped if your crosswalk file is working.
In some situations, QGIS does not formats text fields as numeric. The most common scenario is if a crosswalk contains zip code information. In this situation, QGIS will remove the leading '0' from the numerical field and cause issues with the join in the next step. This issue can be verified by viewing the field in the attribute table. (It will not show the issue during the import process.)
The simplest way to address this issue is create a schema file which explicitly defines the type of field for each column. To do this, create an empty file in the same directory with the same name as the csv, but use the extension csvt instead. Open the csvt in notepad and define each column as follows:
where "String" is repeated for every row in the csv.
Once done, the csv may be used as normal.
Right click on the State shape file -> Properties -> Joins
Add the crosswalk file
Click on the '+' sign.
- Join layer is the name of the crosswalk file we want to join to the shp file.
- The Join field is the name of the field in the crosswalk file that corresponds to the same field in the shp file.
- The Target field is the name of the field in the shp file that matches the Join Field.
NOTE: If you do not know the name of the fields needed, right clicking on the layer and looking at the attribute table.
Save new layer
The joined data can't be used to create custom shapes until the entire new .shp file is saved.
- Right click on .shp layer and 'Save Vector as..'
- Select a location to save the new .shp files
- Ensure that CRS is EPSG:4326, WGS 84)
- Ensure that 'Add saved file to map' is checked.
Remove extra layers
Right-click on the 'StateRegion_Map' vector layer and 'State vector layer' and select 'Remove'. We do not need these anymore, so this step frees up some system resources.
Deleting extra data
If working with large data sets you should remove any data that is not necessary going forward. Data can be removed by right clicking on the layer and editing the Attribute Table.
Now we want to merge all polygons together that share the same value in the “Region” field. This process is called “dissolve”.
Go to Vector -> Geoprocessing Tools -> Dissolve.
Make sure that “Territory” is set as the “Dissolve Field”, specify and output file name, select “Add result to canvas”, and click OK. This step will take several minutes to complete.
Right-click on the “Region” vector layer and select “Remove.” We do not need this anymore, so this step frees up some system resources.
Go to Vector -> Encoded Polyline -> Encoded Polyline Builder to create a CSV file that you can load into your BI tool to use with QlikMaps. For a more detailed walkthrough, read How do I use the Encoded Polyline Exporter plugin for QGIS?