• Global community
    • Language:
      • Deutsch
      • English
      • Español
      • Français
      • Português
  • 日本語コミュニティ
    Dedicated community for Japanese speakers
  • 한국 커뮤니티
    Dedicated community for Korean speakers
Exit
0

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

Explorer ,
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.

kresimirk39695749_0-1728882786425.png

 

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

Views

682

Translate

Translate

Report

Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
LEGEND ,
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

Votes

Translate

Translate

Report

Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Explorer ,
Oct 14, 2024 Oct 14, 2024

Copy link to clipboard

Copied

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

Votes

Translate

Translate

Report

Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
LEGEND ,
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

Votes

Translate

Translate

Report

Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Explorer ,
Oct 14, 2024 Oct 14, 2024

Copy link to clipboard

Copied

Appreciate your honesty, thanks in any case.

Votes

Translate

Translate

Report

Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Community Expert ,
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. 

 

 

Votes

Translate

Translate

Report

Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Enthusiast ,
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.

 

 

Votes

Translate

Translate

Report

Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Community Expert ,
Oct 15, 2024 Oct 15, 2024

Copy link to clipboard

Copied

That's quite clever.

Votes

Translate

Translate

Report

Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Enthusiast ,
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.

Votes

Translate

Translate

Report

Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Explorer ,
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,

Votes

Translate

Translate

Report

Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Enthusiast ,
Oct 16, 2024 Oct 16, 2024

Copy link to clipboard

Copied

Here's an example project you can look at. You can absolutely DM me, but if you have questions that you don't mind being public it would probably help others to keep it here on the forum.

Votes

Translate

Translate

Report

Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Explorer ,
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

Votes

Translate

Translate

Report

Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Enthusiast ,
Oct 16, 2024 Oct 16, 2024

Copy link to clipboard

Copied

What version of AE are you using?

Votes

Translate

Translate

Report

Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Explorer ,
Oct 16, 2024 Oct 16, 2024

Copy link to clipboard

Copied

It's 2.3.1.0. (Build 83)

Votes

Translate

Translate

Report

Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Enthusiast ,
Oct 16, 2024 Oct 16, 2024

Copy link to clipboard

Copied

Do you mean 23.1.0? If so, here is a verson for AE 23.x.

Votes

Translate

Translate

Report

Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Explorer ,
Oct 24, 2024 Oct 24, 2024

Copy link to clipboard

Copied

LATEST

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

Votes

Translate

Translate

Report

Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Community Expert ,
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. 

Votes

Translate

Translate

Report

Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Explorer ,
Oct 15, 2024 Oct 15, 2024

Copy link to clipboard

Copied

Thank you for the idea Rick. Unfortunately, as said to Aaron, your suggestions are beyond my current abilities. I am just a mapper that knows how to move vertices around the project. It does seem to me like you guys would not be interested in paid assistance to help me solve this task, as just a standalone project, which I would then reproduce in future videos?

Thanks,

Votes

Translate

Translate

Report

Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Advocate ,
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'

screenshot.png

 

Votes

Translate

Translate

Report

Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Explorer ,
Oct 16, 2024 Oct 16, 2024

Copy link to clipboard

Copied

Hi Airweb,

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!

 

kresimirk39695749_1-1729071760270.png

 

kresimirk39695749_0-1729071626484.png

 

Votes

Translate

Translate

Report

Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Advocate ,
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

screenshot.png

 

Votes

Translate

Translate

Report

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