Você está na página 1de 11

Create Excel Freeform Shapes from Polygons - Clearly and Simply http://www.clearlyandsimply.com/clearly_and_simply/2015/04/cre...

Clearly and Simply


Intelligent Data Analysis, Modeling, Simulation and Visualization

Home
Archives
Subscribe
Subscribe by email
About
Email Me
Friday, April 17, 2015
Create Excel Freeform Shapes from Polygons
How to transfer polygon data into freeform shapes in Microsoft Excel
If you are a regular reader or even subscribed to this blog, you will not be
surprised to hear that I am working on a new challenge regarding maps in
Microsoft Excel.

We had a lot of articles already on how to create Choropleth Maps in


Microsoft Excel. The new project is about using maps in Excel again, but
with a different type of visualization.

Since I am just starting out on this challenge, I cant provide more details
yet.

Having said that, for the preparation of the new project, I had to work with
geographical polygon data. Now, whats this?

A polygon is [] a plane figure that is bounded by a finite chain of straight


line segments closing in a loop to form a closed chain or circuit. (source: Wikipedia).

In the context of geographical visualization, a polygon represents a region (e.g. country) on a map. Such a polygon
consists of a series of geographical points: pairs of latitude and longitude in a defined order (path). If you draw an
outline through these points in the given order, you get a shape forming the boundary of this region.

And this was my challenge: transfer geographical polygon data into freeform shapes in Microsoft Excel. I am not
sure, but I thought maybe some of you might be interested, so I decided to share my solution.

Todays post first shows how to plot polygon data on standard Excel XY Scatter charts and then describes my
solution to transfer the data into freeform shapes. As always, the article provides a link to download the Excel
workbook for free.

The Data
The underlying database is a simple, but huge Excel table with the polygon data of the borders of all countries in the
world:

1 of 11 3/20/17, 11:42 PM
Create Excel Freeform Shapes from Polygons - Clearly and Simply http://www.clearlyandsimply.com/clearly_and_simply/2015/04/cre...

The table contains the ISO3 abbreviation, the country name, the polygon ID, the point order, the latitude and the
longitude. As I said, simple, but huge: more than 400,000 rows.

I created this polygon data from an ESRI Shapefile of a world map using Richard Leekes fantastic ShapeToTab
utility (more here: Create Your Own Filled Maps in Tableau). Many thanks go again to Richard for his fabulous tools
TabGeoHack and ShapeToTab.

As you can see in the screenshot for the example of Aruba, the first and last point have identical latitude and
longitude values: the 19 points make a closed chain, i.e. the polygon of Arubas boundaries.

Plotting Polygons in an XY Scatter Chart


You can directly plot the polygons on an XY Scatter chart in Excel. No data preprocessing or even coding necessary.
All you have to do is to adjust the minimum and maximum values of the two axes. Lets first try a smaller example
than the world map. Here is the polygon of Austria plotted in an XY Scatter chart:

You can already recognize Austria, right? Changing the chart type to an XY Scatter with smoothed or straight lines
and no markers makes it even clearer:

2 of 11 3/20/17, 11:42 PM
Create Excel Freeform Shapes from Polygons - Clearly and Simply http://www.clearlyandsimply.com/clearly_and_simply/2015/04/cre...

A short distraction, if you dont mind: I love watching The Big Bang Theory, but I simply cant see how Austria on a
map looks like a Wiener:

The Big Bang Theory: Austria

What kind of Wiener are they eating?

Fun fact: in Austria, Wiener are actually called Frankfurter. But I am digressing. Focus attenuation (fits well,
doesnt it?). Reading the last paragraph back, I noticed I am sounding like Sheldon already. So, back to the polygons
and maps.

Lets have a look at Germany. The simple XY scatter:

3 of 11 3/20/17, 11:42 PM
Create Excel Freeform Shapes from Polygons - Clearly and Simply http://www.clearlyandsimply.com/clearly_and_simply/2015/04/cre...

And the same data plotted on an XY Scatter with lines and no markers:

What is going on here? Well, unlike Austria, Germany consists of more than one polygon (23 to be precise):

Since all data is plotted in one data series, Excel is also drawing lines between the start and end points of the
different polygons. To avoid this, we would have to plot the data in 23 separate data series or remove the unwanted

4 of 11 3/20/17, 11:42 PM
Create Excel Freeform Shapes from Polygons - Clearly and Simply http://www.clearlyandsimply.com/clearly_and_simply/2015/04/cre...

lines manually or by VBA.

This becomes a real problem, if you have a lot of polygons in your data. The world map, for instance, consists of 3,775
polygons. Since the maximum number of data series in an Excel chart is 255 (Excel Specifications and Limits), the
option of having one data series per polygon is out of the picture. A VBA routine to delete all unwanted connection
lines between the polygons is the only reasonable way to solve this issue.

Having said that, with very large data sets like the world map, you may even go with a XY Scatter chart with markers
only and no lines:

So much for plotting polygons on Excel charts.

