]>
gitweb.michael.orlitzky.com - dead/census-tools.git/blob - wkt_n-gon
947b03aaaaa54eae4bf234e1f59b0420e773470c
4 Output the Well-Known-Text[1] representation of an n-gon, centered at
7 The parameter n is passed as a required command-line option. The
8 radius of the n-gon is an optional argument.
10 [1] http://en.wikipedia.org/wiki/Well-known_text
16 from optparse
import OptionParser
19 # Basically, add '../src' to our path.
20 # Needed for the imports that follow.
21 site
.addsitedir(os
.path
.dirname(os
.path
.abspath(sys
.argv
[0])) + '/../src')
28 usage
= '%prog [options] <number of sides>'
29 fmtr
= CLI
.VerbatimHelpFormatter()
30 parser
= OptionParser(usage
, formatter
=fmtr
)
32 # Use this module's docstring as the description.
33 parser
.description
= __doc__
36 parser
.add_option('-r',
39 help="The radius of the n-gon. Defaults to 1.0.",
43 (options
, args
) = parser
.parse_args()
45 if (len(args
) < 1) or (not StringUtils
.is_integer(args
[0])):
46 print "\nERROR: You must supply the number of sides, n.\n"
48 print '' # Print a newline.
49 raise SystemExit(ExitCodes
.NOT_ENOUGH_ARGS
)
51 # The conversion of n to int is safe, since it passed is_integer
56 # Each new coordinate is going to be separated from the previous by
57 # angle_delta radians.
58 angle_delta
= 2*math
.pi
/ float(n
)
60 # The set of coordinates, as two-tuples. We already know the first
61 # one--it's just cos/sin of 0.
64 for coord_idx
in range(1, n
):
65 angle
= coord_idx
* angle_delta
66 x
= r
* math
.cos(angle
)
67 y
= r
* math
.sin(angle
)
70 # A well-known-text polygon must be closed, i.e. the last coordinate
71 # must equal the first. So, we're missing a coordinate, but the
72 # Polygon class will handle it for us.
73 p
= Geometry
.Polygon(coords
)