awk scripting to select a rectangle frame

Hi everybody,
I have a csv file as below;

Latitude Longitude

38.9900 38.3000
38.3000 37.1300
37.0200 36.1100
37.6500 36.7600
38.8700 37.7500
38.0900 39.0000
38.7400 39.6700
39.0300 40.9700
38.0100 38.2400
37.0200 37.0000
37.5000 36.3000
38.2700 39.1600
38.2400 38.3200
38.2700 38.9900
38.5100 39.2500
38.3000 38.3000
38.9600 39.3200
39.0000 38.0000
37.6900 38.8200
38.0100 38.2400
37.5000 39.5000

I want to select lines in a rectangle frame whose corners are;

Latitude Longitude

37.0000 36.5000
37.5000 36.5000
39.5000 41.0000
39.0000 41.0000
37.0000 36.5000

Please help!
Thank you

Hello jeo_fb,

Sorry but this is NOT the way UNIX.com forums work. We do encourage people to add their efforts which they have put in order to solve their own problems, so please do add your efforts in your question and let us know then. Your question is also NOT having clear information about how you want to achieve your goal, so kindly do add the rules, conditions and logic by which you want to achieve your goal.

Thanks,
R. Singh

Hello again,

I made many efforts for sure my code is below;

awk '$1 >=37.0000 && $1 <= 37.5000 && $1 >=39.0000 && $1 <= 39.5000 && $1>=36.5000 && $1 <=41.0000 {print $0}' txtfile 

However it did not work correctly.

interesting puzzle!
Are you saying one has to "draw" an imaginary rectangle frame given the input coordinates?
I'm saying "imaginary", because the desired output coordinates are not in your input file.

Very interesting puzzle, but I'm not sure of the algorithm - it's definitely not what you tried.

1 Like

I was thinking the same thing; but refrained from commenting .....

But I had trouble seeing any tangible relationship between the "attempt" and the original post.

I think better we tag this thread amber again and ask the OP to perform a "real attempt" to solve his problem, and add the output or error messages in reply before offering more answers.

Hi again,

I want to select lines in an imaginary rectangle frame whose corners are;

 Latitude Longitude
37.0000 36.5000
37.5000 36.5000
39.5000 41.0000
39.0000 41.0000
37.0000 36.5000

For example I do not want to line including;
39.0000 38.0000
Because it is not in the rectangle.

I hope I could explain my problem.
Thank you.

Not being a native speaker, I think this is not a rectangle. It is a parallelogram or a rhombus / rhomboid.

This is a coordinate pair, a point in a plane. A line needs at least one more point.

Should a line dissect that geometric shape, or lie entirely within?

Pls tell us how to interpret your "coordinate list".

1 Like

sorry, I don't think I can help - not sure of the algorithm and you're not providing any hints.
Maybe other did better in their trigonometry in school.
Would be interesting to see the attempts/solutions.

Also...

Hi jeo_fb...

Assuming you are using Longitude and Latitude WRT Earth as a __sphere__ then it is not possible to create a rectangle as Euclidean geometry does not apply.

IF, however you just want the Longitude to change but have matching Latitudes and vice versa and those listing out then please confirm...

Bazza...

I pretty sure it is possible to take latitudes and longitudes and construct a 2D geometric shape (like a rectangle) if we know the distance from the center of the coordinate system (commonly called "the elevation" above mean sea level of the earth). Otherwise, land surveying would not be possible since surveying is based on longitude and latitude (coordinates) with the elevation (on the Earth this is the distance above MSL).

Latitude and longitude, along with distance from the center of the earth, form polar coordinates, as I fondly remember from "the good ole days" when I worked as a land surveyor (actually I had my own land surveying business for many years),

The problem I have with this question is that the OP is not providing his algorithm as vbe mentioned in post #8.

So, let's ask the OP to post his formulas (algorithms) he is using to do the calculations.

The short answer is "to select a rectangle frame from longitude and latitude" is that you need to know the distance from the center of the coordinate system for each coordinate. This is simply a problem in converting polar to rectangular coordinates, and back.

Of course, you know me, I have done this kind of calculation countless times (in my youth) and never did it with awk . I used to do these calculations (almost daily) on the Tandy TRS 80 which I programmed in Basic and also the Atari / Amiga, back in the "day".

  • an area solution ex.:
awk '
function triangle_area(x1, y1, x2, y2, x3, y3) {
#
# determine area of triangle with the 3 points given
#
area=((x1 * (y2 - y3) + x2 * (y3 - y1) + x3 * (y1 - y2)) / 2.0);
#
# return absolute value of area. area value should be positive. unless you are on drugs.
#
return area < 0 ? -area : area;
}
FILENAME ~ /rectangle/ {
if (! repeat_line[$0]++) {
#
# load points of rectangle in x, y arrays.
#
x[NR]=$1;
y[NR]=$2;
} else {
#
# repeat line found so rectangle is closed. that is nice.
# calculate area of rectangle by adding area of two triangles that make up rectangle
#
Rectangle_area=triangle_area(x[1], y[1], x[2], y[2], x[3], y[3]) + triangle_area(x[1], y[1], x[3], y[3], x[4], y[4]);
}
}
FILENAME ~ /positions/ {
#
# determine area of 4 triangles made up of latitude, longitude point and each side of rectangle
#
triangle1=triangle_area(x[1], y[1], $1, $2, x[2], y[2]);
triangle2=triangle_area(x[2], y[2], $1, $2, x[3], y[3]);
triangle3=triangle_area(x[3], y[3], $1, $2, x[4], y[4]);
triangle4=triangle_area(x[1], y[1], $1, $2, x[4], y[4]);
#
# if rectangle area equals to total area of 4 triangles then point is inside rectangle. it is obvious if you think about it.
#
if (Rectangle_area==triangle1+triangle2+triangle3+triangle4) print $0;
}
' rectangle positions

note, example posted uses format of input/output files shown (not csv, as stated).