The Challenge
The problem I was facing in my project was different. I didnt want to plot polygons on an XY Scatter chart. I rather
needed to transfer them into freeform shapes. A picture says a thousand words: this is what I was after:

A map in Excel made of freeform shapes (exactly like the ones we used in all the Choropleth Maps in Microsoft Excel
posts) automatically created from the raw polygon data table shown above.

The Implementation
Before starting to code the VBA subs, we have to do a few enhancements on the worksheet:

5 of 11 3/20/17, 11:42 PM
Create Excel Freeform Shapes from Polygons - Clearly and Simply http://www.clearlyandsimply.com/clearly_and_simply/2015/04/cre...

add a unique identifier for every polygon (ISO3 abbreviation followed by an underscore followed by the Polygon
ID). A simple string concatenation
add two columns to transfer Latitude and Longitude into positive values in points on a worksheet. The formulas
are very simple: basic arithmetical operations based on maximum and minimum values of Latitude and
Longitude and three parameters (scale, top and left offset)

Now on to the VBA code.

The heart of the solution is the Shapes.AddPolyline Method. Calling this method with an array of coordinate pairs
that specifies the polyline drawing's vertices creates a freeform shape of the closed polygon. This method makes the
implementation a walk in the park:

The sub to create freeform shapes simply loops through the entire data table, transfers the X and Y points of each
polygon to an array and calls the .AddPolyline method with this array to insert the freeform shape.

The VBA project is open, the code is commented and very slim (132 lines). I think I do not have to go into the details
here. If you are interested, download the workbook (download link at the end of the post) and have a look. If you
have any questions, please feel free to leave me a comment.

How to use the Tool


The workbook (download link see below) consists of only two worksheets: one with the polygon data and a few
simple calculations and an output sheet for the freeform shapes.

On the data sheet, the first 6 columns of the table contain the polygon data. The next 3 columns create a unique ID
for each polygon and transfer Latitude and Longitude into valid positions (points) on the output sheet using simple
formulas (see section above):

The VBA procedures can be started from an extra tab on the ribbon (POLYGONS 2 SHAPES):

6 of 11 3/20/17, 11:42 PM
Create Excel Freeform Shapes from Polygons - Clearly and Simply http://www.clearlyandsimply.com/clearly_and_simply/2015/04/cre...

A click on one of the first three buttons on this tab starts the VBA routines:

Transfer the polygon data into freeform shapes on the output sheet
Delete all shapes on the output sheet
Create groups of shapes for each country (if the country has more than one polygon)

Clicking on the last icon is simply sending you to this blog. You see, I am desperately looking for options to increase
the traffic here.

If you want to use the tool for your own polygons, you simply have to clear the first 6 columns of the table on the data
worksheet, copy in your own data, resize the table and finally click on the first icon of the POLYGONS 2 SHAPES tab.
Thats it. A few seconds later you should have the freeform shapes on the output sheet.

The Disadvantage: Performance


The code isnt optimized for performance and it takes some time to transfer the polygon data into freeform shapes.
The polygons of Europe (without Russia and Greenland), for instance, consist of more than 55,000 rows. Creating
476 freeform shapes of 52 countries takes between 5 and 6 seconds on my machine. Not really fast, but sufficient for
my purposes.

The Download Link


Here is the Excel workbook with the polygon data of Europe (without Russia and Greenland) for free download:

Download Create Shapes from Polygons (zipped Excel 2010-2013 workbook, 4.4 MB)

Stay tuned.

Robert at 07:00 PM in Microsoft Excel, Tools | Permalink


17
| | |

Comments

CristianS said...
How about using Qgis for making polygons from points?

Reply Sunday, April 19, 2015 at 10:15 AM

Robert said in reply to CristianS...


Cristian,

I am not claiming Excel would be the best tool to do it. However, I need the routine as a part of a another Excel
project I am working on. Using another tool was not an option.

Reply Sunday, April 19, 2015 at 10:24 AM

CristianS said...
Ok. Qgis it's open source. The idea was just to transform the polygons in the way you want, prior to paste in Excel.
It's a way to easy learn the gis stuff.

Reply Sunday, April 19, 2015 at 10:37 AM

7 of 11 3/20/17, 11:42 PM
Create Excel Freeform Shapes from Polygons - Clearly and Simply http://www.clearlyandsimply.com/clearly_and_simply/2015/04/cre...

Robert said in reply to CristianS...


Cristian,

you are right, if you need a one-off solution to create freeform shapes from polygon data, QGIS is probably the easier
way.

What I needed was a VBA routine in a stand-alone Excel workbook doing this again and again on user input. That's
why I created the workbook published above.

Reply Sunday, April 19, 2015 at 10:55 AM

Stefan Sandauer said...


Hi Robert, as i did all the work for all the districts and cities of Austria by hand, - with a Little VBA help... Your
solution is awesome.

