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.

4

2 回答 2

1

我们需要有关文件格式的更多信息,例如如何识别带有数字的行,这些行上有多少个数字等,但本质上是您打开文件并逐行处理它。

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.

有关如何从文件中收集数据的详细信息需要有关数据/文件格式的更多信息,但通常对于包含数字的行,您可以使用split()对其进行解析,然后使用像这样的列表理解将部分转换为浮点值:

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

现在n是一个float值列表。

n
[5.5, 6.09, 8.0, 12.2]

您可以使用它进行数学运算并打印出来(尽管这里将结果作为字符串分配给变量):

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

旁白:使用with打开文件的好处是当你完成或遇到异常时它们会被关闭。

于 2012-06-21T14:57:55.693 回答
0

我们需要更多信息来帮助您:

  • 请给出定义多边形的文件的实际部分,而不是伪代码。

  • 有没有一种明显的方法来识别什么是和不是所需的多数据?我们如何知道我们正在寻找哪个文件块?鉴于此,打开文件(如 Levon 所示)并获取数据应该很容易。

  • 一旦我们得到多边形数据,找到两个多边形的联合有点复杂,除非它们有连续的重合顶点 - 请参阅如何组合复杂多边形?. 将其委托给像Shapely这样的几何库可能更容易。

于 2012-06-21T21:07:57.600 回答