def __init__(self, **kwargs): kwargs["color"] = C_COLOR Polygon.__init__(self, *POINTS[[0, 1, 2]], edge_colors=[B_COLOR, C_COLOR, A_COLOR], **kwargs) nudge = 0.2 target = POINTS[0] + nudge * (UP + RIGHT) for direction in UP, RIGHT: self.add_line(POINTS[0] + nudge * direction, target, color=WHITE)
def test_intersection(): corners = [ Point(0.0, 0.0, 0.0), Point(1.0, 0.0, 0.0), Point(1.0, 1.0, 0.0), Point(0.0, 1.0, 0.0) ] polygon = Polygon(corners, Point(0.5, 0.5, 0.0)) a = Point(0.5, 0.5, 1.0) b = Point(0.5, 0.5, -1.0) intersection = polygon.plane().intersection(a, b) assert intersection == Point(0.5, 0.5, 0.0)
def setUp(self): self.polygon = Polygon( [Point((0.0, 0.0)), Point((2.0, 0.0)), Point((2.0, 2.0)), Point((0.0, 2.0))]) self.polygon2 = Polygon( [Point((0.0, 0.0)), Point((4.0, 0.0)), Point((4.0, 4.0)), Point((0., 4.0))])
def jitter_polygon(polygon, d, method = "curve"): polygon = Polygon(polygon) segments = polygon.disjoin(120) segments_jittered = [] for segment in segments: segment_jittered = jitter_line(segment, d) segments_jittered.append(segment_jittered) return segments_jittered
def test_intersection(): plane = Plane(0.0, 0.0, 1.0, -0.9) a = Point(0.5, 0.5, -0.9) b = Point(0.0, 0.0, 0.0) assert plane.intersection(a, b) == Point(-0.5, -0.5, 0.9) points = [Point(0.5, 0.0, 0.0), Point(0.5, 1.0, 0.0), Point(0.5, 1.0, 1.0), Point(0.5, 0.1, 1.0)] polygon = Polygon(points, Point(0.5, 0.5, 0.5)) source = Point(0.5, 0.5, 0.5) mirror = source.mirror_with(Plane.from_normal_and_point(Vector(1.0, 0.0, 0.0), Point(1.0, 0.5, 0.5))) intersection = polygon.plane().intersection(mirror, Point(1.0, 0.0, 0.0)) assert intersection == Point(0.5, -0.5, -0.5)
class TestPolygon(unittest.TestCase): def setUp(self): self.polygon = Polygon( [Point((0.0, 0.0)), Point((2.0, 0.0)), Point((2.0, 2.0)), Point((0.0, 2.0))]) self.polygon2 = Polygon( [Point((0.0, 0.0)), Point((4.0, 0.0)), Point((4.0, 4.0)), Point((0., 4.0))]) def test_point_is_inside(self): self.assertTrue(self.polygon.point_is_inside(Point((1.0, 1.0)))) self.assertFalse(self.polygon.point_is_inside(Point((-1.0, -1.0)))) self.assertFalse(self.polygon2.point_is_inside(Point((-1.0, 2.0)))) def test_intersections(self): segment = Segment(Point((-1.0, 1.0)), Point((3.0, 1.0))) intersections = self.polygon.intersections(segment) self.assertEqual(2, len(intersections)) def test_area(self): self.assertAlmostEqual(4.0, self.polygon.area(), 5) self.assertAlmostEqual(16., self.polygon2.area(), 5) def test_transform(self): transform = Transform() transform.translate([1.,1.,1.]) self.polygon.transform(transform) self.assertTrue(np.allclose(np.matrix('1.,3.,3.,1.; 1. 1. 3. 3. ; 1. 1. 1. 1. '), self.polygon.matrix_of_vertices)) def test_clipping_when_no_intersection(self): clipper = Polygon([Point((5., 0.)), Point((6., 0.)), Point((5.5, 1.))]) clipped = self.polygon.clip(clipper) self.assertAlmostEqual(0., clipped.area())
def test_from_normal_and_point(): corners1 = [ Point(0.0, 0.0, 0.0), Point(1.0, 0.0, 0.0), Point(1.0, 1.0, 0.0), Point(0.0, 1.0, 0.0) ] center1 = Point(0.5, 0.5, 0.0) polygon1 = Polygon(corners1, center1) plane1 = Plane.from_normal_and_point(polygon1.plane().normal(), center1) assert polygon1.plane() == plane1 # tests the reduced normal form coefficients. corners2 = [ Point(0.0, 0.0, 0.0), Point(0.0, 1.0, 0.0), Point(0.0, 1.0, 1.0), Point(0.0, 0.0, 1.0) ] center2 = Point(0.0, 0.5, 0.5) polygon2 = Polygon(corners2, center2) plane2 = Plane.from_normal_and_point(polygon2.plane().normal(), center2) assert polygon2.plane() == plane2 corners4 = [ Point(1.0, 0.0, 0.0), Point(1.0, 1.0, 0.0), Point(1.0, 1.0, 1.0), Point(1.0, 0.0, 1.0) ] center4 = Point(1.0, 0.5, 0.5) polygon4 = Polygon(corners4, center4) plane4 = Plane.from_normal_and_point(polygon4.plane().normal(), center4) assert polygon4.plane() == plane4
from geometry import Polygon ap = argparse.ArgumentParser() ap.add_argument('file', nargs='+', type=str) ap.add_argument('--table', type=str, default='polygons') args = ap.parse_args() polygons = dict() parser = PolyfileParser() for file_name in args.file: if not os.path.isfile(file_name): print("Usage: {0} <files>".format(sys.argv[0])) name, ext = os.path.splitext(os.path.basename(file_name)) try: pr = parser.parse(io.open(file_name, 'r').read()) pl = Polygon(pr[1]['1']) polygons[name] = pl.to_wkt() except Exception as e: print("Could not process {0} because {1}".format(file_name, e), file=sys.stderr) quit(1) values = ','.join("('{0}', ST_SetSRID(ST_GeomFromText('{1}'), 4326))".format(n, p) for (n, p) in polygons.items()) print(''' BEGIN; DROP TABLE IF EXISTS {0}; CREATE TABLE {0} ( name varchar(64) primary key, polygon geometry(polygon, 4326) );
def __init__(self, data=None, projection=None, geometry=None, geometry_type=None, name=None, keywords=None, style_info=None, sublayer=None): """Initialise object with either geometry or filename NOTE: Doc strings in constructor are not harvested and exposed in online documentation. Hence the details are specified in the class docstring. """ # Invoke common layer constructor Layer.__init__(self, name=name, projection=projection, keywords=keywords, style_info=style_info, sublayer=sublayer) # Input checks if data is None and geometry is None: # Instantiate empty object self.geometry_type = None self.extent = [0, 0, 0, 0] return if isinstance(data, basestring): self.read_from_file(data) # check QGIS_IS_AVAILABLE to avoid QgsVectorLayer undefined error elif QGIS_IS_AVAILABLE and isinstance(data, QgsVectorLayer): self.read_from_qgis_native(data) else: # Assume that data is provided as sequences provided as # arguments to the Vector constructor # with extra keyword arguments supplying metadata msg = 'Geometry must be specified' verify(geometry is not None, msg) msg = 'Geometry must be a sequence' verify(is_sequence(geometry), msg) if len(geometry) > 0 and isinstance(geometry[0], Polygon): self.geometry_type = ogr.wkbPolygon self.geometry = geometry else: self.geometry_type = get_geometry_type(geometry, geometry_type) if self.is_polygon_data: # Convert to objects if input is a list of simple arrays self.geometry = [Polygon(outer_ring=x) for x in geometry] else: # Convert to list if input is an array if isinstance(geometry, numpy.ndarray): self.geometry = geometry.tolist() else: self.geometry = geometry if data is None: # Generate default attribute as OGR will do that anyway # when writing data = [] for i in range(len(geometry)): data.append({'ID': i}) # Check data self.data = data if data is not None: msg = 'Data must be a sequence' verify(is_sequence(data), msg) msg = ('The number of entries in geometry (%s) and data (%s)' 'must be the same' % (len(geometry), len(data))) verify(len(geometry) == len(data), msg) # Establish extent if len(geometry) == 0: # Degenerate layer self.extent = [0, 0, 0, 0] return # Compute bounding box for each geometry type minx = miny = sys.maxint maxx = maxy = -minx if self.is_point_data: A = numpy.array(self.get_geometry()) minx = min(A[:, 0]) maxx = max(A[:, 0]) miny = min(A[:, 1]) maxy = max(A[:, 1]) elif self.is_line_data: for g in self.get_geometry(): A = numpy.array(g) minx = min(minx, min(A[:, 0])) maxx = max(maxx, max(A[:, 0])) miny = min(miny, min(A[:, 1])) maxy = max(maxy, max(A[:, 1])) elif self.is_polygon_data: # Do outer ring only for g in self.get_geometry(as_geometry_objects=False): A = numpy.array(g) minx = min(minx, min(A[:, 0])) maxx = max(maxx, max(A[:, 0])) miny = min(miny, min(A[:, 1])) maxy = max(maxy, max(A[:, 1])) self.extent = [minx, maxx, miny, maxy]
def test_from_normal_and_point(): corners1 = [ Point(0.0, 0.0, 0.0), Point(1.0, 0.0, 0.0), Point(1.0, 1.0, 0.0), Point(0.0, 1.0, 0.0) ] center1 = Point(0.5, 0.5, 0.0) polygon1 = Polygon(corners1, center1) plane1 = Plane.from_normal_and_point(polygon1.plane().normal(), center1) assert polygon1.plane( ) == plane1 # tests the reduced normal form coefficients. corners2 = [ Point(0.0, 0.0, 0.0), Point(0.0, 1.0, 0.0), Point(0.0, 1.0, 1.0), Point(0.0, 0.0, 1.0) ] center2 = Point(0.0, 0.5, 0.5) polygon2 = Polygon(corners2, center2) plane2 = Plane.from_normal_and_point(polygon2.plane().normal(), center2) assert polygon2.plane() == plane2 corners4 = [ Point(1.0, 0.0, 0.0), Point(1.0, 1.0, 0.0), Point(1.0, 1.0, 1.0), Point(1.0, 0.0, 1.0) ] center4 = Point(1.0, 0.5, 0.5) polygon4 = Polygon(corners4, center4) plane4 = Plane.from_normal_and_point(polygon4.plane().normal(), center4) assert polygon4.plane() == plane4
def c_square(**kwargs): return Polygon(*POINTS[[1, 2, 7, 8]], color=C_COLOR, **kwargs)
def b_square(**kwargs): return Polygon(*POINTS[[1, 0, 5, 6]], color=B_COLOR, **kwargs)
def a_square(**kwargs): return Polygon(*POINTS[[0, 2, 4, 3]], color=A_COLOR, **kwargs)