To give a little hint for the difference between Wiener and Frankfurter (as i am coming from Vienna): the only
difference is the method in making the sausages - the ingredients differ a little bit - and the behavior in Austria in
naming things. The original founder (...) was a butcher from Frankfurt, coming to Vienna. Here, he added beef
(veal)to the sausages. Some years later, the sausages where vernacularly called Frankfurter...
cheers ,
Stefan

Reply Wednesday, April 22, 2015 at 01:16 PM

Mike said...
Hello Robert,

As someone who has followed your blog for a while, I would like to thank you for sharing this information.

It is very creative and insightful. I have learned a great deal from it.

Thanks,
Mike

Reply Sunday, April 26, 2015 at 12:15 AM

CristianS said...
I finally understood why you used this approach. Very interesting!
Have you try to export from Qgis a form in your format?
I tried for myself, but, somehow, it didn't work. My image looks more like your Germany scatter plot than what I
expected.
I've exported the geometry with MMQgis plugin and used in your file. But something I've mised.

Thank you for your example (all your examples, actually). A true inspiration!

Reply Wednesday, November 25, 2015 at 09:11 PM

8 of 11 3/20/17, 11:42 PM
Create Excel Freeform Shapes from Polygons - Clearly and Simply http://www.clearlyandsimply.com/clearly_and_simply/2015/04/cre...

Robert said in reply to CristianS...


Cristian,

I never used Qgis, so I unfortunately I cant help you here. I am sorry.

Reply Wednesday, November 25, 2015 at 09:31 PM

CristianS said in reply to Robert...


Thanks, anyway!

Reply Wednesday, November 25, 2015 at 10:11 PM

PereR said...
It's great script. It's only a pity it doesn't work. I need some hint what's I'm doing wrong - just copied my data from
shp file and filled table in the same manner as in example and code works to the end but there are no polygons.
Maybe Excel 2016 is not compatible? I'd like to know, because I need this polygons.

Reply Friday, February 03, 2017 at 02:57 AM

PereR said...
Ok. I've found my error, that was wrong projection x, y - bad scale coordinates. Cheers!

Reply Friday, February 03, 2017 at 03:22 AM

Flavio Henrique da Silva said...


hi, Robert Clearlyandsimply.com,

please make new Tutorial or explain more about the problem of plot the coordinates with lines and broke the series
to make this issue.

i am want this code and this approach to create this Scatter Charts with lines and create Freeform Shapes with split
series, no problem the limit of Excel.

you are Greate.


please.

Reply Saturday, March 11, 2017 at 07:25 PM

Robert said in reply to Flavio Henrique da Silva...


Flavio,

I am not sure I understand your question correctly, but if you have polygon data that form more than one shape and
want to plot it on an XY scatter chart, you need more than one data series in your XY scatter chart. Simply add one

9 of 11 3/20/17, 11:42 PM
Create Excel Freeform Shapes from Polygons - Clearly and Simply http://www.clearlyandsimply.com/clearly_and_simply/2015/04/cre...

data series per polygon data series which form one consistent shape and Excel will do the rest.

If you want to create freeform shapes from your polygon data, you can directly use the workbook I provided for
download. No changes necessary.

Reply Saturday, March 11, 2017 at 07:50 PM

Flavio Henrique da Silva said...


Hi Robert,

thank you very mutch,


but i am not understand.

I need solution about wrong Zig-Zag Lines to Geramany Map using 2 ways: VBA Plot Shapes and Plot Chart.

if this workbook file it is the solution, i need copy of the problem (xml file, chart of wrong lines) to i understand.

i need the original xml file with original coordinates, the wrong chart with wrong lines zig-zag points, and how insert
the "blank lines" into series to make the correct chart.

i want make the "way of plot chart " and "vba way of plot shapes" of coordinates to solution the problem of wrong
zig-zag lines, i want import the xml and plot them.

an vba routine to delete wrong zig-zag lines on chart is other way i want.

please more sample file and instructions, i have worked hard in this problem.

please copy of your xml files of World Map and Germany map and workbook of GErmany charts (and wrong
Germany chart).

thank you.
Flavio Henrique.

Reply Wednesday, March 15, 2017 at 03:43 PM

Robert said in reply to Flavio Henrique da Silva...


Flavio,

I already sent you an email with another workbook.

Reply Wednesday, March 15, 2017 at 03:52 PM

Flavio Henrique da Silva said...


Thank you very mutch Robert!
awesome!

i looking now to original xml files of this coordinates.

cheers!

Reply Wednesday, March 15, 2017 at 09:58 PM


Comment below or sign in with Typepad Facebook Twitter Google+ and more...

10 of 11 3/20/17, 11:42 PM
Create Excel Freeform Shapes from Polygons - Clearly and Simply http://www.clearlyandsimply.com/clearly_and_simply/2015/04/cre...

(You can use HTML tags like <b> <i> and <ul> to style your text. URLs automatically
linked.)

Email address is not displayed with comment.

Name

Email Address

Web Site URL

Post Preview

Clearly and Simply


Powered by TypePad

11 of 11 3/20/17, 11:42 PM

Você também pode gostar