Auto-suggest helps you quickly narrow down your search results by suggesting possible matches as you type.

Exit

- Home
- After Effects
- Discussions
- Calculating an area of a map in square kilometers/...

- Calculating an area of a map in square kilometers/...

0

Calculating an area of a map in square kilometers/miles using expressions?

/t5/after-effects-discussions/calculating-an-area-of-a-map-in-square-kilometers-miles-using-expressions/td-p/14914223
Oct 13, 2024
Oct 13, 2024

Copy link to clipboard

Copied

Hi,

I'm making battle maps where layer shapes move across the map into bigger or smaller shapes using vertice paths, as the frontline progresses. I've been asked if it was possible to include a dynamic measure of the shape area in square kilometers/miles. So that I can show exact changes in square kilometers based on the shape/frontline changes. I just couldn't get ChatGPT to help me put this together, and I am far from able to do something like this on my own.

Is this something that is at all possible to do using expressions and then linking it to any text layer to show such values?

I've attached a sample. So the shape is the red area, which hence changes it's shape through time.

I would also be willing to pay someone to help me set this up. If this is at all an option.

Thank you,

*[PII removed by Moderator]*

TOPICS

Expressions
,
How to
,
Scripting

Community guidelines

Be kind and respectful, give credit to the original source of content, and search for duplicates before posting.
Learn more

20
Replies
20

/t5/after-effects-discussions/calculating-an-area-of-a-map-in-square-kilometers-miles-using-expressions/m-p/14914441#M257087
Oct 14, 2024
Oct 14, 2024

Copy link to clipboard

Copied

Yes, it's possible, but not easy. Anything from working with a grid-based approach to recursive triangular decomposition. Since you need to hold a lot of temporary data any expression is probably going to be slow as molasses in winter and a script would be more suitable.

Mylenium

Community guidelines

Be kind and respectful, give credit to the original source of content, and search for duplicates before posting.
Learn more

kresimirk39695749
AUTHOR

Explorer
,

/t5/after-effects-discussions/calculating-an-area-of-a-map-in-square-kilometers-miles-using-expressions/m-p/14914488#M257090
Oct 14, 2024
Oct 14, 2024

Copy link to clipboard

Copied

Would it be possible to talk to you via an email or Discord?

Community guidelines

Be kind and respectful, give credit to the original source of content, and search for duplicates before posting.
Learn more

/t5/after-effects-discussions/calculating-an-area-of-a-map-in-square-kilometers-miles-using-expressions/m-p/14915242#M257101
Oct 14, 2024
Oct 14, 2024

Copy link to clipboard

Copied

Nope. Not a gun for hire, at least not for that kind of work. In my hayday I might have tackled this as a challenge to explore the math and algorithms and even done it for free, but these days I just don't have the time and energy.

Mylenium

Community guidelines

kresimirk39695749
AUTHOR

Explorer
,

/t5/after-effects-discussions/calculating-an-area-of-a-map-in-square-kilometers-miles-using-expressions/m-p/14916620#M257124
Oct 14, 2024
Oct 14, 2024

Copy link to clipboard

Copied

Appreciate your honesty, thanks in any case.

Community guidelines

/t5/after-effects-discussions/calculating-an-area-of-a-map-in-square-kilometers-miles-using-expressions/m-p/14916654#M257125
Oct 14, 2024
Oct 14, 2024

Copy link to clipboard

Copied

The math required to calculate the area of an irregular shape can be very complicated. I don't know of of way to calculate the number of pixels that are a certain color or use an alpha channel.

The only possible option that I can think of would be to calculate the length of the vector path. I'm not even sure that is possible.

I think you are out of luck, looking for a simple expression solution to the problem.

Community guidelines

/t5/after-effects-discussions/calculating-an-area-of-a-map-in-square-kilometers-miles-using-expressions/m-p/14916866#M257130
Oct 14, 2024
Oct 14, 2024

Copy link to clipboard

Copied

I think I have a solution that will get you a decent estimate--not perfect, but very close.

To use this method, you will have to blur the shape, so you will need to create a linked copy of your shape layer (or at least its alpha channel), whether that is by linking shapes with expressions or using an effect like Set Matte on a solid. I tested this on a shape layer but either method should work.

Set your project to 32-bit color, make sure your shape is 100 opacity and everything else on the layer is 0% opacity, then apply two fast box blurs to the layer with *Iterations* set to *1* and *Repeat Edge Pixels* set to *ON.* One of these blurs must be set to *Vertical* with a Radius equal to the pixel height of the layer, and the other blur must be *Horizontal* with Radius equal to the layer width. The result will be a transparent solid color across the layer with an opacity roughly equal to the average opacity of all the layer's pixels. Now you can then use a sampleImage expression to find the opacity in the center of the layer, and multiply this value by the number of pixels in the comp to get the number of pixels that were 100% opacity before you applied the blurs. The result will be (pretty close to) the area of the solid shape.

The expression would look something like this:

```
var layerToMeasure = thisComp.layer("Shape Layer 1"); //sub in your layer name/reference here
var totalLayerArea = layerToMeasure.width*layerToMeasure.height;
var samplePoint = [layerToMeasure.width/2, layerToMeasure.height/2];
var sampleRadius = [1,1];
var shapeArea = layerToMeasure.sampleImage(samplePoint, sampleRadius)[3]*totalLayerArea;
shapeArea
```

If you want this in units other than onscreen pixels, you'll have to do that math. Also, in case it's not obvious, since this is done through image processing it can only measure the part of the shape that is inside the bounds of the layer, so the shape can't be cropped. In the case of an actual shape layer, the bounds of the layer are the same as the bounds of the comp.

Community guidelines

/t5/after-effects-discussions/calculating-an-area-of-a-map-in-square-kilometers-miles-using-expressions/m-p/14919698#M257173
Oct 15, 2024
Oct 15, 2024

Copy link to clipboard

Copied

That's quite clever.

Community guidelines

/t5/after-effects-discussions/calculating-an-area-of-a-map-in-square-kilometers-miles-using-expressions/m-p/14919903#M257174
Oct 15, 2024
Oct 15, 2024

Copy link to clipboard

Copied

Thanks! If you like that, you might like this, which I posted last year: https://community.adobe.com/t5/after-effects-ideas/expressions-access-to-media-replacement-comps/idc...

It's a similar method for detecting the rectangular pixel bounding box of a raster image's alpha with only four sampleImage() calls per frame. Sample project and preset are downthread from the linked post.

Community guidelines

kresimirk39695749
AUTHOR

Explorer
,

/t5/after-effects-discussions/calculating-an-area-of-a-map-in-square-kilometers-miles-using-expressions/m-p/14920062#M257177
Oct 15, 2024
Oct 15, 2024

Copy link to clipboard

Copied

Hi Aaron,

Thank you a lot for your idea. Would you mind if I send you a direct message? I am a real newbie with After Effects. I've just barely learned how to use vertices for my mapping project, so I am just about completely lost with how to do that even with the instructions.

Thank you,

Community guidelines

/t5/after-effects-discussions/calculating-an-area-of-a-map-in-square-kilometers-miles-using-expressions/m-p/14921215#M257200
Oct 16, 2024
Oct 16, 2024

Copy link to clipboard

Copied

Community guidelines

kresimirk39695749
AUTHOR

Explorer
,

/t5/after-effects-discussions/calculating-an-area-of-a-map-in-square-kilometers-miles-using-expressions/m-p/14921856#M257212
Oct 16, 2024
Oct 16, 2024

Copy link to clipboard

Copied

Hi Aaron,

Thank you but unfortunately it says that project file is not compatible with the current latest AE version.

Gotcha on DMs, sure we can keep it here. I will ask just something that fits a DM more, if you don't mind.

Thanks

Community guidelines

/t5/after-effects-discussions/calculating-an-area-of-a-map-in-square-kilometers-miles-using-expressions/m-p/14921901#M257213
Oct 16, 2024
Oct 16, 2024

Copy link to clipboard

Copied

What version of AE are you using?

Community guidelines

kresimirk39695749
AUTHOR

Explorer
,

/t5/after-effects-discussions/calculating-an-area-of-a-map-in-square-kilometers-miles-using-expressions/m-p/14921985#M257216
Oct 16, 2024
Oct 16, 2024

Copy link to clipboard

Copied

It's 2.3.1.0. (Build 83)

Community guidelines

/t5/after-effects-discussions/calculating-an-area-of-a-map-in-square-kilometers-miles-using-expressions/m-p/14922558#M257222
Oct 16, 2024
Oct 16, 2024

Copy link to clipboard

Copied

Community guidelines

Explorer
,

LATEST
/t5/after-effects-discussions/calculating-an-area-of-a-map-in-square-kilometers-miles-using-expressions/m-p/14939993#M257598
Oct 24, 2024
Oct 24, 2024

Copy link to clipboard

Copied

This is amazing... thanks for the attachment Aaron!

Community guidelines

/t5/after-effects-discussions/calculating-an-area-of-a-map-in-square-kilometers-miles-using-expressions/m-p/14919922#M257175
Oct 15, 2024
Oct 15, 2024

Copy link to clipboard

Copied

I just had an idea that I'm going to try tomorrow. Here is the idea.

Select the path around one of the vector shapes and choose Create Nulls From Paths.jsx/Null follows Path from the Window Menu. This will give you a null that moves around the entire vector path in just a few frames. If you set the keyframes apart 50 keyframes and recorded the X and Y position value for each frame, you could generate a 50-sided shape around the shape. If you use the shape's center point (sourceRectAtTime() height and width of a single shape layer) as the first vertex of a triangle and the x/y position of the first two frames, you could calculate the area of that triangle. Do that for every pair of position values, and you should have a reasonably accurate calculation of the area of the convoluted shape.

Run the script again to create a second instance of a null tracing the path. Set the keyframes down the timeline, ending on the last frame of the path animation, and you should be able to calculate the ending area of the shape. Then, you should be able to set up an expression that converts the number of total pixels to square kilometers or miles if you know the number of pixels per mile of your map.

If the expressions don't turn out to be incredibly recursive, so they slow down the render time, this approach might work. All I should have to do is figure out a way for the x,y position of the null between two frames to be remembered, so the area of the first, then second, then third, and on to the rest of the triangles can be added, I've got it licked.

Community guidelines

kresimirk39695749
AUTHOR

Explorer
,

/t5/after-effects-discussions/calculating-an-area-of-a-map-in-square-kilometers-miles-using-expressions/m-p/14920080#M257178
Oct 15, 2024
Oct 15, 2024

Copy link to clipboard

Copied

Thanks,

Community guidelines

/t5/after-effects-discussions/calculating-an-area-of-a-map-in-square-kilometers-miles-using-expressions/m-p/14920216#M257180
Oct 16, 2024
Oct 16, 2024

Copy link to clipboard

Copied

If you don't use too many bezier curves, you can use this expression:

```
p = thisComp.layer("Shape Layer 1").content("Shape 1").content("Path 1").path;
pts = p.points();
numPoints = p.points().length;
area = 0;
for (var i = 0; i < numPoints; i++) {
p1 = pts[i];
p2 = pts[(i + 1) % numPoints];
area += p1[0] * p2[1] - p2[0] * p1[1];
}
Math.floor(Math.abs(area) / 2) + 'px²\n' + Math.floor(Math.abs(area) / 2 / 10000) + ' squares'
```

Community guidelines

kresimirk39695749
AUTHOR

Explorer
,

/t5/after-effects-discussions/calculating-an-area-of-a-map-in-square-kilometers-miles-using-expressions/m-p/14920432#M257186
Oct 16, 2024
Oct 16, 2024

Copy link to clipboard

Copied

Much appreciate your example. I've tried to test it, but you are dealing with a newbie, so I do believe I am making some newbie mistakes. I can't even get it to work within my project. It gives me the same error that I was getting from bugging the ChatGPT to solve this. So I must be making some blatant newbie mistakes. If you're able to help me further, would appreciate it a lot, if not still thanks a lot for your time!

Community guidelines

/t5/after-effects-discussions/calculating-an-area-of-a-map-in-square-kilometers-miles-using-expressions/m-p/14920497#M257187
Oct 16, 2024
Oct 16, 2024

Copy link to clipboard

Copied

In the path property you can only draw a path using createPath()

exemple:

`createPath([[-200,200],[200,200],[200,-200],[-200,-200]])`

You can use the expression on the Source Text property of a text layer to display the area

Community guidelines

Resources

Troubleshooting FAQs

After Effects Community Recap

Getting started with After Effects

Copyright © 2024 Adobe. All rights reserved.