Navigation Menu

Skip to content

Instantly share code, notes, and snippets.

@heynicejacket
Forked from bryanbuchanan/GetShapeArea.jsx
Last active August 9, 2021 08:41
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save heynicejacket/bc329428c6198613f90e481a9e568439 to your computer and use it in GitHub Desktop.
Save heynicejacket/bc329428c6198613f90e481a9e568439 to your computer and use it in GitHub Desktop.
Script to find the area of map shapes in Adobe Illustrator, scaled to a hex grid for worldbuilding.
/*
Save this file with a jsx extension and access from the File > Scripts menu
Currently, this assumes your map utilizes a 24 mile hex. This is necessary to have a scale
factor by which to to multiply the area of your map shape to get the area in mi/km.
If your map uses a different scale, select a single hex and run this script. Replace the inches
value with the value in hexAreaInchesMap, select your shape, and run again to get sq mi / km.
By default, this rounds up the mi/km. Km is +/- 1 due to rounding. Will fix later.
* original AI script: https://gist.github.com/bryanbuchanan/11387501
* format JS number with commas: https://stackoverflow.com/a/2901298
*/
var decimalPlaces = 10; // rounding to 10 places because math
var hexAreaMileActual = 498.8307291667; // sq mi area of a 24 mile hex
var hexAreaInchesMap = 0.0523425444; // sq in area of your map's hex
if (app.documents.length > 0) {
if (app.activeDocument.selection.length < 1) {
alert('Select a path');
} else if (app.activeDocument.selection[0].area) {
// Individual Items
var objects = app.activeDocument.selection;
} else if (app.activeDocument.selection[0].pathItems) {
// Group/Compound Shape
var objects = app.activeDocument.selection[0].pathItems;
} else {
alert('Please select a path or group.');
}
// Collect info
var totalArea = 0;
for (var i=0; i<objects.length; i++) {
if (objects[i].area) {
var totalArea = totalArea + objects[i].area;
}
}
// Conversions
var ppi = 72;
var areaIn = totalArea / ppi / ppi;
if (areaIn < 0) var areaIn = -areaIn;
// Scaled map area calculations
var scaleFactor = hexAreaMileActual / hexAreaInchesMap
var geographicAreaMi = scaleFactor * areaIn.toFixed(decimalPlaces)
var geographicAreaKm = geographicAreaMi * 2.58999
var geographicAreaFmtMi = Math.ceil(geographicAreaMi).toString().replace(/\B(?=(\d{3})+(?!\d))/g, ',')
var geographicAreaFmtKm = Math.ceil(geographicAreaKm).toString().replace(/\B(?=(\d{3})+(?!\d))/g, ',')
// Display
alert('\
' + i + ' shape(s) selected \n\
File Area: \
' + areaIn.toFixed(decimalPlaces) + ' sq in \n\
Map Area: \
' + geographicAreaFmtMi + ' sq mi \
' + geographicAreaFmtKm + ' sq km'
);
}
@heynicejacket
Copy link
Author

For a full description of the purpose of this fork, see: https://embers.nicejacket.cc/blog/2019/05/28/measuring-area-in-illustrator-to-scale/

The purpose here is to be able to calculate the in-world area of a border or other shape representing a political, linguistic, etc., region, scaled to a hex grid of a fixed size. In the above code, the common 24-mile hex is used. To use another size hex, run this code on a selected hexagon of your map's size, and replace the inches squared value of hexAreaInchesMap, and calculate the actual area of that size hex with:

A = (3 × √3 × s²) / 2

...where A is the area of the hex, and s is the length of one side of the hex.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment