def basic_check(): """ Example for checks on a basic polygon. Returns ------- None """ # create our polygon object with coordinates bounded by square [0, 1]x[-1, 1] coords = generate_random_polygon() poly = Polygon(coordinates=[ coords, ]) ############################# # perform random samples check samples = 10000 pts = 2.2 * numpy.random.rand(samples, 2) - 1.1 start = time.time() in_poly_condition = poly.contain_coordinates(pts[:, 0], pts[:, 1]) lapsed = time.time() - start print('basic poly: lapsed = {}, lapsed/point = {}'.format( lapsed, lapsed / samples)) ########################### # perform grid check grid_samples = 1001 x_grid = numpy.linspace(-1.1, 1.1, grid_samples) y_grid = numpy.linspace(-1.1, 1.1, grid_samples) start = time.time() in_poly_condition2 = poly.grid_contained(x_grid[:-1], y_grid[:-1]) lapsed = time.time() - start print('basic poly: lapsed = {}, lapsed/point = {}'.format( lapsed, lapsed / ((grid_samples - 1)**2))) ############################# # visualize results fig, axs = pyplot.subplots(nrows=2, ncols=1, sharex='col', sharey='col') fig.suptitle('Basic polygon example') axs[0].scatter(pts[in_poly_condition, 0], pts[in_poly_condition, 1], color='r', marker='.', s=16) axs[0].scatter(pts[~in_poly_condition, 0], pts[~in_poly_condition, 1], color='b', marker='.', s=16) axs[0].plot(coords[:, 0], coords[:, 1], 'k-') y2d, x2d = numpy.meshgrid(y_grid, x_grid, indexing='xy') axs[1].pcolormesh(x2d, y2d, in_poly_condition2, cmap='jet') axs[1].plot(coords[:, 0], coords[:, 1], 'k-', lw=2, zorder=99) pyplot.show()
def callback_annotate_handle_right_mouse_click(self, event): self.annotate_panel.image_canvas_panel.canvas.callback_handle_right_mouse_click(event) if self.annotate_panel.image_canvas_panel.canvas.variables.current_tool == ToolConstants.DRAW_POLYGON_BY_CLICKING: current_canvas_shape_id = self.annotate_panel.image_canvas_panel.canvas.variables.current_shape_id image_coords = self.annotate_panel.image_canvas_panel.canvas.get_shape_image_coords(current_canvas_shape_id) geometry_coords = np.asarray([x for x in zip(image_coords[0::2], image_coords[1::2])]) polygon = Polygon(coordinates=[geometry_coords]) annotation = Annotation() annotation.geometry = polygon self.variables.canvas_geom_ids_to_annotations_id_dict[str(current_canvas_shape_id)] = annotation
def from_dict(cls, input_dict): typ = input_dict.get('type', None) if typ is None: return cls() elif typ == 'Point': return cls(Point=PointType.from_dict(input_dict)) elif typ == 'LineString': return cls(Line=LineStringType.from_dict(input_dict)) elif typ == 'LinearRing': return cls(LinearRing=LinearRingType.from_dict(input_dict)) elif typ == 'Polygon': return cls(Polygon=PolygonType.from_dict(input_dict)) elif typ == 'MultiPoint': return cls(MultiPoint=MultiPointType.from_dict(input_dict)) elif typ == 'MultiLineString': return cls(MultiLineString=MultiLineStringType.from_dict(input_dict)) elif typ == 'MultiPolygon': return cls(MultiPolygon=MultiPolygonType.from_dict(input_dict)) else: logging.error('AnnotationObjectType got unsupported input dictionary {}. ' 'Returning None.'.format(input_dict)) return None
def compound_poly_check(): """ Example for compound polygon with a hole in it. Returns ------- None """ # create our polygon object with coordinates bounded by square [0, 1]x[-1, 1] outer_coords = numpy.array([ [-1, 0], [-0.5, -1], [0.5, -1], [1, 0], [0.5, 1], [-0.5, 1], [-1, 0], ], dtype='float64') inner_coords = 0.5 * generate_random_polygon() poly = Polygon(coordinates=[outer_coords, inner_coords]) ############################# # perform random samples check samples = 10000 pts = 2.2 * numpy.random.rand(samples, 2) - 1.1 start = time.time() in_poly_condition = poly.contain_coordinates(pts[:, 0], pts[:, 1]) lapsed = time.time() - start print('compound poly: lapsed = {}, lapsed/point = {}'.format( lapsed, lapsed / samples)) ########################### # perform grid check grid_samples = 1001 x_grid = numpy.linspace(-1.1, 1.1, grid_samples) y_grid = numpy.linspace(-1.1, 1.1, grid_samples) start = time.time() in_poly_condition2 = poly.grid_contained(x_grid[:-1], y_grid[:-1]) lapsed = time.time() - start print('compound poly: lapsed = {}, lapsed/point = {}'.format( lapsed, lapsed / ((grid_samples - 1)**2))) ############################# # visualize results fig, axs = pyplot.subplots(nrows=2, ncols=1, sharex='col', sharey='col') fig.suptitle('Compound polygon example') axs[0].scatter(pts[in_poly_condition, 0], pts[in_poly_condition, 1], color='r', marker='.', s=16) axs[0].scatter(pts[~in_poly_condition, 0], pts[~in_poly_condition, 1], color='b', marker='.', s=16) axs[0].plot(outer_coords[:, 0], outer_coords[:, 1], 'k-') axs[0].plot(inner_coords[:, 0], inner_coords[:, 1], 'k-') y2d, x2d = numpy.meshgrid(y_grid, x_grid, indexing='xy') axs[1].pcolormesh(x2d, y2d, in_poly_condition2, cmap='jet') axs[1].plot(outer_coords[:, 0], outer_coords[:, 1], 'k-') axs[1].plot(inner_coords[:, 0], inner_coords[:, 1], 'k-') pyplot.show()