Escolar Documentos
Profissional Documentos
Cultura Documentos
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.
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?
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.
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:
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.
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...
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:
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)
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.
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.
Download Create Shapes from Polygons (zipped Excel 2010-2013 workbook, 4.4 MB)
Stay tuned.
Comments
CristianS said...
How about using Qgis for making polygons from points?
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.
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.
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...
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.
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
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
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!
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...
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.
PereR said...
Ok. I've found my error, that was wrong projection x, y - bad scale coordinates. Cheers!
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.
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.
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.
cheers!
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.)
Name
Email Address
Post Preview
11 of 11 3/20/17, 11:42 PM