A couple of weeks ago Robert Svebeck posted about the use of the map object in Qlik Sense, see here. Besides making cool things with a 3D map, you can also create your own floor plan analysis. Let’s see how we do this.
First step is finding an image that you want to use as your background layer. I used the following background:
In order to be able to zoom in the map object, the image needs to be divided into multiple images. The tool for this is ImageMagick. Download this here. Then download the XLSX that is needed ImageMagick XLSX.
- Open the Excel file
- Enter the location of your image
- Enter the location of the magick.exe
- Give the zoom level (use 4 for this example)
- Go to tab conversion, copy column I.
- Start Windows cmd and paste the content. It will now start to create the images
- Copy the images and paste them in your content directory: C:\Users\{YourAccountName}\Documents\Qlik\Sense\Content\Default\
Now that this part is finished, create a new application on your Qlik Sense Desktop.
Go to the script editor. First set the dimensions of the image. Because you have chosen zoom level 4, the pixels will be 4096 by 4096.
SET vWidth1 = 4096; SET vHeight1 = 4096;
Then define two functions, one for Longitude, one for Latitude projection
SET fX2Long1 = Round((RangeMax($2,$3)/2 - $2/2 + $1) * 40075014 / RangeMax($2,$3) - 20037507); SET fY2Lat1 = Round((RangeMax($2,$3)/2- $3/2 + $1) * -40075014 / RangeMax($2,$3) + 20037507);
Then define function for a diagonal from northwest to southeast. This will be used when you want to zoom out to the original image.
SET fFullArea1 = '[[' & Round((RangeMax($1,$2)/2 - $1/2) * 40075014 / RangeMax($1,$2) - 20037507) & ',' & Round((RangeMax($1,$2)/2 - $2/2) * -40075014 / RangeMax($1,$2) + 20037507) & '],[' & Round((RangeMax($1,$2)/2 + $1/2 ) * 40075014 / RangeMax($1,$2) - 20037507) & ',' & Round((RangeMax($1,$2)/2 + $2/2) * -40075014 / RangeMax($1,$2) + 20037507) & ']]';
We are going to create polygons and there is a big difference in how pixels and coordinates work. In order to do this correctly, let me explain the difference with a small example.
Check the example below to see that 0,0 from the pixels is not the same of 0,0 in coordinates.
1. Pixels work like this:
0,0----------------1,0 | | | | | | 0,1----------------1,1
2. Coordinates work like this:
-1,1---------------1,1 | | | 0,0 | | | -1,-1--------------1,-1
This means if you have an image of 4096 by 4096 pixels, you have to convert the 0,0 (middle of coordinates) to 2048,2048 (middle of pixels). This is both the middle. Then a calculation is needed when pixels are entered to determine if they are positive or negative coordinates.
For this two variables are created:
LET vPos = (20037507/2048); // 9,783.94 this is the scaling factor that is needed LET vNeg = (20037507/2048) * -1; // -9,783.94 this is the scaling factor that is needed
A second thing to remember is the way that polygons are created. They always start in the lower left corner and work clockwise. First A, then B, then C, then D and D will get back to A.
B -------------------- C | | | | | | A -------------------- D
When the script parts are added to the script editor, the transformation from pixels -> coordinates -> polygon is needed. I’ve only put one example here below because of the amount of space that is otherwise needed. Because the example is a rectangle that is not tilted, I only need two points (A & C) to determine the other two (B & D).
Table: LOAD Key, '[[[[' & AX & ',' & AY & '],['& BX & ',' & BY & '],['& CX & ',' & CY & '],['& DX & ',' & DY & ']]]]' as Polygoon ; LOAD Key, IF((2048 - AX) < 0, (AX - 2048) * $(vPos), (2048 - AX) * $(vNeg)) as AX, IF((2048 - AY) < 0, (AY - 2048) * $(vNeg), (2048 - AY) * $(vPos)) as AY, IF((2048 - AX) < 0, (AX - 2048) * $(vPos), (2048 - AX) * $(vNeg)) as BX, IF((2048 - CY) < 0, (CY - 2048) * $(vNeg), (2048 - CY) * $(vPos)) as BY, IF((2048 - CX) < 0, (CX - 2048) * $(vPos), (2048 - CX) * $(vNeg)) as CX, IF((2048 - CY) < 0, (CY - 2048) * $(vNeg), (2048 - CY) * $(vPos)) as CY, IF((2048 - CX) < 0, (CX - 2048) * $(vPos), (2048 - CX) * $(vNeg)) as DX, IF((2048 - AY) < 0, (AY - 2048) * $(vNeg), (2048 - AY) * $(vPos)) as DY, ; LOAD* Inline [ Key, AX, AY, CX, CY, Example, 990, 2342, 1088, 1655, ];
Having finished the script, the application needs to be created. Get the native Map object from Qlik Sense and make sure that you have:
- Set the white base map
- Set projection to User defined (meters)
Then create your first Background layer. Format is TMS and set the URL to: =’http://localhost:4848/Content\Default\{YourImageName}_z${z}_${x}_${y}.png’. After that, go to the Options in the properties panel, click on Layer display and set the zoom custom from 1x zoom – 4x zoom. This is because we have 4 zoom levels.
Then create the Area layer that is used for zooming out to the right level. The formula used is a function with two variables that were defined in the script: $(fFullArea1(vWidth,vHeight)). In the data section of the properties panel you can enter anything you want, it does not matter for this. Make sure that this layer is set at last so the line is not visible in front of the image.
Then at last, another Area layer is created for the polygons. In the Location field you enter your polygon field and they will appear. The coloring as below has been done in the Colors section of the properties panel. This can be based on sales, traffic or availability of the items sold. Additional data can be added in the normal way and can expand the current capabilities.
Now you have created your own floor plan analysis!