Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations strongm on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

MATCH AND ADD XY

Status
Not open for further replies.

mrr

Technical User
May 3, 2001
67
US
I have a data file that contains area polygons and sub polygons that I write to a different format. My script works except where there is a sub polygon that contains only 4 xy pairs, I need to add the first or last xy of that group to the beg or end in order for another program to handle a required 5 xy pair polygon.
I also have the problem in that the input data file could have 40,41,etc xy pairs as "SUB 40","SUB 41" not just "SUB 4"


Any suggestions, please.
Thanks,

INPUT:
ADD 5
XY 2233831 349494.08
XY 2233700 349492.86
XY 2233700 349364.25
XY 2233831 349365.46
XY 2233831 349494.08
SUB 5
XY 2233831 349494.08
XY 2233700 349492.86
XY 2233700 349364.25
XY 2233831 349365.46
XY 2233831 349494.08
SUB 6
XY 2226772 339594.57
XY 2226604 339678.39
XY 2226591 339641.26
XY 2226475 339250.52
XY 2226782 339255.27
XY 2226772 339594.57
SUB 4
XY 2226772 339594.57
XY 2226774 339553.71
XY 2226782 339255.27
XY 2226772 339594.57

GAWK SCRIPT:
BEGIN { OFS=","
}
{ fd_1 ="A"
fd_2 = "B"

if ($0 ~ /^H/) next
if ($0 ~ /^ *$/) next
if ($0 ~ /^$/) next
if (match($0, "EOF")) next

if (match($0, "ADD")) {
count=0;type ="AREA";group_id="0";next
}
if (match($0, "SUB ")) {
count=0;type ="SUB";group_id++;next
}
{
count++
x = $2
y = $3
print fd_1,fd_2,type,group_id,x,y,count
}
}
 
Try this

BEGIN { OFS=","
}
{ fd_1 ="A"
fd_2 = "B"

if ($0 ~ /^H/) next
if ($0 ~ /^ *$/) next
if ($0 ~ /^$/) next
if (match($0, "EOF")) next

if (match($0, "ADD")) {
count=0;type ="AREA";group_id="0";next
}
if (match($0, "SUB ")) {
count=0;type ="SUB";group_id++;subt=$2;if (int(subt/10)==4)subt=4;next
}
{
count++
x = $2
y = $3
print fd_1,fd_2,type,group_id,x,y,count
if (subt == 4) {
if (count == 1) {
x0 = x
y0 = y
}
if (count == 4) {
print fd_1,fd_2,type,group_id,x0,y0,count+1
}
}
}
}


CaKiwi
 
Thanks CaKiwi.
The output is slightly off on the count and format when I have input such as after the 4th point:

SUB 40
XY 1000000 2000000
XY 2226774 339553.71
XY 2226782 339255.27
XY 2226772 339594.57
XY 2226772 339594.57
XY 2226774 339553.71
XY 2226782 339255.27
XY 2226772 339594.57
XY 2226772 339594.57
XY 2226774 339553.71
XY 2226782 339255.27
XY 2226772 339594.57
XY 2226774 339553.71
XY 2226782 339255.27
XY 2226772 339594.57
XY 2226772 339594.57
XY 2226774 339553.71
XY 2226782 339255.27
XY 2226772 339594.57
XY 2226772 339594.57
XY 2226774 339553.71
XY 2226782 339255.27
XY 2226772 339594.57
 
Thanks CaKiwi,

I changed your code of:
if (match($0, "SUB ")) {
count=0;type ="SUB";group_id++;subt=$2;if (int(subt/10)==4)subt=4;next
}

to:
if (match($0, "SUB ")) {
count=0;type ="SUB";group_id++;subt=$2;
if (subt==4);next
}

and it fixed the problem.

Thanks again.
Much appreciated!
 
I'm glad it's working for you, but the line

if (subt==4);next

has exactly the same effect as just

next

The if statement checks if subt is 4 and executes everything up to the semicolon, i.e. does nothing. Take out the "if (subt==4);" part and see what happens.

The statement

if (int(subt/10)==4)subt=4

that I had in my original solution was to handle the case of sub being in the 40's and having only 4 points but it looks like that never happens

Anyway, glad it is working for you

CaKiwi
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top