Modify your custom maps with mapshaper


Custom maps in Einstein Analytics is a brilliant way of portraying your data. I’ve already done a few blogs on the matter (this and this), but all these blogs assume you have the map ready to go. Let’s have a look if you have found a map but it’s not quite ready yet.

How do custom maps work?

Custom maps can be created in Einstein Analytics by uploading a geojson file, which is a standard format for polygon shapes in json. Depending on what your geojson represent there are different ways of creating that file. Typically, I hear an ask for country or region maps based on a specific granular level like city, postcode or even custom sales regions etc. Many countries have an open data platform where you can find a map you can use as a base. I can also suggest looking at the national postal service website. Finally, there is good old Google that at least has allowed me to find some pages with a collection of maps – this and this are examples of those pages. Of course, always check for copy-rights.

Of course, you can also be more creative and take an image and convert that into a map – we do not need to stick to an actual map of a geographic location. I’ve added two random examples I’ve used for presentations at Dreamforce and Dreamin events.

So how do you create that? Well, I can recommend watching Mike Lowe’s tutorial on YouTube, where he walks through each step in the process of converting a SVG image file to a geojson. The video is quite detailed and using free tools, so you should be able to easily follow the steps. Also, Reinhard Michel had written a blog on how to create your own geojson from scratch by using multiple layers, so his blog is a good read as well.

A few facts

Before getting started with modifying some maps let’s just get some facts about custom maps covered.

  • A custom map must be in the geojson format
  • The size for a custom map can max be 10mb. Personally, I would recommend minimizing the file size as I have seen large maps taking a while to load. Read on to see how this can be achieved.
  • In the properties section of the geojson there must be a ‘name’ (or id) parameter. Meaning each polygon (selectable shape of the map) has a name. When you make a grouping in your exploration in Einstein Analytics each name should then exist as a ‘name’ in the geojson.
  • Avoid duplicate ‘name’ values in the geojson. I have had some cases where the map didn’t display properly due to duplicate naming of polygons.

Using Mapshaper

Often when you get your hands on a geojson you want to clean it up and make sure it works for Einstein Analytics and your needs, this is where I find very useful. However, I don’t work with maps every day, so I have a little hard remembering all the commands that are useful, hence I have a document with different use cases, which I, of course, will share with you now.

When you go to mapshaper you will see the option to add your files. I have a map with the different (official) regions in Denmark, which I found on the Danish open data site. I will add that by clicking select.

You will now see the map in the window as shown in the image below.

Let’s have a look at the different use cases. We will be entering our commands in the console which you can access by clicking on the “console” link in the top right of your window.

Creating new fields

Sometimes it can be useful to create new fields especially if you are creating a new geojson from scratch. In mapshaper there is a simple command for that:

$ each customName=customerName

When I use this command each polygon or selectable shape on my map gets a new attribute in the properties section, this new attribute is called whatever you put before and after the equal sign. The value will by default be null, but you can manually populate those values.

Change the value of an attribute

We now have two attributes for each shape. In order for me to change the null value, I can go to inspect the attribute by clicking on the cursor on the right-hand side and click “edit attributes”. Now you can click on any shape and change the null value and of course also the name attribute if you wish. This is especially helpful if you have an existing geojson, but want to make sure the attribute values match what you have in your dataset.

Use a mapping file to create new polygons

Let’s say you want to have your own custom sales regions on a map. You may base this on postcodes, cities or regions, but you don’t want a salesperson to click on all the relevant areas, rather you want them grouped in custom regions you have defined. We can create a mapping file in CSV format and join that with our map.

Alright, the mapping file should contain two columns; one for the name that matches the attribute on the map and one with the custom region. You can see my mapping file below.

Essentially I want to just have a “Vest” and a “Øst” section – which basically just means west and east in Danish.

Now we want to import the mapping file as a second layer and use a join command in the console. Click on the name of your map to import the second layer (mapping CSV) and choose to show the custom map from before. We now need to add the command in the console.

The command is first saying you want to join the CSV file followed by the keys from the map and the CSV finally you say which field you want to add over from your CSV file.

$ join DK_Mapping.csv keys=name,Region fields=Sales_Region

Dissolve polygons

Now we want to keep just the Sales_Region which we just brought over and make sure that the original regions are dropped, hence only “Vest” and “Øst” can be selected. For this, we can use the dissolve command.

$ dissolve Sales_Region

Notice that all the attributes I previously had have been dropped.

Rename fields

Now Einstein Analytics wouldn’t recognize “Sales_Region” as an attribute, we need to use “name” instead. In order to do this, there is a rename command. All we have to do is call the command and say which field to rename and to what.

$ rename-fields name=Sales_Region

If you want to see what other commands you can use you can always type ‘help’ or ‘tips’ and a list of commands will appear.

Make your map smaller

I mentioned in the facts that there is a limit on how large the geojson file can be (10mb) however, my recommendation is if you can make it smaller it would be better for the load time of the map. So how is that then done? Well, mapshaper can help us with this as well.

Right next to where you found the console link there is an option for “Simplify”, which will allow us to make our map less complex and smaller in file size. When you click on it you will see a menu, where you can just keep the settings and hit “Apply”. You will now see a range bar at the top where we can decide how much we want to simplify the shape. Drag the slider until you are happy.

Basically the higher you go in percentage the more unrecognizable it will get as the details of the shape get lost. Obviously, you do not want to go to 0%, but often you can easily go to 80% without losing the details that make this a map you recognize.

Export your map

Once you have made the relevant modifications you can click on the export button in the top right corner of the window. In the menu make sure to select geojson as the file type. If you have multiple layers for mapping reasons you can deselect the layer you don’t need.

Just remember to click export and you have a new map to upload in Einstein Analytics.

You can watch the webinar I delivered on the topic here.

How useful was this post?

Click on a star to rate useful the post is!

Written by

3 thoughts on “Modify your custom maps with mapshaper”

  • 1
    Jarred on March 30, 2021 Reply

    Hi Rikke, thank you for the amazing guide, as per usual. I finally found the issue regarding some of my maps not displaying correctly due to the names of the polygons. But I found out that it’s not the entire name of the polygon, but the starting word. e.g. A polygon named “Cape Town” and another “Cape Town City Centre” will cause the map to not display certain polygons. Is there any way around this? I can manage to change entire duplicate names, but changing each starting word will be impossible. Any feedback would be much appreciated.

    By the way, an amazing tool for creating GeoJSON files and editing their attributes is the open-source tool QGIS.

  • 2
    Steve Groves on September 24, 2021 Reply

    Fantastic article… I followed it to the letter and have not got 4 different maps of the UK – starting with Counties, Main Regions, 9 Regions + London and finally just 9 regions with London as part of SE Region

    Great article – thanks for saving me hours of trying to figure this out myself..

    • 3
      Lawrence Peter on July 25, 2023 Reply

      @steve Groves Please could you share the GEO JSONs

Leave a Reply

Your email address will not be published. Required fields are marked *

This site uses Akismet to reduce spam. Learn how your comment data is processed.