0

I try to convert an postscript file such that individual, adjacent polygons (drawn by gnuplot's filledcurve mode) are converted to one single polygon. See also this related question:

gnuplot artifacts

To to so, I'm facing the following problem.

the polygons have such a structure in the ps-file

some statements
A1 A2 A3 A4 A5
B1 B2 B3 B4 B5
some statements

A1,A2.. etc are numbers. The processed file should then look like this (arbitrary example here)

some statements
A1    A2
B1    B2
B1+A2 B5-A4
B1+A2 B5-A5
B1    B2
A1    A2
some statements

Here, e.g. B1+A2 should be the result of the arithmetic operation, i.e. a float. How can one something like this in python? Somehow I need to scan the file, math certain lines and save them (i.e. the individual fields as in awk)?

EDIT:

a section from the original postscript file looks like this

5918 4703 N
399 0 V
0 70 V
-399 0 V
0 -70 V
Z stroke
LT0
630 399 N 0 -3498 586 131 0 3490 h
1216 522 N 0 -3525 1171 204 0 3498 h
2387 699 N 0 -3557 1171 134 0 3525 h
3558 801 N 0 -3587 1171 55 0 3557 h
4729 826 N 0 -3613 1171 -20 0 3587 h
5900 780 N 0 -3624 585 -43 0 3613 h
% End plot #1
1.000 UL
LTb
0.500 UL
LTa
0.13 0.13 0.13 C 630 280 M
5855 0 V
stroke

where N and h stand for

/N {newpath moveto} bind def
/h {rlineto rlineto rlineto gsave closepath fill grestore} bind def

in this file we have 6 polygons, they are defined between the line "LT0" and "% end plot #1". The lines where polygons are defined are easy to match with regex

/^[0-9,-]+\ [0-9,-]+\ N\ [0-9]\ [0-9,-]+\ [0-9,-]+\ [0-9,-]+\ [0-9,-]+\ [0-9,-]+ h/

I'd like to convert them into something like

newpath
 630 399 moveto
1216 522 lineto
2387 699 lineto
3558 801 lineto
4729 826 lineto
5900 780 lineto
..   ..   ..
..   ..   ..

The new polygon has therefore more lines of code as I'd like to define the absolute coordinates point by point. replacing single lines does not work.

Community
  • 1
  • 1
Raphael Roth
  • 26,751
  • 15
  • 88
  • 145

2 Answers2

1

We need much more information about the file format, such as how to identify lines with numbers, how many numbers on such lines, etc, but essentially you open the file and process it line by line.

with open('data.txt') as inf, open('out.txt', 'w') as outf:
   for line in inf:
      if # line with numbers
         # then code similar to what's shown below
         # generating a new value for variable line
         line = .... # see below

      outf.write(line+'\n') # write out "line" to output file, either the
                            # original non-number line, or the "modified"
                            # line with the parsed/math results.

Details for how to collect the data from your file needs more information about the data/file format, but in general for a line that contains numbers you can parse it using split() and then convert the parts into float values using list comprehension like this:

line = '5.5 6.09 8.0 12.2'
n = [float(i) for i in line.split()]

now n is a list of float values.

n
[5.5, 6.09, 8.0, 12.2]

with which you can do math and print out (though here the results are assigned as a string to a variable):

line = "%.2f %.2f" %((n[0]+n[1]), (n[2]+n[3]))
11.59 20.20

Aside: The advantage of using with to open files is that they get closed when you are done or an exception is encountered.

Levon
  • 138,105
  • 33
  • 200
  • 191
0

We need a bit more information to help you:

  • Please give the actual section of file where the polygons are defined, not pseudocode.

  • Is there an obvious way to recognize what is and isn't the desired poly-data? How can we tell which chunk of file we are looking for? Given that, it should be pretty easy to open the file (as shown by Levon) and grab the data.

  • Once we get the poly data, finding the union of two polygons is a bit involved unless they have consecutive coincident vertices - see How do I combine complex polygons? . It might be easier to delegate this to a geometry library like Shapely.

Community
  • 1
  • 1
Hugh Bothwell
  • 55,315
  • 8
  • 84
  • 99