Exemplo n.º 1
0
def tight_envelope(geom):
    """
    return the geometry which builds an envelope around `geom`
    """
    if hasattr(geom, 'geoms'):
        g0 = max((sub.envelope.area, sub) for sub in geom.geoms)[1]
        g00 = asPolygon(g0.exterior)
    elif isinstance(geom, Polygon):
        g00 = asPolygon(geom.exterior)
    return g00
Exemplo n.º 2
0
 def test_polygon(self):
     a = numpy.array([[1.0, 1.0, 2.0, 2.0, 1.0], [3.0, 4.0, 4.0, 3.0, 3.0]])
     t = a.T
     s = geometry.asPolygon(t)
     self.failUnlessEqual(
         list(s.exterior.coords),
         [(1.0, 3.0), (1.0, 4.0), (2.0, 4.0), (2.0, 3.0), (1.0, 3.0)] )
Exemplo n.º 3
0
Arquivo: map.py Projeto: csdms/pymt
    def __init__(self, *args, **kwargs):
        super(UnstructuredMap, self).__init__(*args, **kwargs)

        self._point = {}
        last_offset = 0
        for (cell_id, offset) in enumerate(self._offset):
            cell = self._connectivity[last_offset:offset]
            last_offset = offset

            for point_id in cell:
                try:
                    self._point[point_id].append(cell_id)
                except KeyError:
                    self._point[point_id] = [cell_id]

        (point_x, point_y) = (self.get_x(), self.get_y())
        self._polys = []
        last_offset = 0
        for (cell_id, offset) in enumerate(self._offset):
            cell = self._connectivity[last_offset:offset]
            last_offset = offset

            (x, y) = (point_x.take(cell), point_y.take(cell))
            if len(x) > 2:
                self._polys.append(asPolygon(zip(x, y)))
            elif len(x) == 2:
                self._polys.append(asLineString(zip(x, y)))
            else:
                self._polys.append(asPoint(zip(x, y)))
Exemplo n.º 4
0
def poly_relocate(polylist):
    """
    Relocates all polygons to be centered on 0,0
    Input: A list of polygons as arrays of points (2L), the output of 
    polygon_rotator found in img_geometry.
    Output: A list of similar shape to the input
    requires Shapely.geometry as sg
             numpy as np
    """
    
    cent = sg.asPolygon(polylist[0]).centroid.wkt #find polygon centroid
    cent_x = np.float64(cent[7:np.uint8((len(cent)+6)/2)]) #pull x as float
    cent_y = np.float64(cent[np.uint8((  len(cent)+7)/2):
                                         len(cent)-1]) #pull y as float

    ## Subtract centroid value from all points in all rotations of the input polygon
    rot_std = []
    for pt in polylist:
        
        rot_stdx = []
        rot_stdy = []
        for ptx, pty in pt:
            rot_stdx.append(ptx-cent_x)
            rot_stdy.append(pty-cent_y)
        rot_std.append(np.asarray(zip(rot_stdx, rot_stdy)))
        #avg_x 
    return(rot_std)
Exemplo n.º 5
0
 def get_mask(l, p):
     p = _normalize_point(p)
     total_diam = (r+wallwidth)*2
     perp0 = perpendicular_at(l, p, total_diam)
     p2 = line_extrapolate_point(l, p, (r+wallwidth)*1.01)
     perp1 = perpendicular_at(l, p2, total_diam)
     mask = asPolygon(
         linering(perp0.coords[0], perp0.coords[1], perp1.coords[1], perp1.coords[0])
     ).convex_hull
     return mask
Exemplo n.º 6
0
 def setUp(self):
     # insert an initial feature in the datadabase
     feature = Polygon()
     feature.name = "foo"
     feature.geometry = asPolygon(((0.0, 0.0), (0.0, 1.0), (1.0, 1.0), (1.0, 0.0), (0.0, 0.0)),
                                  [((0.5, 0.5), (0.5, 0.7), (0.7, 0.7), (0.7, 0.5), (0.5, 0.5))])
     Session.add(feature)
     Session.commit()
     self.fid = feature.id
     self.fids = [feature.id]
Exemplo n.º 7
0
 def shape_buffer(self, shape=None, size=1., res=1):
     """
     Function that returns a new polygon of the larger buffered points. 
     Can import polygon into function if desired. Default is 
     self.shape_poly()
     """
     if shape is None:
         self.polygon = self.shape_poly()
     
     return asPolygon(self.polygon.buffer(size, resolution=res)\
     .exterior)
Exemplo n.º 8
0
def _polygon_wkt_to_xyz(polygon, as_wkt=False):
    '''Converts a polygon in 'POLYGON' wkt format to xyz. If as_wkt is 
    True, will return the xyz output back as a wkt format, otherwise
    it will return an array'''
    polygon = wkt.loads(polygon)
    polygon = polygon.boundary.xy
    number_nodes = len(polygon[0])
    xyz =  spherical_to_cartesian(polygon[0], polygon[1], 
        np.zeros(number_nodes, dtype=float))
    if as_wkt:
        xyz = asPolygon(xyz[:, :-1]).wkt
    return xyz
def build_polygon(shape_data):
    point_list = [None] * len(shape_data)
    counter = 0

    for point in shape_data:
        lat_lng = [float(coord) for coord in point.split(",")]
        point_list.insert(counter, lat_lng)
        counter += 1

    point_list = [x for x in point_list if x is not None]

    p = asPolygon(point_list)

    return p
Exemplo n.º 10
0
def minkowski_sum_2(poly1, poly2):
    ''' Slower minkowski sum algorithm (convex hull operation is a lot slower), but 
    uses blazing fast numpy sums and doesn't require particular polygon orientation'''
    
    assert poly1.is_valid, validation.explain_validity(poly1)
    assert poly2.is_valid, validation.explain_validity(poly2)

    ext1 = np.array(poly1.boundary)[:-1,:]
    ext2 = np.array(poly2.boundary)[:-1,:]
    n_ext2 = ext2.shape[0]    
    n_ext1 = ext1.shape[0]
    
    ext_sum = np.repeat(ext1, n_ext2, axis=0) + np.tile(ext2, (n_ext1, 1))    
    
    return geom.asPolygon(ext_sum).convex_hull
#compute normal orientation of the segment
normal = (p[1] - p[0]) * np.array([1, -1])
normal[0], normal[1], = normal[1], normal[0]
#exchanging x and y, wihtout copy
normal = normal / np.linalg.norm(normal)
print(normal)

#creating the upper point and down point for first and second (hopefully last) point in segment
p1u = p[0] + normal * r1
p1d = p[0] - normal * r1

p2u = p[1] + normal * r2
p2d = p[1] - normal * r2

output_line = (p1u, p2u, p2d, p1d, p1u)

ogeom = asPolygon(output_line)

print(ogeom)
pp1.pprint(str(geom))

#outputing for postgis : @NOTE : if inside postgres, hex =False, if outside, hex=True
output = wkb.dumps(ogeom, hex=True)

print(output)

# ##emulation of postgres output
# #return   None   ;
# 	#return { "center": center, "radius": radius ,  "t1": t1, "t2":t2}
#==============================================================================
Exemplo n.º 12
0
def get_rate(path, mianliao_h):
    # 加载零件数据
    boxes = get_boxes_data(path)
    # print(PolyArea(boxes[0]['x_list'], boxes[0]['y_list']))
    # print(boxes[0]['space'])
    # 按宽度对boxes进行排序
    boxes = sorted(boxes, key=itemgetter('w'), reverse=True)
    # 零件个数
    boxes_len = len(boxes)
    # 初始化
    move_list = []
    move_xy = np.array([0.0, 0.0])
    flag = 0
    for i in range(boxes_len):
        move_list.append(move_xy)
    cur_point = np.array([0.0, 0.0])
    init_point = np.array([0.0, 0.0])
    # 排列零件
    for i in range(boxes_len):
        if boxes[i]['isuse']:
            continue
        cur_point = init_point
        available_h = mianliao_h
        last_up = 0.0  # 用于计算两零件之间的高度间距(存储上一次y轴坐标)
        last_box = {
            'x_list': [cur_point[0], 20000.0],
            'y_list': [cur_point[1], cur_point[1]],
            'id': 0
        }
        for j in range(i, boxes_len):
            move = boxes[j]['bl_point'] - cur_point
            # rec_width = boxes[j]['w']
            # rec_height = boxes[j]['h']
            # x_min = cur_point[0]
            # y_min = cur_point[1]
            # x_list = np.array([x_min, x_min, x_min + rec_width, x_min + rec_width, x_min])  # 第j个矩形的所有轮廓点的x坐标集合
            # y_list = np.array([y_min, y_min + rec_height, y_min + rec_height, y_min, y_min])
            # message = {'cur_point': cur_point, 'move': move, 'rec_x': x_list, 'rec_y': y_list}
            if boxes[j]['h'] <= available_h and boxes[j]['isuse'] is False:
                # plt.plot(message['rec_x'], message['rec_y'], linewidth=0.5)  # 这一行是用来显示矩形边缘的
                boxes[j]['isuse'] = True
                move_list[j] = move

                # 存储移动后的多边形顶点坐标
                for k in range(len(boxes[j]['x_list'])):
                    boxes[j]['x_list'][
                        k] = boxes[j]['x_list'][k] - move_list[j][0]
                    boxes[j]['y_list'][
                        k] = boxes[j]['y_list'][k] - move_list[j][1]
                # 绘制多边形
                plt.plot(boxes[j]['x_list'], boxes[j]['y_list'], linewidth=0.3)

                # 计算gap
                gap_h = boxes[j]['y_left'][0] - move[1] - last_up
                last_up = boxes[j]['y_left'][1] - move[1]
                # 搜索放置在gap间的小零件
                for small in range(j + 1, boxes_len):
                    if boxes[small]['h'] <= gap_h \
                            and boxes[small]['h'] <= available_h \
                            and boxes[small]['isuse'] is False:
                        # 零件small的外接矩形的4个顶点和中心点
                        move_s = boxes[small]['bl_point'] - cur_point
                        tmp_x2 = boxes[small]['x1'] + boxes[small][
                            'w'] - move_s[0]
                        tmp_y2 = boxes[small]['y1'] + boxes[small][
                            'h'] - move_s[1]
                        tmp_x1 = boxes[small]['x1'] - move_s[0]
                        tmp_y1 = boxes[small]['y1'] - move_s[1]
                        tmp_mx = boxes[small][
                            'x1'] + boxes[small]['w'] / 2 - move_s[0]
                        tmp_my = boxes[small][
                            'y1'] + boxes[small]['h'] / 2 - move_s[1]
                        tmp_s = []  # 移动后的零件small所有顶点坐标
                        # 判断4个顶点和中心点是否在已排列零件内部
                        if inpolygon(tmp_x2, tmp_y2, boxes[j]['x_list'], boxes[j]['y_list'])[0] + 0 == 0 \
                                and inpolygon(tmp_x1, tmp_y2, boxes[j]['x_list'], boxes[j]['y_list'])[0] + 0 == 0 \
                                and inpolygon(tmp_x1, tmp_y1, boxes[j]['x_list'], boxes[j]['y_list'])[0] + 0 == 0 \
                                and inpolygon(tmp_x2, tmp_y1, boxes[j]['x_list'], boxes[j]['y_list'])[0] + 0 == 0 \
                                and inpolygon(tmp_mx, tmp_my, boxes[j]['x_list'], boxes[j]['y_list'])[0] + 0 == 0:
                            # 计算移动后的多边形顶点坐标
                            for k in range(len(boxes[small]['x_list'])):
                                tmp_s.append([
                                    boxes[small]['x_list'][k] - move_s[0],
                                    boxes[small]['y_list'][k] - move_s[1]
                                ])
                            tmp_pols = np.array(tmp_s)
                            # 判断small零件与已排放的boxes[j]是否重叠
                            tmp_polj = tranfer_(boxes[j]['x_list'],
                                                boxes[j]['y_list'])
                            pol_s = asPolygon(tmp_pols)
                            pol_j = asPolygon(tmp_polj)
                            if pol_s.disjoint(pol_j):
                                boxes[small]['isuse'] = True
                                move_list[small] = move_s
                                # 存储移动后的多边形顶点坐标
                                for k in range(len(boxes[small]['x_list'])):
                                    boxes[small]['x_list'][k] = boxes[small][
                                        'x_list'][k] - move_list[small][0]
                                    boxes[small]['y_list'][k] = boxes[small][
                                        'y_list'][k] - move_list[small][1]
                                # 绘制多边形
                                plt.plot(boxes[small]['x_list'],
                                         boxes[small]['y_list'],
                                         linewidth=0.3)
                                break

                available_h = available_h - boxes[j]['h']
                # 零件排列到布料顶端的情况
                if boxes[j]['h'] > available_h and flag == 0:
                    flag = 1  # 标记用于只需判断1次布料顶端能否排列下小零件(small)
                    # 计算gap
                    gap_h = mianliao_h - (boxes[j]['y_left'][1] - move[1])
                    # print(gap_h)
                    # 搜索放置在gap间的小零件
                    for small in range(j + 1, boxes_len):
                        if boxes[small]['h'] <= gap_h \
                                and boxes[small]['isuse'] is False:
                            # 零件small的外接矩形的4个顶点和中心点
                            move_s = boxes[small]['bl_point'] + np.array([
                                0.0, boxes[small]['h'] - 5
                            ]) - np.array([cur_point[0], mianliao_h])
                            tmp_x2 = boxes[small]['x1'] + boxes[small][
                                'w'] - move_s[0]
                            tmp_y2 = boxes[small]['y1'] + boxes[small][
                                'h'] - move_s[1]
                            tmp_x1 = boxes[small]['x1'] - move_s[0]
                            tmp_y1 = boxes[small]['y1'] - move_s[1]
                            tmp_mx = boxes[small][
                                'x1'] + boxes[small]['w'] / 2 - move_s[0]
                            tmp_my = boxes[small][
                                'y1'] + boxes[small]['h'] / 2 - move_s[1]
                            # 判断4个顶点和中心点是否在已排列零件内部
                            tmp_s = []
                            if inpolygon(tmp_x2, tmp_y2, boxes[j]['x_list'], boxes[j]['y_list'])[0] + 0 == 0 \
                                    and inpolygon(tmp_x1, tmp_y2, boxes[j]['x_list'], boxes[j]['y_list'])[0] + 0 == 0 \
                                    and inpolygon(tmp_x1, tmp_y1, boxes[j]['x_list'], boxes[j]['y_list'])[0] + 0 == 0 \
                                    and inpolygon(tmp_x2, tmp_y1, boxes[j]['x_list'], boxes[j]['y_list'])[0] + 0 == 0 \
                                    and inpolygon(tmp_mx, tmp_my, boxes[j]['x_list'], boxes[j]['y_list'])[0] + 0 == 0:
                                # 计算移动后的多边形顶点坐标
                                for k in range(len(boxes[small]['x_list'])):
                                    tmp_s.append([
                                        boxes[small]['x_list'][k] - move_s[0],
                                        boxes[small]['y_list'][k] - move_s[1]
                                    ])
                                tmp_pols = np.array(tmp_s)
                                # 判断small零件与已排放的boxes[j]是否重叠
                                tmp_polj = tranfer_(boxes[j]['x_list'],
                                                    boxes[j]['y_list'])
                                pol_s = asPolygon(tmp_pols)
                                pol_j = asPolygon(tmp_polj)
                                if pol_s.disjoint(pol_j):
                                    boxes[small]['isuse'] = True
                                    move_list[small] = move_s
                                    # 存储移动后的多边形顶点坐标
                                    for k in range(len(
                                            boxes[small]['x_list'])):
                                        boxes[small]['x_list'][
                                            k] = boxes[small]['x_list'][
                                                k] - move_list[small][0]
                                        boxes[small]['y_list'][
                                            k] = boxes[small]['y_list'][
                                                k] - move_list[small][1]
                                    # 绘制多边形
                                    plt.plot(boxes[small]['x_list'],
                                             boxes[small]['y_list'],
                                             linewidth=0.3)

                                    break
                    break
                last_box = boxes[j]
                cur_point = cur_point + np.array([0.0, boxes[j]['h']])
        init_point = init_point + np.array([boxes[i]['w'], 0.0])
        flag = 0
    # 计算布料利用率
    area_box = 0
    width = init_point[0]
    for box in boxes:
        area_box += PolyArea(box['x_list'], box['y_list'])
    rate = area_box / (width * mianliao_h)
    # 保存排样图片
    path_name = path.split("\\")[-1].split('.')[0]
    path_plot = path_name + '.png'
    plt.title(r'%s——%0.4f' % (path_name, rate))
    plt.savefig(path_plot, dpi=600)
    # plt.ylim((0, 1600))
    plt.show()

    # 保存csv文件
    path_csv = path_name + '.csv'
    headers = ['下料批次号', '零件号', '面料号', '零件外轮廓线坐标']
    values = []

    for box in boxes:
        values.append([
            box['batch'], box['id'], box['cloth'],
            str(
                tranfer_(box['x_list'].round(1),
                         box['y_list'].round(1)).tolist())
        ])
    csv_file = open(path_csv, "w", encoding='utf-8', newline='')
    # csv按行写入
    writer = csv.writer(csv_file)
    writer.writerow(headers)
    writer.writerows(values)
    csv_file.close()

    return rate
Exemplo n.º 13
0
        prop["valid"] = boundary.contains(geom)
        return geom, prop


@_layer_operation
def layer_contained(boundary: geo, **kwargs):
    """Analyse containment for all features within a single-layer vector file according to a Geometry
    and return a single GeoJSON file."""
    geom, prop = kwargs["geom"], kwargs["prop"]
    if isinstance(geom, geo.Polygon):
        prop["valid"] = boundary.contains(geom)
        return geom, prop


if __name__ == "__main__":
    source = Path("/home/tjs/Downloads/GIS_Data/hydro_s")
    vec = source.joinpath("hydro_s.shp")
    output_folder = source.joinpath("output")
    output_file = source.joinpath("output.json")

    bound = geo.asPolygon([(-71.5, 49), (-71.5, 54), (-63.5, 54), (-63.5, 49),
                           (-71.5, 49)])

    # feature_convex_hull(vector=vec, output=output_folder)
    feature_contained(boundary=bound, vector=vec, output=output_folder)
    print(feature_envelope.__doc__, "\n")
    print(inspect.signature(feature_envelope))
    layer_contained(boundary=bound, vector=vec, output=output_file)
    print(layer_envelope.__doc__, "\n")
    print(inspect.signature(layer_envelope))
Exemplo n.º 14
0
def _plausibilize_group(regionspolys, rogroup, mark_for_deletion,
                        mark_for_merging):
    LOG = getLogger('processor.RepairSegmentation')
    wait_for_deletion = list()
    reading_order = dict()
    regionrefs = list()
    ordered = False
    # the reading order does not have to include all regions
    # but it may include all types of regions!
    if isinstance(rogroup, (OrderedGroupType, OrderedGroupIndexedType)):
        regionrefs = (rogroup.get_RegionRefIndexed() +
                      rogroup.get_OrderedGroupIndexed() +
                      rogroup.get_UnorderedGroupIndexed())
        ordered = True
    if isinstance(rogroup, (UnorderedGroupType, UnorderedGroupIndexedType)):
        regionrefs = (rogroup.get_RegionRef() + rogroup.get_OrderedGroup() +
                      rogroup.get_UnorderedGroup())
    for elem in regionrefs:
        reading_order[elem.get_regionRef()] = elem
        if not isinstance(elem, (RegionRefType, RegionRefIndexedType)):
            # recursive reading order element (un/ordered group):
            _plausibilize_group(regionspolys, elem, mark_for_deletion,
                                mark_for_merging)
    for regionpoly in regionspolys:
        delete = regionpoly.region.id in mark_for_deletion
        merge = regionpoly.region.id in mark_for_merging
        if delete or merge:
            region = regionpoly.region
            poly = regionpoly.polygon
            if merge:
                # merge region with super region:
                superreg = mark_for_merging[region.id]
                # granularity will necessarily be lost here --
                # this is not for workflows/processors that already
                # provide good/correct segmentation and reading order
                # (in which case orientation, script and style detection
                #  can be expected as well), but rather as a postprocessor
                # for suboptimal segmentation (possibly before reading order
                # detection/correction); hence, all we now do here is
                # show warnings when granularity is lost; but there might
                # be good reasons to do more here when we have better processors
                # and use-cases in the future
                superpoly = Polygon(
                    polygon_from_points(superreg.get_Coords().points))
                superpoly = superpoly.union(poly)
                if superpoly.type == 'MultiPolygon':
                    superpoly = superpoly.convex_hull
                if superpoly.minimum_clearance < 1.0:
                    superpoly = asPolygon(np.round(superpoly.exterior.coords))
                superpoly = make_valid(superpoly)
                superpoly = superpoly.exterior.coords[:-1]  # keep open
                superreg.get_Coords().set_points(
                    points_from_polygon(superpoly))
                # FIXME should we merge/mix attributes and features?
                if region.get_orientation() != superreg.get_orientation():
                    LOG.warning(
                        'Merging region "{}" with orientation {} into "{}" with {}'
                        .format(region.id, region.get_orientation(),
                                superreg.id, superreg.get_orientation()))
                if region.get_type() != superreg.get_type():
                    LOG.warning(
                        'Merging region "{}" with type {} into "{}" with {}'.
                        format(region.id, region.get_type(), superreg.id,
                               superreg.get_type()))
                if region.get_primaryScript() != superreg.get_primaryScript():
                    LOG.warning(
                        'Merging region "{}" with primaryScript {} into "{}" with {}'
                        .format(region.id, region.get_primaryScript(),
                                superreg.id, superreg.get_primaryScript()))
                if region.get_primaryLanguage(
                ) != superreg.get_primaryLanguage():
                    LOG.warning(
                        'Merging region "{}" with primaryLanguage {} into "{}" with {}'
                        .format(region.id, region.get_primaryLanguage(),
                                superreg.id, superreg.get_primaryLanguage()))
                if region.get_TextStyle():
                    LOG.warning(
                        'Merging region "{}" with TextStyle {} into "{}" with {}'
                        .format(
                            region.id,
                            region.get_TextStyle(),  # FIXME needs repr...
                            superreg.id,
                            superreg.get_TextStyle()))  # ...to be informative
                if region.get_TextEquiv():
                    LOG.warning(
                        'Merging region "{}" with TextEquiv {} into "{}" with {}'
                        .format(
                            region.id,
                            region.get_TextEquiv(),  # FIXME needs repr...
                            superreg.id,
                            superreg.get_TextEquiv()))  # ...to be informative
            wait_for_deletion.append(region)
            if region.id in reading_order:
                regionref = reading_order[region.id]
                # TODO: re-assign regionref.continuation and regionref.type to other?
                # could be any of the 6 types above:
                regionrefs = rogroup.__getattribute__(
                    regionref.__class__.__name__.replace('Type', ''))
                # remove in-place
                regionrefs.remove(regionref)

    if ordered:
        # re-index the reading order!
        regionrefs.sort(key=RegionRefIndexedType.get_index)
        for i, regionref in enumerate(regionrefs):
            regionref.set_index(i)

    for region in wait_for_deletion:
        if region.parent_object_:
            # remove in-place
            region.parent_object_.get_TextRegion().remove(region)
Exemplo n.º 15
0
    def test_polygon(self):

        # Initialization
        # Linear rings won't usually be created by users, but by polygons
        coords = ((0.0, 0.0), (0.0, 1.0), (1.0, 1.0), (1.0, 0.0))
        ring = LinearRing(coords)
        self.assertEqual(len(ring.coords), 5)
        self.assertEqual(ring.coords[0], ring.coords[4])
        self.assertEqual(ring.coords[0], ring.coords[-1])
        self.assertTrue(ring.is_ring)

        # Coordinate modification
        ring.coords = ((0.0, 0.0), (0.0, 2.0), (2.0, 2.0), (2.0, 0.0))
        self.assertEqual(
            ring.__geo_interface__,
            {'type': 'LinearRing',
             'coordinates': ((0.0, 0.0), (0.0, 2.0), (2.0, 2.0), (2.0, 0.0),
                             (0.0, 0.0))})

        # Test ring adapter
        coords = [[0.0, 0.0], [0.0, 1.0], [1.0, 1.0], [1.0, 0.0]]
        ra = asLinearRing(coords)
        self.assertTrue(ra.wkt.upper().startswith('LINEARRING'))
        self.assertEqual(dump_coords(ra),
                         [(0.0, 0.0), (0.0, 1.0), (1.0, 1.0), (1.0, 0.0),
                          (0.0, 0.0)])
        coords[3] = [2.0, -1.0]
        self.assertEqual(dump_coords(ra),
                         [(0.0, 0.0), (0.0, 1.0), (1.0, 1.0), (2.0, -1.0),
                          (0.0, 0.0)])

        # Construct a polygon, exterior ring only
        polygon = Polygon(coords)
        self.assertEqual(len(polygon.exterior.coords), 5)

        # Ring Access
        self.assertIsInstance(polygon.exterior, LinearRing)
        ring = polygon.exterior
        self.assertEqual(len(ring.coords), 5)
        self.assertEqual(ring.coords[0], ring.coords[4])
        self.assertEqual(ring.coords[0], (0., 0.))
        self.assertTrue(ring.is_ring)
        self.assertEqual(len(polygon.interiors), 0)

        # Create a new polygon from WKB
        data = polygon.wkb
        polygon = None
        ring = None
        polygon = load_wkb(data)
        ring = polygon.exterior
        self.assertEqual(len(ring.coords), 5)
        self.assertEqual(ring.coords[0], ring.coords[4])
        self.assertEqual(ring.coords[0], (0., 0.))
        self.assertTrue(ring.is_ring)
        polygon = None

        # Interior rings (holes)
        polygon = Polygon(coords, [((0.25, 0.25), (0.25, 0.5),
                                    (0.5, 0.5), (0.5, 0.25))])
        self.assertEqual(len(polygon.exterior.coords), 5)
        self.assertEqual(len(polygon.interiors[0].coords), 5)
        with self.assertRaises(IndexError):  # index out of range
            polygon.interiors[1]

        # Test from another Polygon
        copy = Polygon(polygon)
        self.assertEqual(len(polygon.exterior.coords), 5)
        self.assertEqual(len(polygon.interiors[0].coords), 5)
        with self.assertRaises(IndexError):  # index out of range
            polygon.interiors[1]

        # Coordinate getters and setters raise exceptions
        self.assertRaises(NotImplementedError, polygon._get_coords)
        with self.assertRaises(NotImplementedError):
            polygon.coords

        # Geo interface
        self.assertEqual(
            polygon.__geo_interface__,
            {'type': 'Polygon',
             'coordinates': (((0.0, 0.0), (0.0, 1.0), (1.0, 1.0), (2.0, -1.0),
                             (0.0, 0.0)), ((0.25, 0.25), (0.25, 0.5),
                             (0.5, 0.5), (0.5, 0.25), (0.25, 0.25)))})

        # Adapter
        hole_coords = [((0.25, 0.25), (0.25, 0.5), (0.5, 0.5), (0.5, 0.25))]
        pa = asPolygon(coords, hole_coords)
        self.assertEqual(len(pa.exterior.coords), 5)
        self.assertEqual(len(pa.interiors), 1)
        self.assertEqual(len(pa.interiors[0].coords), 5)

        # Test Non-operability of Null rings
        r_null = LinearRing()
        self.assertEqual(r_null.wkt, 'GEOMETRYCOLLECTION EMPTY')
        self.assertEqual(r_null.length, 0.0)

        # Check that we can set coordinates of a null geometry
        r_null.coords = [(0, 0), (1, 1), (1, 0)]
        self.assertAlmostEqual(r_null.length, 3.414213562373095)

        # Error handling
        with self.assertRaises(ValueError):
            # A LinearRing must have at least 3 coordinate tuples
            Polygon([[1, 2], [2, 3]])
Exemplo n.º 16
0
def show_output(tf_vars, deb_oo, deb_jo, d_query, session, smooth_pairs,
                d_postfix, f_postfix, um):
    colors = stealth_colors
    p_deb = os.path.join(d_query, 'debug_isec' + d_postfix)
    if not os.path.exists(p_deb):
        os.makedirs(p_deb)
    #     os.system("rm %s/*.png" % p_deb)
    #     os.system("rm %s/*.svg" % p_deb)
    # else:

    # assert np.allclose(tf_vars.oo_mask_same.eval(),
    #                    tf_vars.oo_mask_same_2.eval())
    # assert np.allclose(tf_vars.oo_mask_cat.eval(),
    #                    tf_vars.oo_mask_cat_2.eval())
    # assert np.allclose(tf_vars.oo_mask_interacting_2,
    #                    tf_vars._oo_mask_interacting.eval())
    # assert np.isclose(tf_vars._oo_mask_interacting_sum_inv.eval(),
    #                   tf_vars.oo_mask_interacting_sum_inv_2.eval())

    obj_vxs_t = tf_vars.obj_2d_vertices_transformed
    o_obj_vxs_t, o_joints, o_smooth_pairs, distances, o_mgrid_vxs_t = \
        session.run([obj_vxs_t, deb_jo['joints'], smooth_pairs,
                     deb_jo['d'], tf_vars._obj_2d_mgrid_vertices_transformed])
    o_polys, o_poly_indices = session.run(
        [tf_vars.obj_2d_polys, tf_vars._obj_2d_poly_transform_indices])
    o_oo_mask, o_d_oo = session.run([deb_oo['oo_mask'], deb_oo['d_oo']])
    py_cat_ids = np.squeeze(tf_vars.cat_ids_polys.eval(), axis=0)
    lg.debug("distances: %s" % repr(distances.shape))
    lg.debug("obj_vxs_t: %s" % repr(o_obj_vxs_t.shape))
    lg.debug("joints: %s" % repr(o_joints.shape))

    mn1 = np.min(o_obj_vxs_t, axis=0)[[0, 2]]
    mx1 = np.max(o_obj_vxs_t, axis=0)[[0, 2]]
    mn2 = np.min(o_smooth_pairs, axis=0)
    mn2 = np.minimum(mn2[:3], mn2[3:])[[0, 2]]
    mx2 = np.max(o_smooth_pairs, axis=0)
    mx2 = np.maximum(mx2[:3], mx2[3:])[[0, 2]]
    mn = np.minimum(mn1, mn2)
    mx = np.maximum(mx1, mx2)
    assert o_joints.shape[0] // 5 == distances.shape[0]
    o_joints = o_joints.reshape(5, -1, 3)[0, ...]
    assert o_polys.shape[0] == distances.shape[1]

    fig = plt.figure()
    ax0 = fig.add_subplot(111, aspect='equal')
    # for pair in o_smooth_pairs:
    #     ax0.plot([pair[0], pair[3]], [pair[2], pair[5]], 'k--')

    # for id_pnt in range(1, o_joints.shape[0]):
    #     pnt0 = o_joints[id_pnt, :]
    o_joints_ordered = np.asarray([
        e[1]
        for e in sorted([(um.pids_2_scenes[pid].frame_id, o_joints[pid, ...])
                         for pid in range(o_joints.shape[0])],
                        key=lambda e: e[0])
    ])
    assert o_joints_ordered.ndim == 2 and o_joints_ordered.shape[1] == 3
    ax0.plot(o_joints_ordered[:, 0], o_joints_ordered[:, 2], 'kx--')

    for id_poly in range(distances.shape[1]):
        idx_t = o_poly_indices[id_poly, 0]
        color = tuple(c / 255. for c in colors[(idx_t + 1) % len(colors)])
        d_sum = 0.
        # poly = np.concatenate((
        #     o_obj_vxs_t[4*id_poly:4*(id_poly+1), :],
        #     o_obj_vxs_t[4*id_poly:4*id_poly+1, :]))

        # ax0.plot(o_polys[id_poly, :, 0], o_polys[id_poly, :, 2], color=color)
        shapely_poly = geom.asPolygon(o_polys[id_poly, :, [0, 2]].T)
        patch = PolygonPatch(shapely_poly,
                             facecolor=color,
                             edgecolor=color,
                             alpha=0.25)
        ax0.add_artist(patch)
        cat = next(cat for cat in CATEGORIES
                   if CATEGORIES[cat] == py_cat_ids[id_poly])
        xy = (shapely_poly.centroid.xy[0][0] - 0.1,
              shapely_poly.centroid.xy[1][0])
        ax0.annotate(cat, xy=xy, color=color, fontsize=6)
        for id_pnt in range(distances.shape[0]):
            d_ = distances[id_pnt, id_poly]
            if d_ < 0.:
                pnt = o_joints[id_pnt, :]
                ax0.scatter(pnt[0],
                            pnt[2],
                            s=(5 * (1 + d_))**2,
                            color=color,
                            zorder=5)
                d_sum += distances[id_pnt, id_poly]
                ax0.annotate("%.2f" % d_,
                             xy=(np.random.rand() * 0.1 + pnt[0] - 0.05,
                                 np.random.rand() * 0.1 + pnt[2] - 0.05),
                             fontsize=4)

        for id_pnt in range(o_d_oo.shape[0]):
            d_ = o_d_oo[id_pnt, id_poly]
            if d_ < 0.:
                pnt = o_mgrid_vxs_t[id_pnt, :]
                ax0.scatter(pnt[0],
                            pnt[2],
                            s=(7 * (1 + d_))**2,
                            edgecolor=color,
                            zorder=5,
                            color=(1., 1., 1., 0.))
                ax0.annotate("%.2f" % d_,
                             xy=(np.random.rand() * 0.1 + pnt[0] - 0.05,
                                 np.random.rand() * 0.1 + pnt[2] - 0.05),
                             fontsize=8,
                             color='r',
                             zorder=6)

    # fig.suptitle("d_sum: %s" % d_sum)
    p_out = os.path.join(p_deb, "op_%s.png" % f_postfix)
    plt.draw()
    ax0.set_xlim(mn[0] - 0.2, mx[0] + 0.2)
    ax0.set_ylim(mn[1] - 0.2, mx[1] + 0.2)
    # plt.show()

    try:
        fig.savefig(p_out, dpi=300)
        lg.debug("saved to %s" % p_out)
    except PermissionError as e:
        lg.error("Could not save %s" % e)

    plt.close(fig)
Exemplo n.º 17
0
def main():      
    gdal.AllRegister()
    path = auxil.select_directory('Input directory')
    if path:
        os.chdir(path)        
#  input image    
    infile = auxil.select_infile(title='Image file') 
    if infile:                   
        inDataset = gdal.Open(infile,GA_ReadOnly)
        cols = inDataset.RasterXSize
        rows = inDataset.RasterYSize    
        bands = inDataset.RasterCount
        projection = inDataset.GetProjection()
        geotransform = inDataset.GetGeoTransform()
        if geotransform is not None:
            gt = list(geotransform) 
        else:
            print 'No geotransform available'
            return       
        imsr = osr.SpatialReference()  
        imsr.ImportFromWkt(projection)      
    else:
        return  
    pos =  auxil.select_pos(bands)   
    if not pos:
        return
    N = len(pos) 
    rasterBands = [] 
    for b in pos:
        rasterBands.append(inDataset.GetRasterBand(b)) 
#  training algorithm
    trainalg = auxil.select_integer(1,msg='1:Maxlike,2:Backprop,3:Congrad,4:SVM') 
    if not trainalg:
        return           
#  training data (shapefile)      
    trnfile = auxil.select_infile(filt='.shp',title='Train shapefile')
    if trnfile:
        trnDriver = ogr.GetDriverByName('ESRI Shapefile')
        trnDatasource = trnDriver.Open(trnfile,0)
        trnLayer = trnDatasource.GetLayer() 
        trnsr = trnLayer.GetSpatialRef()             
    else:
        return     
    tstfile = auxil.select_outfile(filt='.tst', title='Test results file') 
    if not tstfile:
        print 'No test output'      
#  outfile
    outfile, outfmt = auxil.select_outfilefmt(title='Classification file')   
    if not outfile:
        return                   
    if trainalg in (2,3,4):
#      class probabilities file, hidden neurons
        probfile, probfmt = auxil.select_outfilefmt(title='Probabilities file')
    else:
        probfile = None     
    if trainalg in (2,3):    
        L = auxil.select_integer(8,'Number of hidden neurons')    
        if not L:
            return                  
#  coordinate transformation from training to image projection   
    ct= osr.CoordinateTransformation(trnsr,imsr) 
#  number of classes    
    K = 1
    feature = trnLayer.GetNextFeature() 
    while feature:
        classid = feature.GetField('CLASS_ID')
        if int(classid)>K:
            K = int(classid)
        feature = trnLayer.GetNextFeature() 
    trnLayer.ResetReading()    
    K += 1       
    print '========================='
    print 'supervised classification'
    print '========================='
    print time.asctime()    
    print 'image:    '+infile
    print 'training: '+trnfile  
    if trainalg == 1:
        print 'Maximum Likelihood'
    elif trainalg == 2:
        print 'Neural Net (Backprop)'
    elif trainalg ==3:
        print 'Neural Net (Congrad)'
    else:
        print 'Support Vector Machine'               
#  loop through the polygons    
    Gs = [] # train observations
    ls = [] # class labels
    classnames = '{unclassified'
    classids = set()
    print 'reading training data...'
    for i in range(trnLayer.GetFeatureCount()):
        feature = trnLayer.GetFeature(i)
        classid = str(feature.GetField('CLASS_ID'))
        classname  = feature.GetField('CLASS_NAME')
        if classid not in classids:
            classnames += ',   '+ classname
        classids = classids | set(classid)        
        l = [0 for i in range(K)]
        l[int(classid)] = 1.0
        polygon = feature.GetGeometryRef()
#      transform to same projection as image        
        polygon.Transform(ct)  
#      convert to a Shapely object            
        poly = shapely.wkt.loads(polygon.ExportToWkt())
#      transform the boundary to pixel coords in numpy        
        bdry = np.array(poly.boundary) 
        bdry[:,0] = bdry[:,0]-gt[0]
        bdry[:,1] = bdry[:,1]-gt[3]
        GT = np.mat([[gt[1],gt[2]],[gt[4],gt[5]]])
        bdry = bdry*np.linalg.inv(GT) 
#      polygon in pixel coords        
        polygon1 = asPolygon(bdry)
#      raster over the bounding rectangle        
        minx,miny,maxx,maxy = map(int,list(polygon1.bounds))  
        pts = [] 
        for i in range(minx,maxx+1):
            for j in range(miny,maxy+1): 
                pts.append((i,j))             
        multipt =  MultiPoint(pts)   
#      intersection as list              
        intersection = np.array(multipt.intersection(polygon1),dtype=np.int).tolist()
#      cut out the bounded image cube               
        cube = np.zeros((maxy-miny+1,maxx-minx+1,len(rasterBands)))
        k=0
        for band in rasterBands:
            cube[:,:,k] = band.ReadAsArray(minx,miny,maxx-minx+1,maxy-miny+1)
            k += 1
#      get the training vectors
        for (x,y) in intersection:         
            Gs.append(cube[y-miny,x-minx,:])
            ls.append(l)   
        polygon = None
        polygon1 = None            
        feature.Destroy()  
    trnDatasource.Destroy() 
    classnames += '}'
    m = len(ls)       
    print str(m) + ' training pixel vectors were read in' 
    Gs = np.array(Gs) 
    ls = np.array(ls)
#  stretch the pixel vectors to [-1,1] for ffn
    maxx = np.max(Gs,0)
    minx = np.min(Gs,0)
    for j in range(N):
        Gs[:,j] = 2*(Gs[:,j]-minx[j])/(maxx[j]-minx[j]) - 1.0 
#  random permutation of training data
    idx = np.random.permutation(m)
    Gs = Gs[idx,:] 
    ls = ls[idx,:]     
#  setup output datasets 
    driver = gdal.GetDriverByName(outfmt)    
    outDataset = driver.Create(outfile,cols,rows,1,GDT_Byte) 
    projection = inDataset.GetProjection()
    geotransform = inDataset.GetGeoTransform()
    if geotransform is not None:
        outDataset.SetGeoTransform(tuple(gt))
    if projection is not None:
        outDataset.SetProjection(projection) 
    outBand = outDataset.GetRasterBand(1) 
    if probfile:
        driver = gdal.GetDriverByName(probfmt)    
        probDataset = driver.Create(probfile,cols,rows,K,GDT_Byte) 
        if geotransform is not None:
            probDataset.SetGeoTransform(tuple(gt))
        if projection is not None:
            probDataset.SetProjection(projection)  
        probBands = [] 
        for k in range(K):
            probBands.append(probDataset.GetRasterBand(k+1))         
    if tstfile:
#  train on 2/3 training examples         
        Gstrn = Gs[0:2*m//3,:]
        lstrn = ls[0:2*m//3,:] 
        Gstst = Gs[2*m//3:,:]  
        lstst = ls[2*m//3:,:]    
    else:
        Gstrn = Gs
        lstrn = ls         
    if   trainalg == 1:
        classifier = sc.Maxlike(Gstrn,lstrn)
    elif trainalg == 2:
        classifier = sc.Ffnbp(Gstrn,lstrn,L)
    elif trainalg == 3:
        classifier = sc.Ffncg(Gstrn,lstrn,L)
    elif trainalg == 4:
        classifier = sc.Svm(Gstrn,lstrn)         
            
    print 'training on %i pixel vectors...' % np.shape(Gstrn)[0]
    start = time.time()
    result = classifier.train()
    print 'elapsed time %s' %str(time.time()-start) 
    if result:
        if trainalg in [2,3]:
            cost = np.log10(result)  
            ymax = np.max(cost)
            ymin = np.min(cost) 
            xmax = len(cost)      
            plt.plot(range(xmax),cost,'k')
            plt.axis([0,xmax,ymin-1,ymax])
            plt.title('Log(Cross entropy)')
            plt.xlabel('Epoch')              
#      classify the image           
        print 'classifying...'
        start = time.time()
        tile = np.zeros((cols,N))    
        for row in range(rows):
            for j in range(N):
                tile[:,j] = rasterBands[j].ReadAsArray(0,row,cols,1)
                tile[:,j] = 2*(tile[:,j]-minx[j])/(maxx[j]-minx[j]) - 1.0               
            cls, Ms = classifier.classify(tile)  
            outBand.WriteArray(np.reshape(cls,(1,cols)),0,row)
            if probfile:
                Ms = np.byte(Ms*255)
                for k in range(K):
                    probBands[k].WriteArray(np.reshape(Ms[k,:],(1,cols)),0,row)
        outBand.FlushCache()
        print 'elapsed time %s' %str(time.time()-start)
        outDataset = None
        inDataset = None      
        if probfile:
            for probBand in probBands:
                probBand.FlushCache() 
            probDataset = None
            print 'class probabilities written to: %s'%probfile   
        K =  lstrn.shape[1]+1                     
        if (outfmt == 'ENVI') and (K<19):
#          try to make an ENVI classification header file            
            hdr = header.Header() 
            headerfile = outfile+'.hdr'
            f = open(headerfile)
            line = f.readline()
            envihdr = ''
            while line:
                envihdr += line
                line = f.readline()
            f.close()         
            hdr.read(envihdr)
            hdr['file type'] ='ENVI Classification'
            hdr['classes'] = str(K)
            classlookup = '{0'
            for i in range(1,3*K):
                classlookup += ', '+str(str(ctable[i]))
            classlookup +='}'    
            hdr['class lookup'] = classlookup
            hdr['class names'] = classnames
            f = open(headerfile,'w')
            f.write(str(hdr))
            f.close()             
        print 'thematic map written to: %s'%outfile
        if trainalg in [2,3]:
            print 'please close the cross entropy plot to continue'
            plt.show()
        if tstfile:
            with open(tstfile,'w') as f:
                print >>f, 'FFN test results for %s'%infile
                print >>f, time.asctime()
                print >>f, 'Classification image: %s'%outfile
                print >>f, 'Class probabilities image: %s'%probfile
                print >>f, lstst.shape[0],lstst.shape[1]
                classes, _ = classifier.classify(Gstst)
                labels = np.argmax(lstst,axis=1)+1
                for i in range(len(classes)):
                    print >>f, classes[i], labels[i]              
                f.close()
                print 'test results written to: %s'%tstfile
        print 'done'
    else:
        print 'an error occured' 
        return 
Exemplo n.º 18
0
def main():

    depth_img = cv2.imread("./images/depth/kaula_depth_map_50fov.png",cv2.IMREAD_GRAYSCALE)
    intensity_img = cv2.imread("./images/intensity/kaula_int_4.jpg",cv2.IMREAD_GRAYSCALE)

    depth_img_transformed = np.empty_like(intensity_img)

    cv2.namedWindow(depthWindow)
    cv2.namedWindow(intensityWindow)
    cv2.namedWindow(blendedWindow, cv2.WINDOW_AUTOSIZE)
    cv2.namedWindow("contour", cv2.WINDOW_AUTOSIZE)

    depth_img = cv2.pyrDown(depth_img)

    intensity_img = cv2.pyrDown(intensity_img)
    intensity_img = cv2.pyrDown(intensity_img)
    intensity_img = cv2.pyrDown(intensity_img)

    cv2.imshow(depthWindow, depth_img)
    cv2.imshow(intensityWindow, intensity_img)

    cv2.waitKey(1000)

    cv2.createTrackbar("lowerThresh", depthWindow, 5, 255, nothing)
    cv2.createTrackbar("higherThresh", depthWindow, 8, 255, nothing)
    cv2.createTrackbar("lowerThresh", intensityWindow, 5, 255, nothing)
    cv2.createTrackbar("higherThresh", intensityWindow, 10, 255, nothing)
    cv2.createTrackbar("corner offset", intensityWindow, 5, 255, nothing)
    cv2.createTrackbar("bilat int", intensityWindow, 50, 255, nothing)
    cv2.createTrackbar("bilat space", intensityWindow, 50, 255, nothing)
    cv2.createTrackbar("blend", blendedWindow, 5, 255, nothing)

    contours_found = []

    cv2.setMouseCallback(depthWindow,mouseCallbackDepth)

    pressedKey = cv2.waitKey(100)

    while pressedKey != 27:

        if pressedKey == ord('c'): #key c

            lower_threshold = cv2.getTrackbarPos("lowerThresh", depthWindow)
            higher_threshold = cv2.getTrackbarPos("higherThresh", depthWindow)
            depth_img_edge = cv2.Canny(depth_img, lower_threshold, higher_threshold)

            bilat_int = cv2.getTrackbarPos("bilat int", intensityWindow)
            bilat_col = cv2.getTrackbarPos("bilat space", intensityWindow)

            intensity_img_filtered = cv2.bilateralFilter(intensity_img, 7, bilat_int, bilat_col)
            lower_threshold = cv2.getTrackbarPos("lowerThresh", intensityWindow)
            higher_threshold = cv2.getTrackbarPos("higherThresh", intensityWindow)
            intensity_img_edge = cv2.Canny(intensity_img_filtered, lower_threshold, higher_threshold)

            structuring_element = cv2.getStructuringElement(cv2.MORPH_RECT,(3,3))
            cv2.dilate(intensity_img_edge, structuring_element, intensity_img_edge)
            cv2.erode(intensity_img_edge, structuring_element, intensity_img_edge)

            corner_offset = cv2.getTrackbarPos("corner offset", intensityWindow)

            corners = cv2.goodFeaturesToTrack(intensity_img, 10, 0.02, 100)

            for i,point in zip(range(0,3),corners[corner_offset:corner_offset+3]):
                cvt_ponint= (int(point[0,0]), int(point[0,1]))
                cv2.circle(intensity_img_edge,cvt_ponint,5,(130+i*50),2)

            cv2.imshow(depthWindow, depth_img_edge)
            cv2.imshow(intensityWindow, intensity_img_edge)

        if pressedKey == ord('t'):  # key t

            transformMatrix = cv2.getAffineTransform(corners[corner_offset:corner_offset+3],depthTransformPoints)
            #transformMatrix = cv2.getAffineTransform(corners[corner_offset:corner_offset+3],np.array([[286, 13],[107, 15],[338, 88]],dtype=np.float32))

            map_x = np.empty_like(intensity_img, dtype=np.float32)
            map_y = np.empty_like(intensity_img, dtype=np.float32)

            for x in range(intensity_img.shape[0]):
                for y in range(intensity_img.shape[1]):

                    transformed = np.dot(transformMatrix,[y, x, 1])
                    map_x[x, y] = transformed[0]
                    map_y[x, y] = transformed[1]

            depth_img_transformed = cv2.remap(depth_img, map_x, map_y, interpolation=cv2.INTER_LINEAR)
            cv2.setMouseCallback(blendedWindow, mouseCallbackBlended, param=depth_img_transformed)

            intensity_img_edge[0, :] = 255
            intensity_img_edge[-1, :] = 255
            intensity_img_edge[:, 0] = 255
            intensity_img_edge[:, -1] = 255

            # lower_threshold = cv2.getTrackbarPos("lowerThresh", depthWindow)
            # higher_threshold = cv2.getTrackbarPos("higherThresh", depthWindow)
            contour = np.copy(intensity_img_edge)

            image, contours_found, hierarchy = cv2.findContours(contour,cv2.RETR_TREE,cv2.CHAIN_APPROX_SIMPLE)
            print("contour shape",contour.shape)
            cv2.imshow(depthWindow,contour)
            print("transformed depth size:", depth_img_transformed.shape, "intensity img size", intensity_img.shape)

        if pressedKey == ord('b'):
            blend = cv2.getTrackbarPos("blend", blendedWindow)
            blended_img = cv2.addWeighted(depth_img_transformed, blend / 255.0, intensity_img, 1 - (blend / 255.0), 0.0)

            for rect in rectList:
                number_of_pts = rect.number_of_added_points()
                for i in range(1,number_of_pts):
                    cv2.line(blended_img,(rect.points[i,1], rect.points[i,0]),(rect.points[i-1,1], rect.points[i-1,0]), 0, 2)
                if number_of_pts == 4:
                    cv2.line(blended_img, (rect.points[3, 1], rect.points[3, 0]), (rect.points[0, 1], rect.points[0, 0]), 0, 2)


            cv2.imshow(blendedWindow, blended_img)

        if pressedKey == ord('p'):

            print("model writing started")

            globalVertexList = np.empty((1,3),dtype=np.float32) # first element is empty

            for rect in rectList:
                rect.regression_coeffitients()

            rectList.sort(key=lambda rect: rect.beta[0]) # ezt lehet meg kel forditani

            faceList = []

            for i in range(len(rectList)): # goes through every rect

                localVertexList = np.empty((4,3), dtype=np.float32)
                localSegmentList = np.empty((1,2), dtype=np.int)
                local_rect_list = []

                localVertexList[0, :] = rectList[i].estimate_point(rectList[i].points[0, :2])
                localVertexList[1, :] = rectList[i].estimate_point(rectList[i].points[1, :2])
                localVertexList[2, :] = rectList[i].estimate_point(rectList[i].points[2, :2])
                localVertexList[3, :] = rectList[i].estimate_point(rectList[i].points[3, :2])

                for j in reversed( range(i) ): #select every rectangle before current

                    rect_project_to = asPolygon(rectList[i].points[:, :2])
                    projected_rect = asPolygon(rectList[j].points[:, :2])

                    inside = projected_rect.within(rect_project_to)

                    if inside == True:

                        segLen = len(localVertexList)

                        temp_vertex_list = np.empty((4, 3), dtype=np.float32)

                        temp_vertex_list[0] = rectList[i].estimate_point(rectList[j].points[0, :2])
                        temp_vertex_list[1] = rectList[i].estimate_point(rectList[j].points[1, :2])
                        temp_vertex_list[2] = rectList[i].estimate_point(rectList[j].points[2, :2])
                        temp_vertex_list[3] = rectList[i].estimate_point(rectList[j].points[3, :2])

                        thisRect = asPolygon(temp_vertex_list)

                        stop_adding=False

                        for other_rect in local_rect_list:
                            if thisRect.within(other_rect):
                                stop_adding=True

                        if not stop_adding:
                            local_rect_list.append(thisRect)

                            localVertexList = np.append( localVertexList,temp_vertex_list,axis=0)

                            temp_segment_list = [[segLen,segLen+1],
                                        [segLen + 1, segLen + 2],
                                        [segLen + 2, segLen + 3],
                                        [segLen + 3, segLen]]

                            localSegmentList = np.append(localSegmentList,temp_segment_list,axis=0)


                triangulation_input = {}
                localSegmentList = np.delete(localSegmentList,0,0)

                triangulation_input['vertices'] = localVertexList[:, 0:2]
                if len(localSegmentList) > 0 :
                    triangulation_input['segments'] = localSegmentList

                tria = tri.triangulate(triangulation_input)

                triangle_offset = len(globalVertexList)

                for triangle in tria['triangles']:

                    vertices = np.array([localVertexList[triangle[0]], localVertexList[triangle[1]], localVertexList[triangle[2]]])
                    trianglePolygon = asPolygon(vertices)

                    add_triangle = True

                    for rect in local_rect_list:
                        if trianglePolygon.within(rect):
                            add_triangle = False

                    if add_triangle:
                        triangle_to_add = triangle + triangle_offset
                        faceList.append(triangle_to_add)

                globalVertexList = np.append(globalVertexList, localVertexList, axis=0)

            globalVertexList = np.delete(globalVertexList, 0, 0)
            textureCoordinates = np.copy(globalVertexList[:,:2])

            textureCoordinates[:, 0] = -1*(textureCoordinates[:, 0]/float(intensity_img.shape[0]))+1
            textureCoordinates[:, 1] = textureCoordinates[:, 1]/float(intensity_img.shape[1])

            make_obj_file("model.obj", globalVertexList, faceList,textureCoordinates,"material.mtl","Kaula")
            print("model written to file")


        pressedKey = cv2.waitKey(10)

    cv2.destroyAllWindows()
Exemplo n.º 19
0
    def __init__(self,
                 polygon_file=None,
                 proj4_str='+proj=lonlat +ellps=WGS84'):

        # self.proj4 = '+proj=lonlat +ellps=WGS84'
        self.proj4 = proj4_str
        self.crs = pyproj.CRS(self.proj4)
        # self.skippoly = skippoly

        super(Reader, self).__init__()

        shore = np.loadtxt(
            polygon_file)  # nan-delimited closed polygons for land and islands

        if 'lonlat' not in proj4_str:  # not functional yet - polygons must be in wgs84 for now
            print(
                'custom landmask polygon must be in WGS84 coordinates..for now'
            )
            import pdb
            pdb.set_trace()
            # if not in wgs84 coordinates already, convert now to lon,lat
            xx, yy = self.xy2lonlat(shore[:, 0], shore[:, 1])
            xx[np.where(np.isinf(xx))] = np.nan
            yy[np.where(np.isinf(yy))] = np.nan
            # update the shore polygon
            shore[:, 0] = xx.copy()
            shore[:, 1] = yy.copy()
            # update the projection
            self.proj4 = '+proj=lonlat +ellps=WGS84'
            self.crs = pyproj.CRS(self.proj4)
            # this doesnt work because self.proj.crs.is_geographic remains false in basereader.py
            # not sure why/how ?
            import pdb
            pdb.set_trace()

        # Depth
        self.z = None
        self.xmin, self.ymin = np.nanmin(shore[:, 0]), np.nanmin(shore[:, 1])
        self.xmax, self.ymax = np.nanmax(shore[:, 0]), np.nanmax(shore[:, 1])
        # convert to xy
        self.xmin, self.ymin = self.lonlat2xy(self.xmin, self.ymin)
        self.xmax, self.ymax = self.lonlat2xy(self.xmax, self.ymax)

        # setup landmask
        # self.mask = Landmask(extent, skippoly)

        # Loop through polygon to build MultiPolygon object
        #
        # make sure that start and end lines are [nan,nan] as well
        if not np.isnan(shore[0, 0]):
            shore = np.vstack(([np.nan, np.nan], shore))
        if not np.isnan(shore[-1, 0]):
            shore = np.vstack((shore, [np.nan, np.nan]))
        id_nans = np.where(np.isnan(shore[:, 0]))[0]
        poly = []
        for cnt, id_i in enumerate(id_nans[:-1]):
            # The shapely.geometry.asShape() family of functions can be used to wrap Numpy coordinate arrays
            # https://shapely.readthedocs.io/en/latest/manual.html
            poly.append(
                asPolygon(shore[id_nans[cnt] + 1:id_nans[cnt + 1] - 1, :]))
        # We can pass multiple Polygon -objects into our MultiPolygon as a list
        self.mask = MultiPolygon(poly)
        self.mask = shapely.prepared.prep(self.mask)
Exemplo n.º 20
0
            state, bboxes, rboxes = siamese_track(state,
                                                  im,
                                                  mask_enable=True,
                                                  refine_enable=True,
                                                  device=device)  # track

            print("---- FRAME ", f, " -------")
            print("num_bxs = ", len(rboxes))
            rboxes = filter_bboxes(rboxes, 5, c=10 *
                                   len(rboxes))  #puto recursiu, funciona be
            print("num_filtered = ", len(rboxes), " - ", rboxes[0][1])

            # dibuixar nomes la bbox filtrada si esta fora de la bona (state)
            locaux_bona = np.int0(state['ploygon'].flatten()).reshape((-1, 2))
            locaux_filt = rboxes[0][0]
            pol_a = asPolygon(locaux_bona)
            pol_b = asPolygon(locaux_filt)
            intersection_area = pol_a.intersection(pol_b)
            if ((intersection_area.area / (pol_a.area + pol_b.area)) <= 0.2):
                for i in range(len(rboxes)):
                    location = rboxes[i][0].flatten()
                    location = np.int0(location).reshape((-1, 1, 2))
                    cv2.polylines(im, [location], True, (255, 255, 0), 1)
                    traj = np.average(location, axis=0)[0]
                    frame_boxes.append([traj])
                    cv2.circle(im, (int(traj[0]), int(traj[1])), 3,
                               (255, 255, 0), 2)

            location = state['ploygon'].flatten()
            mask = state['mask'] > state['p'].seg_thr
            im[:, :, 2] = (mask > 0) * 255 + (mask == 0) * im[:, :, 2]
Exemplo n.º 21
0
def minkowski_sum(poly1, poly2):
    ''' minkowski sum of two convex polygons. 
    Any random polygon that gets input here should be:
        oriented with geom.polygon.orient
        made convex with *.convex_hull
        cleaned with *.buffer(0)
        
    Some of this might be unnecesary but who knows.'''
    #assert poly1.is_valid, validation.explain_validity(poly1)
    #assert poly2.is_valid, validation.explain_validity(poly2)
    
    # Ensure correct edge ordering
    poly1 = geom.polygon.orient(poly1)
    poly2 = geom.polygon.orient(poly2)
    
    sum_edges = []
    sum_angles = []

    min_angle_points = []
    for p in [poly1, poly2]:
        try:
            boundary_pts = np.array(p.boundary)
        except TypeError:
            print 'In minkowski sum: p.boundary didn\'t work, so trying p.exterior'
            plt.figure()
            plt.gca().add_patch(plt.Polygon(np.array(p.exterior)))
            plt.gca().add_patch(plt.Polygon(np.array(p.convex_hull.exterior), alpha=0.5))

            plt.autoscale()
            plt.pause(2)
            plt.close()
            raise Exception
        edges = np.diff(boundary_pts, axis=0)
        angles = np.arctan2(edges[:,1], edges[:,0]) # polar angles
        angles[angles<0] = angles[angles<0] + 2*np.pi # wrap [0, 2pi]


        # save the point where the smallest-angle edge originates so we can shift final 
        # shape correctly
        min_angle_points.append(boundary_pts[np.argmin(angles),:])
        
        min_idx = angles.argmin() # does first edge have the smallest polar angle?
        if min_idx != 0: # shuffle until edges are sorted by angle
            edges = np.roll(edges, -min_idx, axis=0)
            angles = np.roll(angles, -min_idx)
        sum_edges.append(edges)
        sum_angles.append(angles)
    
    sum_edges = np.concatenate(sum_edges) # stick into a big numpy array
    sum_angles = np.concatenate(sum_angles)
    
    idx = np.argsort(sum_angles)
    
    sum_edges = sum_edges[idx,:] # sorted edges of the minkowski sum
    sum_angles = sum_angles[idx] # sorted angles

    # find exterior points on the resulting minkowski sum polygon
    # do this hack to get the absolute coord of the first point
    first_point = min_angle_points[0] + min_angle_points[1]    
    
    sum_ext = np.cumsum(np.concatenate((first_point[np.newaxis,:],sum_edges)), axis=0)
    sum_ext[-1,:] = sum_ext[0,:] # ensure start and end points match
    
    poly = geom.asPolygon(sum_ext)
    #assert poly.is_valid
    return poly
Exemplo n.º 22
0
def main():    
    usage = '''
Usage: 
---------------------------------------------------------
python %s  [-p bandPositions] [- a algorithm] [-L number of hidden neurons]   
[-P generate class probabilities image] filename trainShapefile

bandPositions is a list, e.g., -p [1,2,4]  

algorithm  1=MaxLike
           2=NNet(backprop)
           3=NNet(congrad)
           4=SVM

If the input file is named 

         path/filenbasename.ext then

The output classification file is named 

         path/filebasename_class.ext

the class probabilities output file is named

         path/filebasename_classprobs.ext
         
and the test results file is named

         path/filebasename_<classifier>.tst
--------------------------------------------------------''' %sys.argv[0]
    options, args = getopt.getopt(sys.argv[1:],'hnPp:a:L:')
    pos = None
    probs = False   
    L = 8
    trainalg = 1
    graphics = True
    for option, value in options:
        if option == '-h':
            print usage
            return
        elif option == '-p':
            pos = eval(value)
        elif option == '-n':
            graphics = False            
        elif option == '-a':
            trainalg = eval(value)
        elif option == '-L':
            L = eval(value)    
        elif option == '-P':
            probs = True                              
    if len(args) != 2: 
        print 'Incorrect number of arguments'
        print usage
        sys.exit(1)      
    if trainalg == 1:
        algorithm = 'MaxLike'
    elif trainalg == 2:
        algorithm = 'NNet(Backprop)'
    elif trainalg == 3:
        algorithm =  'NNet(Congrad)'
    else:
        algorithm = 'SVM'              
    infile = args[0]  
    trnfile = args[1]      
    gdal.AllRegister() 
    if infile:                   
        inDataset = gdal.Open(infile,GA_ReadOnly)
        cols = inDataset.RasterXSize
        rows = inDataset.RasterYSize    
        bands = inDataset.RasterCount
        projection = inDataset.GetProjection()
        geotransform = inDataset.GetGeoTransform()
        if geotransform is not None:
            gt = list(geotransform) 
        else:
            print 'No geotransform available'
            return       
        imsr = osr.SpatialReference()  
        imsr.ImportFromWkt(projection)    
    else:
        return  
    if pos is None: 
        pos = range(1,bands+1)
    N = len(pos)    
    rasterBands = [] 
    for b in pos:
        rasterBands.append(inDataset.GetRasterBand(b))     
#  output files
    path = os.path.dirname(infile)
    basename = os.path.basename(infile)
    root, ext = os.path.splitext(basename)
    outfile = '%s/%s_class%s'%(path,root,ext)  
    tstfile = '%s/%s_%s.tst'%(path,root,algorithm)            
    if (trainalg in (2,3,4)) and probs:
#      class probabilities file
        probfile = '%s/%s_classprobs%s'%(path,root,ext) 
    else:
        probfile = None        
#  training data        
    trnDriver = ogr.GetDriverByName('ESRI Shapefile')
    trnDatasource = trnDriver.Open(trnfile,0)
    trnLayer = trnDatasource.GetLayer() 
    trnsr = trnLayer.GetSpatialRef()             
#  coordinate transformation from training to image projection   
    ct = osr.CoordinateTransformation(trnsr,imsr) 
#  number of classes    
    K = 1
    feature = trnLayer.GetNextFeature() 
    while feature:
        classid = feature.GetField('CLASS_ID')
        if int(classid)>K:
            K = int(classid)
        feature = trnLayer.GetNextFeature() 
    trnLayer.ResetReading()    
    K += 1        
#  es kann losgehen    
    print '========================='
    print 'supervised classification'
    print '========================='
    print time.asctime()    
    print 'image:     '+infile
    print 'training:  '+trnfile  
    print 'algorithm: '+algorithm             
#  loop through the polygons    
    Gs = [] # train observations
    ls = [] # class labels
    classnames = '{unclassified'
    classids = set()
    print 'reading training data...'
    for i in range(trnLayer.GetFeatureCount()):
        feature = trnLayer.GetFeature(i)
        classid = str(feature.GetField('CLASS_ID'))
        classname  = feature.GetField('CLASS_NAME')
        if classid not in classids:
            classnames += ',   '+ classname
        classids = classids | set(classid)     
#      label for this ROI           
        l = [0 for i in range(K)]
        l[int(classid)] = 1.0
        polygon = feature.GetGeometryRef()
#      transform to same projection as image        
        polygon.Transform(ct)  
#      convert to a Shapely object            
        poly = shapely.wkt.loads(polygon.ExportToWkt())
#      transform the boundary to pixel coords in numpy        
        bdry = np.array(poly.boundary) 
        bdry[:,0] = bdry[:,0]-gt[0]
        bdry[:,1] = bdry[:,1]-gt[3]
        GT = np.mat([[gt[1],gt[2]],[gt[4],gt[5]]])
        bdry = bdry*np.linalg.inv(GT) 
#      polygon in pixel coords        
        polygon1 = asPolygon(bdry)
#      raster over the bounding rectangle        
        minx,miny,maxx,maxy = map(int,list(polygon1.bounds))  
        pts = [] 
        for i in range(minx,maxx+1):
            for j in range(miny,maxy+1): 
                pts.append((i,j))             
        multipt =  MultiPoint(pts)   
#      intersection as list              
        intersection = np.array(multipt.intersection(polygon1),dtype=np.int).tolist()
#      cut out the bounded image cube               
        cube = np.zeros((maxy-miny+1,maxx-minx+1,len(rasterBands)))
        k=0
        for band in rasterBands:
            cube[:,:,k] = band.ReadAsArray(minx,miny,maxx-minx+1,maxy-miny+1)
            k += 1
#      get the training vectors
        for (x,y) in intersection:         
            Gs.append(cube[y-miny,x-minx,:])
            ls.append(l)   
        polygon = None
        polygon1 = None            
        feature.Destroy()  
    trnDatasource.Destroy() 
    classnames += '}'
    m = len(ls)       
    print str(m) + ' training pixel vectors were read in' 
    Gs = np.array(Gs) 
    ls = np.array(ls)
#  stretch the pixel vectors to [-1,1] for ffn
    maxx = np.max(Gs,0)
    minx = np.min(Gs,0)
    for j in range(N):
        Gs[:,j] = 2*(Gs[:,j]-minx[j])/(maxx[j]-minx[j]) - 1.0 
#  random permutation of training data
    idx = np.random.permutation(m)
    Gs = Gs[idx,:] 
    ls = ls[idx,:]     
#  train on 2/3 training examples         
    Gstrn = Gs[0:2*m//3,:]
    lstrn = ls[0:2*m//3,:] 
    Gstst = Gs[2*m//3:,:]  
    lstst = ls[2*m//3:,:]               
#  setup output datasets 
    driver = inDataset.GetDriver() 
    outDataset = driver.Create(outfile,cols,rows,1,GDT_Byte) 
    projection = inDataset.GetProjection()
    geotransform = inDataset.GetGeoTransform()
    if geotransform is not None:
        outDataset.SetGeoTransform(tuple(gt))
    if projection is not None:
        outDataset.SetProjection(projection) 
    outBand = outDataset.GetRasterBand(1) 
    if probfile:   
        probDataset = driver.Create(probfile,cols,rows,K,GDT_Byte) 
        if geotransform is not None:
            probDataset.SetGeoTransform(tuple(gt))
        if projection is not None:
            probDataset.SetProjection(projection)  
        probBands = [] 
        for k in range(K):
            probBands.append(probDataset.GetRasterBand(k+1))         
#  initialize classifier  
    if   trainalg == 1:
        classifier = sc.Maxlike(Gstrn,lstrn)
    elif trainalg == 2:
        classifier = sc.Ffnbp(Gstrn,lstrn,L)
    elif trainalg == 3:
        classifier = sc.Ffncg(Gstrn,lstrn,L)
    elif trainalg == 4:
        classifier = sc.Svm(Gstrn,lstrn)         
#  train it            
    print 'training on %i pixel vectors...' % np.shape(Gstrn)[0]
    start = time.time()
    result = classifier.train()
    print 'elapsed time %s' %str(time.time()-start) 
    if result:
        if (trainalg in [2,3]) and graphics:
            cost = np.log10(result)  
            ymax = np.max(cost)
            ymin = np.min(cost) 
            xmax = len(cost)      
            plt.plot(range(xmax),cost,'k')
            plt.axis([0,xmax,ymin-1,ymax])
            plt.title('Log(Cross entropy)')
            plt.xlabel('Epoch')              
#      classify the image           
        print 'classifying...'
        start = time.time()
        tile = np.zeros((cols,N),dtype=np.float32)    
        for row in range(rows):
            for j in range(N):
                tile[:,j] = rasterBands[j].ReadAsArray(0,row,cols,1)
                tile[:,j] = 2*(tile[:,j]-minx[j])/(maxx[j]-minx[j]) - 1.0               
            cls, Ms = classifier.classify(tile)  
            outBand.WriteArray(np.reshape(cls,(1,cols)),0,row)
            if probfile:
                Ms = np.byte(Ms*255)
                for k in range(K):
                    probBands[k].WriteArray(np.reshape(Ms[k,:],(1,cols)),0,row)
        outBand.FlushCache()
        print 'elapsed time %s' %str(time.time()-start)
        outDataset = None
        inDataset = None      
        if probfile:
            for probBand in probBands:
                probBand.FlushCache() 
            probDataset = None
            print 'class probabilities written to: %s'%probfile   
        K =  lstrn.shape[1]+1                     
        print 'thematic map written to: %s'%outfile
        if trainalg in [2,3]:
            plt.show()
        if tstfile:
            with open(tstfile,'w') as f:               
                print >>f, algorithm +'test results for %s'%infile
                print >>f, time.asctime()
                print >>f, 'Classification image: %s'%outfile
                print >>f, 'Class probabilities image: %s'%probfile
                print >>f, lstst.shape[0],lstst.shape[1]
                classes, _ = classifier.classify(Gstst)
                labels = np.argmax(lstst,axis=1)+1
                for i in range(len(classes)):
                    print >>f, classes[i], labels[i]              
                f.close()
                print 'test results written to: %s'%tstfile
        print 'done'
    else:
        print 'an error occured' 
        return 
Exemplo n.º 23
0
                                         kelias.juostos[-1].plotis)
    offset_lines = []
    for offset in kelias.juostos:
        l = kelias_line.parallel_offset(offset.plotis * offset_multiplier,
                                        offset.kryptis,
                                        join_style=2)
        offset_lines.append(l)
        ax.plot(*l.xy,
                linewidth=.5,
                dashes=offset.dashes,
                color=offset.spalva,
                zorder=kelias.kat)
    # kelio poligonas su plotu
    kelias_poly = np.vstack((offset_lines[0].coords, offset_lines[-1].coords))
    ax.add_patch(
        PolygonPatch(asPolygon(kelias_poly),
                     fc='white',
                     zorder=kelias.kat,
                     linewidth=0))
    keliai_l[id] = kelias_l(line=kelias_line, offsets=offset_lines)

# kelių anotacijos
for id, kelias in keliai_l.items():
    linestart, lineend = np.array(kelias.offsets[-1].coords)[0:2]
    delta = lineend - linestart
    angle = atan(delta[1] / delta[0]) * 180 / pi
    offset = road_annotations[id]
    ax.annotate(id,
                kelias.offsets[-1].coords[0],
                zorder=KAT0,
                textcoords='offset points',
Exemplo n.º 24
0
 def __init__(self, points, visible=True):
     self.points = np.array(points)
     self.polygon = asPolygon(self.points)
     self.visible = visible
 #compute normal orientation of the segment
normal =  (p[1]-p[0]) * np.array([ 1,-1] ) ; 
normal[0], normal[1], = normal[1],normal[0] ; #exchanging x and y, wihtout copy
normal = normal/np.linalg.norm(normal) ;
print(normal) ;
 
 #creating the upper point and down point for first and second (hopefully last) point in segment
p1u = p[0] + normal * r1 ;
p1d = p[0] - normal * r1 ;

p2u = p[1] + normal * r2 ;
p2d = p[1] - normal * r2 ;
 
output_line = (p1u,p2u,p2d,p1d,p1u) ;
 
ogeom = asPolygon(output_line) ;
 

print(ogeom) ; 
pp1.pprint( str(geom)); 

#outputing for postgis : @NOTE : if inside postgres, hex =False, if outside, hex=True
output = wkb.dumps(ogeom, hex=True);

print(output) ;
 
# ##emulation of postgres output
# #return   None   ;
# 	#return { "center": center, "radius": radius ,  "t1": t1, "t2":t2}
#==============================================================================
	
Exemplo n.º 26
0
    def test_polygon(self):

        # Initialization
        # Linear rings won't usually be created by users, but by polygons
        coords = ((0.0, 0.0), (0.0, 1.0), (1.0, 1.0), (1.0, 0.0))
        ring = LinearRing(coords)
        self.assertEqual(len(ring.coords), 5)
        self.assertEqual(ring.coords[0], ring.coords[4])
        self.assertEqual(ring.coords[0], ring.coords[-1])
        self.assertTrue(ring.is_ring)

        # Ring from sequence of Points
        self.assertEqual(LinearRing((map(Point, coords))), ring)

        # Coordinate modification
        ring.coords = ((0.0, 0.0), (0.0, 2.0), (2.0, 2.0), (2.0, 0.0))
        self.assertEqual(
            ring.__geo_interface__, {
                'type':
                'LinearRing',
                'coordinates':
                ((0.0, 0.0), (0.0, 2.0), (2.0, 2.0), (2.0, 0.0), (0.0, 0.0))
            })

        # Test ring adapter
        coords = [[0.0, 0.0], [0.0, 1.0], [1.0, 1.0], [1.0, 0.0]]
        ra = asLinearRing(coords)
        self.assertTrue(ra.wkt.upper().startswith('LINEARRING'))
        self.assertEqual(dump_coords(ra), [(0.0, 0.0), (0.0, 1.0), (1.0, 1.0),
                                           (1.0, 0.0), (0.0, 0.0)])
        coords[3] = [2.0, -1.0]
        self.assertEqual(dump_coords(ra), [(0.0, 0.0), (0.0, 1.0), (1.0, 1.0),
                                           (2.0, -1.0), (0.0, 0.0)])

        # Construct a polygon, exterior ring only
        polygon = Polygon(coords)
        self.assertEqual(len(polygon.exterior.coords), 5)

        # Ring Access
        self.assertIsInstance(polygon.exterior, LinearRing)
        ring = polygon.exterior
        self.assertEqual(len(ring.coords), 5)
        self.assertEqual(ring.coords[0], ring.coords[4])
        self.assertEqual(ring.coords[0], (0., 0.))
        self.assertTrue(ring.is_ring)
        self.assertEqual(len(polygon.interiors), 0)

        # Create a new polygon from WKB
        data = polygon.wkb
        polygon = None
        ring = None
        polygon = load_wkb(data)
        ring = polygon.exterior
        self.assertEqual(len(ring.coords), 5)
        self.assertEqual(ring.coords[0], ring.coords[4])
        self.assertEqual(ring.coords[0], (0., 0.))
        self.assertTrue(ring.is_ring)
        polygon = None

        # Interior rings (holes)
        polygon = Polygon(coords, [((0.25, 0.25), (0.25, 0.5), (0.5, 0.5),
                                    (0.5, 0.25))])
        self.assertEqual(len(polygon.exterior.coords), 5)
        self.assertEqual(len(polygon.interiors[0].coords), 5)
        with self.assertRaises(IndexError):  # index out of range
            polygon.interiors[1]

        # Test from another Polygon
        copy = Polygon(polygon)
        self.assertEqual(len(polygon.exterior.coords), 5)
        self.assertEqual(len(polygon.interiors[0].coords), 5)
        with self.assertRaises(IndexError):  # index out of range
            polygon.interiors[1]

        # Coordinate getters and setters raise exceptions
        self.assertRaises(NotImplementedError, polygon._get_coords)
        with self.assertRaises(NotImplementedError):
            polygon.coords

        # Geo interface
        self.assertEqual(
            polygon.__geo_interface__, {
                'type':
                'Polygon',
                'coordinates':
                (((0.0, 0.0), (0.0, 1.0), (1.0, 1.0), (2.0, -1.0), (0.0, 0.0)),
                 ((0.25, 0.25), (0.25, 0.5), (0.5, 0.5), (0.5, 0.25),
                  (0.25, 0.25)))
            })

        # Adapter
        hole_coords = [((0.25, 0.25), (0.25, 0.5), (0.5, 0.5), (0.5, 0.25))]
        pa = asPolygon(coords, hole_coords)
        self.assertEqual(len(pa.exterior.coords), 5)
        self.assertEqual(len(pa.interiors), 1)
        self.assertEqual(len(pa.interiors[0].coords), 5)

        # Test Non-operability of Null rings
        r_null = LinearRing()
        self.assertEqual(r_null.wkt, 'GEOMETRYCOLLECTION EMPTY')
        self.assertEqual(r_null.length, 0.0)

        # Check that we can set coordinates of a null geometry
        r_null.coords = [(0, 0), (1, 1), (1, 0)]
        self.assertAlmostEqual(r_null.length, 3.414213562373095)

        # Error handling
        with self.assertRaises(ValueError):
            # A LinearRing must have at least 3 coordinate tuples
            Polygon([[1, 2], [2, 3]])
Exemplo n.º 27
0
def readshp(trnfile, inDataset, pos):
    #  projection info from input image
    projection = inDataset.GetProjection()
    geotransform = inDataset.GetGeoTransform()
    gt = list(geotransform)
    imsr = osr.SpatialReference()
    imsr.ImportFromWkt(projection)

    trnDriver = ogr.GetDriverByName('ESRI Shapefile')
    trnDatasource = trnDriver.Open(trnfile, 0)
    trnLayer = trnDatasource.GetLayer()
    trnsr = trnLayer.GetSpatialRef()
    #  coordinate transformation from training to image projection
    ct = osr.CoordinateTransformation(trnsr, imsr)
    #  image bands
    rasterBands = []
    for b in pos:
        rasterBands.append(inDataset.GetRasterBand(b))
#  number of classes
    K = 1
    feature = trnLayer.GetNextFeature()
    while feature:
        classid = feature.GetField('CLASS_ID')
        if int(classid) > K:
            K = int(classid)
        feature = trnLayer.GetNextFeature()
    trnLayer.ResetReading()
    K += 1
    #  loop through the polygons
    Xs = []  # train observations (data matrix)
    Ls = []  # class labels (lists)
    classnames = []
    classids = set()
    print 'reading training data...'
    for i in range(trnLayer.GetFeatureCount()):
        feature = trnLayer.GetFeature(i)
        classid = str(feature.GetField('CLASS_ID'))
        classname = feature.GetField('CLASS_NAME')
        if classid not in classids:
            classnames.append(classname)
        classids = classids | set(classid)
        #      label for this ROI
        y = int(classid)
        l = [0 for i in range(K)]
        l[y] = 1.0
        polygon = feature.GetGeometryRef()
        #      transform to same projection as image
        polygon.Transform(ct)
        #      convert to a Shapely object
        poly = shapely.wkt.loads(polygon.ExportToWkt())
        #      transform the boundary to pixel coords in numpy
        bdry = np.array(poly.boundary)
        bdry[:, 0] = bdry[:, 0] - gt[0]
        bdry[:, 1] = bdry[:, 1] - gt[3]
        GT = np.mat([[gt[1], gt[2]], [gt[4], gt[5]]])
        bdry = bdry * np.linalg.inv(GT)
        #      polygon in pixel coords
        polygon1 = asPolygon(bdry)
        #      raster over the bounding rectangle
        minx, miny, maxx, maxy = map(int, list(polygon1.bounds))
        pts = []
        for i in range(minx, maxx + 1):
            for j in range(miny, maxy + 1):
                pts.append((i, j))
        multipt = MultiPoint(pts)
        #      intersection as list
        intersection = np.array(multipt.intersection(polygon1),
                                dtype=np.int).tolist()
        #      cut out the bounded image cube
        cube = np.zeros((maxy - miny + 1, maxx - minx + 1, len(rasterBands)))
        k = 0
        for band in rasterBands:
            cube[:, :, k] = band.ReadAsArray(minx, miny, maxx - minx + 1,
                                             maxy - miny + 1)
            k += 1


#      get the training vectors
        for (x, y) in intersection:
            Xs.append(cube[y - miny, x - minx, :])
            Ls.append(l)
        polygon = None
        polygon1 = None
        feature.Destroy()
    trnDatasource.Destroy()
    Xs = np.array(Xs)
    Ls = np.array(Ls)
    return (Xs, Ls, K, classnames)
Exemplo n.º 28
0
def main():
    gdal.AllRegister()
    path = auxil.select_directory('Input directory')
    if path:
        os.chdir(path)
#  input image
    infile = auxil.select_infile(title='Image file')
    if infile:
        inDataset = gdal.Open(infile, GA_ReadOnly)
        cols = inDataset.RasterXSize
        rows = inDataset.RasterYSize
        bands = inDataset.RasterCount
        projection = inDataset.GetProjection()
        geotransform = inDataset.GetGeoTransform()
        if geotransform is not None:
            gt = list(geotransform)
        else:
            print 'No geotransform available'
            return
        imsr = osr.SpatialReference()
        imsr.ImportFromWkt(projection)
    else:
        return
    pos = auxil.select_pos(bands)
    if not pos:
        return
    N = len(pos)
    rasterBands = []
    for b in pos:
        rasterBands.append(inDataset.GetRasterBand(b))
#  training algorithm
    trainalg = auxil.select_integer(1,
                                    msg='1:Maxlike,2:Backprop,3:Congrad,4:SVM')
    if not trainalg:
        return
#  training data (shapefile)
    trnfile = auxil.select_infile(filt='.shp', title='Train shapefile')
    if trnfile:
        trnDriver = ogr.GetDriverByName('ESRI Shapefile')
        trnDatasource = trnDriver.Open(trnfile, 0)
        trnLayer = trnDatasource.GetLayer()
        trnsr = trnLayer.GetSpatialRef()
    else:
        return
    tstfile = auxil.select_outfile(filt='.tst', title='Test results file')
    if not tstfile:
        print 'No test output'
#  outfile
    outfile, outfmt = auxil.select_outfilefmt(title='Classification file')
    if not outfile:
        return
    if trainalg in (2, 3, 4):
        #      class probabilities file, hidden neurons
        probfile, probfmt = auxil.select_outfilefmt(title='Probabilities file')
    else:
        probfile = None
    if trainalg in (2, 3):
        L = auxil.select_integer(8, 'Number of hidden neurons')
        if not L:
            return
#  coordinate transformation from training to image projection
    ct = osr.CoordinateTransformation(trnsr, imsr)
    #  number of classes
    K = 1
    feature = trnLayer.GetNextFeature()
    while feature:
        classid = feature.GetField('CLASS_ID')
        if int(classid) > K:
            K = int(classid)
        feature = trnLayer.GetNextFeature()
    trnLayer.ResetReading()
    K += 1
    print '========================='
    print 'supervised classification'
    print '========================='
    print time.asctime()
    print 'image:    ' + infile
    print 'training: ' + trnfile
    if trainalg == 1:
        print 'Maximum Likelihood'
    elif trainalg == 2:
        print 'Neural Net (Backprop)'
    elif trainalg == 3:
        print 'Neural Net (Congrad)'
    else:
        print 'Support Vector Machine'
#  loop through the polygons
    Gs = []  # train observations
    ls = []  # class labels
    classnames = '{unclassified'
    classids = set()
    print 'reading training data...'
    for i in range(trnLayer.GetFeatureCount()):
        feature = trnLayer.GetFeature(i)
        classid = str(feature.GetField('CLASS_ID'))
        classname = feature.GetField('CLASS_NAME')
        if classid not in classids:
            classnames += ',   ' + classname
        classids = classids | set(classid)
        l = [0 for i in range(K)]
        l[int(classid)] = 1.0
        polygon = feature.GetGeometryRef()
        #      transform to same projection as image
        polygon.Transform(ct)
        #      convert to a Shapely object
        poly = shapely.wkt.loads(polygon.ExportToWkt())
        #      transform the boundary to pixel coords in numpy
        bdry = np.array(poly.boundary)
        bdry[:, 0] = bdry[:, 0] - gt[0]
        bdry[:, 1] = bdry[:, 1] - gt[3]
        GT = np.mat([[gt[1], gt[2]], [gt[4], gt[5]]])
        bdry = bdry * np.linalg.inv(GT)
        #      polygon in pixel coords
        polygon1 = asPolygon(bdry)
        #      raster over the bounding rectangle
        minx, miny, maxx, maxy = map(int, list(polygon1.bounds))
        pts = []
        for i in range(minx, maxx + 1):
            for j in range(miny, maxy + 1):
                pts.append((i, j))
        multipt = MultiPoint(pts)
        #      intersection as list
        intersection = np.array(multipt.intersection(polygon1),
                                dtype=np.int).tolist()
        #      cut out the bounded image cube
        cube = np.zeros((maxy - miny + 1, maxx - minx + 1, len(rasterBands)))
        k = 0
        for band in rasterBands:
            cube[:, :, k] = band.ReadAsArray(minx, miny, maxx - minx + 1,
                                             maxy - miny + 1)
            k += 1
#      get the training vectors
        for (x, y) in intersection:
            Gs.append(cube[y - miny, x - minx, :])
            ls.append(l)
        polygon = None
        polygon1 = None
        feature.Destroy()
    trnDatasource.Destroy()
    classnames += '}'
    m = len(ls)
    print str(m) + ' training pixel vectors were read in'
    Gs = np.array(Gs)
    ls = np.array(ls)
    #  stretch the pixel vectors to [-1,1] for ffn
    maxx = np.max(Gs, 0)
    minx = np.min(Gs, 0)
    for j in range(N):
        Gs[:, j] = 2 * (Gs[:, j] - minx[j]) / (maxx[j] - minx[j]) - 1.0
#  random permutation of training data
    idx = np.random.permutation(m)
    Gs = Gs[idx, :]
    ls = ls[idx, :]
    #  setup output datasets
    driver = gdal.GetDriverByName(outfmt)
    outDataset = driver.Create(outfile, cols, rows, 1, GDT_Byte)
    projection = inDataset.GetProjection()
    geotransform = inDataset.GetGeoTransform()
    if geotransform is not None:
        outDataset.SetGeoTransform(tuple(gt))
    if projection is not None:
        outDataset.SetProjection(projection)
    outBand = outDataset.GetRasterBand(1)
    if probfile:
        driver = gdal.GetDriverByName(probfmt)
        probDataset = driver.Create(probfile, cols, rows, K, GDT_Byte)
        if geotransform is not None:
            probDataset.SetGeoTransform(tuple(gt))
        if projection is not None:
            probDataset.SetProjection(projection)
        probBands = []
        for k in range(K):
            probBands.append(probDataset.GetRasterBand(k + 1))
    if tstfile:
        #  train on 2/3 training examples
        Gstrn = Gs[0:2 * m // 3, :]
        lstrn = ls[0:2 * m // 3, :]
        Gstst = Gs[2 * m // 3:, :]
        lstst = ls[2 * m // 3:, :]
    else:
        Gstrn = Gs
        lstrn = ls
    if trainalg == 1:
        classifier = sc.Maxlike(Gstrn, lstrn)
    elif trainalg == 2:
        classifier = sc.Ffnbp(Gstrn, lstrn, L)
    elif trainalg == 3:
        classifier = sc.Ffncg(Gstrn, lstrn, L)
    elif trainalg == 4:
        classifier = sc.Svm(Gstrn, lstrn)

    print 'training on %i pixel vectors...' % np.shape(Gstrn)[0]
    start = time.time()
    result = classifier.train()
    print 'elapsed time %s' % str(time.time() - start)
    if result:
        if trainalg in [2, 3]:
            cost = np.log10(result)
            ymax = np.max(cost)
            ymin = np.min(cost)
            xmax = len(cost)
            plt.plot(range(xmax), cost, 'k')
            plt.axis([0, xmax, ymin - 1, ymax])
            plt.title('Log(Cross entropy)')
            plt.xlabel('Epoch')


#      classify the image
        print 'classifying...'
        start = time.time()
        tile = np.zeros((cols, N))
        for row in range(rows):
            for j in range(N):
                tile[:, j] = rasterBands[j].ReadAsArray(0, row, cols, 1)
                tile[:, j] = 2 * (tile[:, j] - minx[j]) / (maxx[j] -
                                                           minx[j]) - 1.0
            cls, Ms = classifier.classify(tile)
            outBand.WriteArray(np.reshape(cls, (1, cols)), 0, row)
            if probfile:
                Ms = np.byte(Ms * 255)
                for k in range(K):
                    probBands[k].WriteArray(np.reshape(Ms[k, :], (1, cols)), 0,
                                            row)
        outBand.FlushCache()
        print 'elapsed time %s' % str(time.time() - start)
        outDataset = None
        inDataset = None
        if probfile:
            for probBand in probBands:
                probBand.FlushCache()
            probDataset = None
            print 'class probabilities written to: %s' % probfile
        K = lstrn.shape[1] + 1
        if (outfmt == 'ENVI') and (K < 19):
            #          try to make an ENVI classification header file
            hdr = header.Header()
            headerfile = outfile + '.hdr'
            f = open(headerfile)
            line = f.readline()
            envihdr = ''
            while line:
                envihdr += line
                line = f.readline()
            f.close()
            hdr.read(envihdr)
            hdr['file type'] = 'ENVI Classification'
            hdr['classes'] = str(K)
            classlookup = '{0'
            for i in range(1, 3 * K):
                classlookup += ', ' + str(str(ctable[i]))
            classlookup += '}'
            hdr['class lookup'] = classlookup
            hdr['class names'] = classnames
            f = open(headerfile, 'w')
            f.write(str(hdr))
            f.close()
        print 'thematic map written to: %s' % outfile
        if trainalg in [2, 3]:
            print 'please close the cross entropy plot to continue'
            plt.show()
        if tstfile:
            with open(tstfile, 'w') as f:
                print >> f, 'FFN test results for %s' % infile
                print >> f, time.asctime()
                print >> f, 'Classification image: %s' % outfile
                print >> f, 'Class probabilities image: %s' % probfile
                print >> f, lstst.shape[0], lstst.shape[1]
                classes, _ = classifier.classify(Gstst)
                labels = np.argmax(lstst, axis=1) + 1
                for i in range(len(classes)):
                    print >> f, classes[i], labels[i]
                f.close()
                print 'test results written to: %s' % tstfile
        print 'done'
    else:
        print 'an error occured'
        return
out1 = watershed_seg(image, show_img=True, file_name="eroded_seal.png")

out1[0][0][0]
splotches = out1[0]

########## Working out overlap/rotation for matching

rots = polygon_rotator(out1[0][16], angle=2*math.pi/180) #test1 eroded
rots_1 = polygon_rotator(out2[0][20], angle=2*math.pi/4) #seal1_1 eroded1
sg.Polygon(rots[18]).is_valid
out2 = out1 #test1

PercentOverlap = poly1.intersection(poly2).area / poly1.union(poly2).area
print PercentOverlap

poly1 = sg.asPolygon(out1[0][0])

poly1 = sg.asPolygon(rots[0])
poly2 = sg.asPolygon(rots_1[0]) #pol2cart(rots_1[18])
poly2 = poly1

poly3 = sg.Polygon(pol2cart(rots[18]))
poly4 = sg.Polygon(pol2cart(rots_1[20]))
poly3.buffer(0).wkt
poly4 = poly3.buffer(0)
poly1
poly2
PercentOverlap = poly1.intersection(poly2).area / poly1.union(poly2).area
poly1.type
poly1.is_valid
Exemplo n.º 30
0
def test_polygon_adapter_deprecated():
    coords = ((0.0, 0.0), (0.0, 1.0), (1.0, 1.0), (1.0, 0.0))
    hole_coords = [((0.25, 0.25), (0.25, 0.5), (0.5, 0.5), (0.5, 0.25))]
    with pytest.warns(ShapelyDeprecationWarning, match="proxy geometries"):
        asPolygon(coords, hole_coords)
Exemplo n.º 31
0
    def response(self, x, y, method='fractional'):
        r"""
        Compute the response function of the aperture to the sky over
        a regular grid. This is the same as rendering an "image" of
        the aperture.

        The integral of the returned map is normalized to the
        aperture area.

        Args:
            x (array-like):
                The list of x coordinates for the grid.  Must be
                linearly spaced.
            y (array-like):
                The list of y coordinates for the grid.  Must be
                linearly spaced.
            method (:obj:`str`, optional):
                Method used to construct the overlap grid.  Options
                are:

                    - 'whole': Any grid cell with its center inside the
                      aperture is set to the area of the grid cell.  All
                      others set to 0.
                    - 'fractional': Perform the detailed calculation of
                      the fraction of each grid-cell within the
                      aperture.
    
        Returns:
            `numpy.ndarray`_: An array with shape :math:`(N_x, N_y)`
            with the fraction of each grid cell covered by the
            aperture.

        Raises:
            ValueError:
                Raised if the provided arguments are not regularly
                spaced, or if there aren't at least 2 grid points in
                each dimension.
        """
        # Check input
        if len(x) < 2 or len(y) < 2:
            raise ValueError('Must provide at least 2 points per grid point.')
        minimum_x_difference = 0 if numpy.issubdtype(x.dtype, numpy.integer) \
                                    else numpy.finfo(x.dtype).eps*100
        minimum_y_difference = 0 if numpy.issubdtype(y.dtype, numpy.integer) \
                                    else numpy.finfo(y.dtype).eps*100
        if numpy.any(numpy.absolute(numpy.diff(numpy.diff(x))) > minimum_x_difference):
            raise ValueError('X coordinates are not regular to numerical precision.')
        if numpy.any(numpy.absolute(numpy.diff(numpy.diff(y))) > minimum_y_difference):
            raise ValueError('Y coordinates are not regular to numerical precision.')

        # Grid shape
        nx = len(x)
        ny = len(y)

        # Get the cell size
        dx = abs(x[1]-x[0])
        dy = abs(y[1]-y[0])

        cell_area = dx*dy

        if method == 'whole':
            # Only include whole pixels
#            X,Y = map(lambda x : x.ravel(), numpy.meshgrid(x, y))
#            img = numpy.array(list(map(lambda x: self.shape.contains(Point(x[0],x[1])),
#                                        zip(X,Y)))).reshape(ny,nx).astype(int)/cell_area
            # Build the coordinate of the pixel centers
            coo = numpy.array(list(map(lambda x : x.ravel(), numpy.meshgrid(x, y)))).T
            # Find the pixels with their centers inside the shape
            img = point_inside_polygon(self.vertices(), coo).reshape(ny,nx).astype(int)/cell_area
            # Return after adjusting to match the defined area
            return img * (self.area/numpy.sum(img)/cell_area)
        elif method == 'fractional':
            # Allow for fractional pixels by determining the overlap
            # between the shape and each grid cell

#            # Build the cell polygons
#            cells, sx, ex, sy, ey = self._overlapping_grid_polygons(x, y)
#
#            # Construct a grid with the fractional area covered by the
#            # aperture
#            img = numpy.zeros((len(y), len(x)), dtype=float)
#            img[sy:ey,sx:ex] = numpy.array(list(map(lambda x: self.shape.intersection(x).area,
#                                                    cells))).reshape(ey-sy,ex-sx)/cell_area
#            return img * (self.area/numpy.sum(img)/cell_area)

            # NOTE: This is much faster than the above
            sx, ex, _, sy, ey, _ = self._overlapping_region(x,y)

            X,Y = map(lambda x : x.ravel(), numpy.meshgrid(x[sx:ex], y[sy:ey]))

            # Get the points describing the corners of each overlapping grid cell
            cx = X[:,None] + (numpy.array([-0.5,0.5,0.5,-0.5])*dx)[None,:]
            cy = Y[:,None] + (numpy.array([-0.5,-0.5,0.5,0.5])*dy)[None,:]
            cells = numpy.append(cx, cy, axis=1).reshape(-1,2,4).transpose(0,2,1).reshape(-1,2)
            # cells has shape (ncell*4,2)
            inside_shape = point_inside_polygon(self.vertices(), cells).reshape(-1,4)
            indx = numpy.all(inside_shape, axis=1)
            _img = indx.astype(float)
            intersect = numpy.any(inside_shape, axis=1) & numpy.invert(indx)
            _img[intersect] = numpy.array(list(map(lambda x:
                                                    self.shape.intersection(asPolygon(x)).area,
                                                cells.reshape(-1,4,2)[intersect,...])))/cell_area
            img = numpy.zeros((len(y), len(x)), dtype=float)
            img[sy:ey,sx:ex] = _img.reshape(ey-sy,ex-sx)
            return img * (self.area/numpy.sum(img)/cell_area)

        raise ValueError('Unknown response method {0}.'.format(method))
Exemplo n.º 32
0
def main():  
    gdal.AllRegister()
    path = auxil.select_directory('Choose input directory')
    if path:
        os.chdir(path)        
#  input image    
    infile = auxil.select_infile(title='Choose image file') 
    if infile:                   
        inDataset = gdal.Open(infile,GA_ReadOnly)     
        cols = inDataset.RasterXSize
        rows = inDataset.RasterYSize    
        bands = inDataset.RasterCount
        projection = inDataset.GetProjection()
        geotransform = inDataset.GetGeoTransform()
        if geotransform is not None:
            gt = list(geotransform) 
        else:
            print 'No geotransform available'
            return       
        imsr = osr.SpatialReference()  
        imsr.ImportFromWkt(projection)      
    else:
        return  
    pos =  auxil.select_pos(bands)  
    if not pos:
        return
    N = len(pos) 
    rasterBands = [] 
    for b in pos:
        rasterBands.append(inDataset.GetRasterBand(b)) 
#  training data (shapefile)      
    trnfile = auxil.select_infile(filt='.shp',title='Choose train shapefile')
    if trnfile:
        trnDriver = ogr.GetDriverByName('ESRI Shapefile')
        trnDatasource = trnDriver.Open(trnfile,0)
        trnLayer = trnDatasource.GetLayer() 
        trnsr = trnLayer.GetSpatialRef()             
    else:
        return
#  hidden neurons
    L = auxil.select_integer(8,'number of hidden neurons')    
    if not L:
        return
#  outfile
    outfile, fmt = auxil.select_outfilefmt()   
    if not outfile:
        return     
#  coordinate transformation from training to image projection   
    ct= osr.CoordinateTransformation(trnsr,imsr) 
#  number of classes    
    feature = trnLayer.GetNextFeature() 
    while feature:
        classid = feature.GetField('CLASS_ID')
        feature = trnLayer.GetNextFeature() 
    trnLayer.ResetReading()    
    K = int(classid)+1       
    print '========================='
    print '       ffncg'
    print '========================='
    print time.asctime()    
    print 'image:    '+infile
    print 'training: '+trnfile          
#  loop through the polygons    
    Gs = [] # train observations
    ls = [] # class labels
    print 'reading training data...'
    for i in range(trnLayer.GetFeatureCount()):
        feature = trnLayer.GetFeature(i)
        classid = feature.GetField('CLASS_ID')
        l = [0 for i in range(K)]
        l[int(classid)] = 1.0
        polygon = feature.GetGeometryRef()
#      transform to same projection as image        
        polygon.Transform(ct)  
#      convert to a Shapely object            
        poly = shapely.wkt.loads(polygon.ExportToWkt())
#      transform the boundary to pixel coords in numpy        
        bdry = np.array(poly.boundary) 
        bdry[:,0] = bdry[:,0]-gt[0]
        bdry[:,1] = bdry[:,1]-gt[3]
        GT = np.mat([[gt[1],gt[2]],[gt[4],gt[5]]])
        bdry = bdry*np.linalg.inv(GT) 
#      polygon in pixel coords        
        polygon1 = asPolygon(bdry)
#      raster over the bounding rectangle        
        minx,miny,maxx,maxy = map(int,list(polygon1.bounds))  
        pts = [] 
        for i in range(minx,maxx+1):
            for j in range(miny,maxy+1): 
                pts.append((i,j))             
        multipt =  MultiPoint(pts)   
#      intersection as list              
        intersection = np.array(multipt.intersection(polygon1),dtype=np.int).tolist()
#      cut out the bounded image cube               
        cube = np.zeros((maxy-miny+1,maxx-minx+1,len(rasterBands)))
        k=0
        for band in rasterBands:
            cube[:,:,k] = band.ReadAsArray(minx,miny,maxx-minx+1,maxy-miny+1)
            k += 1
#      get the training vectors
        for (x,y) in intersection:         
            Gs.append(cube[y-miny,x-minx,:])
            ls.append(l)   
        polygon = None
        polygon1 = None            
        feature.Destroy()  
    trnDatasource.Destroy() 
    m = len(ls)       
    print str(m) + ' training pixel vectors were read in' 
    Gs = np.array(Gs) 
    ls = np.array(ls)
#  stretch the pixel vectors to [-1,1]
    maxx = np.max(Gs,0)
    minx = np.min(Gs,0)
    for j in range(N):
        Gs[:,j] = 2*(Gs[:,j]-minx[j])/(maxx[j]-minx[j]) - 1.0 
#  random permutation of training data
    idx = np.random.permutation(m)
    Gs = Gs[idx,:] 
    ls = ls[idx,:]     
#  setup output dataset 
    driver = gdal.GetDriverByName(fmt)    
    outDataset = driver.Create(outfile,cols,rows,1,GDT_Byte) 
    projection = inDataset.GetProjection()
    geotransform = inDataset.GetGeoTransform()
    if geotransform is not None:
        outDataset.SetGeoTransform(tuple(gt))
    if projection is not None:
        outDataset.SetProjection(projection) 
    outBand = outDataset.GetRasterBand(1) 
#  train on 9/10 training examples         
    Gstrn = Gs[0:9*m//10,:]
    lstrn = ls[0:9*m//10,:]
    affn = Ffncg(Gstrn,lstrn,L)
    print 'training on %i pixel vectors...' % np.shape(Gstrn)[0]
    start = time.time()
    cost = affn.train(epochs=epochs)
    print 'elapsed time %s' %str(time.time()-start) 
    if cost is not None:
#        cost = np.log10(cost)  
        ymax = np.max(cost)
        ymin = np.min(cost) 
        xmax = len(cost)      
        plt.plot(range(xmax),cost,'k')
        plt.axis([0,xmax,ymin-1,ymax])
        plt.title('Cross entropy')
        plt.xlabel('Epoch')              
#      classify the image           
        print 'classifying...'
        tile = np.zeros((cols,N))    
        for row in range(rows):
            for j in range(N):
                tile[:,j] = rasterBands[j].ReadAsArray(0,row,cols,1)
                tile[:,j] = 2*(tile[:,j]-minx[j])/(maxx[j]-minx[j]) - 1.0 
            cls, _ = affn.classify(tile)  
            outBand.WriteArray(np.reshape(cls,(1,cols)),0,row)
        outBand.FlushCache()
        outDataset = None
        inDataset = None  
        print 'thematic map written to: ' + outfile
        print 'please close the cross entropy plot to continue'
        plt.show()
    else:
        print 'an error occured' 
        return 
    
    print 'submitting cross-validation to multyvac'    
    start = time.time()
    jid = mv.submit(traintst,Gs,ls,L,_layer='ms_image_analysis')  
    print 'submission time: %s' %str(time.time()-start)
    start = time.time()    
    job = mv.get(jid)
    result = job.get_result(job) 
    
    
    print 'execution time: %s' %str(time.time()-start)      
    print 'misclassification rate: %f' %np.mean(result)
    print 'standard deviation:     %f' %np.std(result)         
    print '--------done---------------------'       
Exemplo n.º 33
0
    def calculate(self, k=3):
        """
        Calculates the convex hull of the data set as an array of points
        :param k: Number of nearest neighbors
        :return: Array of points (N, 2) with the concave hull of the data set
        """
        if self.data_set.shape[0] < 3:
            return None

        if self.data_set.shape[0] == 3:
            return self.data_set

        # Make sure that k neighbors can be found
        kk = min(k, self.data_set.shape[0])

        first_point = self.get_lowest_latitude_index(self.data_set)
        current_point = first_point

        # Note that hull and test_hull are matrices (N, 2)
        hull = np.reshape(np.array(self.data_set[first_point, :]), (1, 2))
        test_hull = hull

        # Remove the first point
        self.indices[first_point] = False

        prev_angle = 270  # Initial reference id due west. North is zero, measured clockwise.
        step = 2
        stop = 2 + kk

        while ((current_point != first_point) or
               (step == 2)) and len(self.indices[self.indices]) > 0:
            if step == stop:
                self.indices[first_point] = True

            knn = self.get_k_nearest(current_point, kk)

            # Calculates the headings between first_point and the knn points
            # Returns angles in the same indexing sequence as in knn
            angles = self.calculate_headings(current_point, knn, prev_angle)

            # Calculate the candidate indexes (largest angles first)
            candidates = np.argsort(-angles)

            i = 0
            invalid_hull = True

            while invalid_hull and i < len(candidates):
                candidate = candidates[i]

                # Create a test hull to check if there are any self-intersections
                next_point = np.reshape(self.data_set[knn[candidate]], (1, 2))
                test_hull = np.append(hull, next_point, axis=0)

                line = asLineString(test_hull)
                invalid_hull = not line.is_simple
                i += 1

            if invalid_hull:
                return self.recurse_calculate()

            # prev_angle = self.calculate_headings(current_point, np.array([knn[candidate]]))
            prev_angle = self.calculate_headings(knn[candidate],
                                                 np.array([current_point]))
            current_point = knn[candidate]
            hull = test_hull

            # write_line_string(hull)

            self.indices[current_point] = False
            step += 1

        poly = asPolygon(hull)

        count = 0
        total = self.data_set.shape[0]
        for ix in range(total):
            pt = asPoint(self.data_set[ix, :])
            if poly.intersects(pt) or pt.within(poly):
                count += 1
            else:
                d = poly.distance(pt)
                if d < 1e-5:
                    count += 1

        if count == total:
            return hull
        else:
            return self.recurse_calculate()
Exemplo n.º 34
0
 def show_polygon(ax, polygon, facecolor=None, alpha=1.):
     ax.add_artist(
         PolygonPatch(geom.asPolygon(polygon),
                      facecolor=facecolor,
                      alpha=alpha))
Exemplo n.º 35
0
def main():    
    usage = '''
Usage: 
---------------------------------------------------------
python %s  [-p bandPositions] [- a algorithm] [-L number of hidden neurons]   
[-P generate class probabilities image] filename trainShapefile

bandPositions is a list, e.g., -p [1,2,4]  

algorithm  1=MaxLike
           2=NNet(backprop)
           3=NNet(congrad)
           4=SVM

If the input file is named 

         path/filenbasename.ext then

The output classification file is named 

         path/filebasename_class.ext

the class probabilities output file is named

         path/filebasename_classprobs.ext
         
and the test results file is named

         path/filebasename_<classifier>.tst
--------------------------------------------------------''' %sys.argv[0]
    options, args = getopt.getopt(sys.argv[1:],'hnPp:a:L:')
    pos = None
    probs = False   
    L = 8
    graphics = True
    trainalg = 1
    for option, value in options:
        if option == '-h':
            print usage
            return
        elif option == '-p':
            pos = eval(value)
        elif option == '-n':
            graphics = False            
        elif option == '-a':
            trainalg = eval(value)
        elif option == '-L':
            L = eval(value)    
        elif option == '-P':
            probs = True                              
    if len(args) != 2: 
        print 'Incorrect number of arguments'
        print usage
        sys.exit(1)      
    if trainalg == 1:
        algorithm = 'MaxLike'
    elif trainalg == 2:
        algorithm = 'NNet(Backprop)'
    elif trainalg == 3:
        algorithm =  'NNet(Congrad)'
    elif trainalg == 4:
        algorithm = 'SVM'              
    infile = args[0]  
    trnfile = args[1]      
    gdal.AllRegister() 
    if infile:                   
        inDataset = gdal.Open(infile,GA_ReadOnly)
        cols = inDataset.RasterXSize
        rows = inDataset.RasterYSize    
        bands = inDataset.RasterCount
        projection = inDataset.GetProjection()
        geotransform = inDataset.GetGeoTransform()
        if geotransform is not None:
            gt = list(geotransform) 
        else:
            print 'No geotransform available'
            return       
        imsr = osr.SpatialReference()  
        imsr.ImportFromWkt(projection)    
    else:
        return  
    if pos is None: 
        pos = range(1,bands+1)
    N = len(pos)    
    rasterBands = [] 
    for b in pos:
        rasterBands.append(inDataset.GetRasterBand(b))     
#  output files
    path = os.path.dirname(infile)
    basename = os.path.basename(infile)
    root, ext = os.path.splitext(basename)
    outfile = '%s/%s_class%s'%(path,root,ext)  
    tstfile = '%s/%s_%s.tst'%(path,root,algorithm)            
    if (trainalg in (2,3,4)) and probs:
#      class probabilities file
        probfile = '%s/%s_classprobs%s'%(path,root,ext) 
    else:
        probfile = None        
#  training data        
    trnDriver = ogr.GetDriverByName('ESRI Shapefile')
    trnDatasource = trnDriver.Open(trnfile,0)
    trnLayer = trnDatasource.GetLayer() 
    trnsr = trnLayer.GetSpatialRef()             
#  coordinate transformation from training to image projection   
    ct = osr.CoordinateTransformation(trnsr,imsr) 
#  number of classes    
    K = 1
    feature = trnLayer.GetNextFeature() 
    while feature:
        classid = feature.GetField('CLASS_ID')
        if int(classid)>K:
            K = int(classid)
        feature = trnLayer.GetNextFeature() 
    trnLayer.ResetReading()    
    K += 1       
#  es kann losgehen    
    print '========================='
    print 'supervised classification'
    print '========================='
    print time.asctime()    
    print 'image:     '+infile
    print 'training:  '+trnfile  
    print 'algorithm: '+algorithm             
#  loop through the polygons    
    Gs = [] # train observations
    ls = [] # class labels
    classnames = '{unclassified'
    classids = set()
    print 'reading training data...'
    for i in range(trnLayer.GetFeatureCount()):
        feature = trnLayer.GetFeature(i)
        classid = str(feature.GetField('CLASS_ID'))
        classname  = feature.GetField('CLASS_NAME')
        if classid not in classids:
            classnames += ',   '+ classname
        classids = classids | set(classid)     
#      label for this ROI           
        l = [0 for i in range(K)]
        l[int(classid)] = 1.0
        polygon = feature.GetGeometryRef()
#      transform to same projection as image        
        polygon.Transform(ct)  
#      convert to a Shapely object            
        poly = shapely.wkt.loads(polygon.ExportToWkt())
#      transform the boundary to pixel coords in numpy        
        bdry = np.array(poly.boundary) 
        bdry[:,0] = bdry[:,0]-gt[0]
        bdry[:,1] = bdry[:,1]-gt[3]
        GT = np.mat([[gt[1],gt[2]],[gt[4],gt[5]]])
        bdry = bdry*np.linalg.inv(GT) 
#      polygon in pixel coords        
        polygon1 = asPolygon(bdry)
#      raster over the bounding rectangle        
        minx,miny,maxx,maxy = map(int,list(polygon1.bounds))  
        pts = [] 
        for i in range(minx,maxx+1):
            for j in range(miny,maxy+1): 
                pts.append((i,j))             
        multipt =  MultiPoint(pts)   
#      intersection as list              
        intersection = np.array(multipt.intersection(polygon1),dtype=np.int).tolist()
#      cut out the bounded image cube               
        cube = np.zeros((maxy-miny+1,maxx-minx+1,len(rasterBands)))
        k=0
        for band in rasterBands:
            cube[:,:,k] = band.ReadAsArray(minx,miny,maxx-minx+1,maxy-miny+1)
            k += 1
#      get the training vectors
        for (x,y) in intersection:         
            Gs.append(cube[y-miny,x-minx,:])
            ls.append(l)   
        polygon = None
        polygon1 = None            
        feature.Destroy()  
    trnDatasource.Destroy() 
    classnames += '}'
    m = len(ls)       
    print str(m) + ' training pixel vectors were read in' 
    Gs = np.array(Gs) 
    ls = np.array(ls)
#  stretch the pixel vectors to [-1,1] (for ffn)
    maxx = np.max(Gs,0)
    minx = np.min(Gs,0)
    for j in range(N):
        Gs[:,j] = 2*(Gs[:,j]-minx[j])/(maxx[j]-minx[j]) - 1.0   
#  random permutation of training data
    idx = np.random.permutation(m)
    Gs = Gs[idx,:] 
    ls = ls[idx,:]             
#  setup output datasets 
    driver = inDataset.GetDriver() 
    outDataset = driver.Create(outfile,cols,rows,1,GDT_Byte) 
    projection = inDataset.GetProjection()
    geotransform = inDataset.GetGeoTransform()
    if geotransform is not None:
        outDataset.SetGeoTransform(tuple(gt))
    if projection is not None:
        outDataset.SetProjection(projection) 
    outBand = outDataset.GetRasterBand(1) 
    if probfile:   
        probDataset = driver.Create(probfile,cols,rows,K,GDT_Byte) 
        if geotransform is not None:
            probDataset.SetGeoTransform(tuple(gt))
        if projection is not None:
            probDataset.SetProjection(projection)  
        probBands = [] 
        for k in range(K):
            probBands.append(probDataset.GetRasterBand(k+1))         
#  initialize classifier  
    if   trainalg == 1:
        classifier = sc.Maxlike(Gs,ls)
    elif trainalg == 2:
        classifier = sc.Ffnbp(Gs,ls,L)
    elif trainalg == 3:
        classifier = sc.Ffncg(Gs,ls,L)
    elif trainalg == 4:
        classifier = sc.Svm(Gs,ls)         
#  train it            
    print 'training on %i pixel vectors...' % np.shape(Gs)[0]
    start = time.time()
    result = classifier.train()
    print 'elapsed time %s' %str(time.time()-start) 
    if result:
        if (trainalg in [2,3]) and graphics:
            cost = np.log10(result)  
            ymax = np.max(cost)
            ymin = np.min(cost) 
            xmax = len(cost)      
            plt.plot(range(xmax),cost,'k')
            plt.axis([0,xmax,ymin-1,ymax])
            plt.title('Log(Cross entropy)')
            plt.xlabel('Epoch')   
            plt.show()
#      classify the image           
        print 'classifying...'
        start = time.time()
        tile = np.zeros((cols,N),dtype=np.float32)    
        for row in range(rows):
            for j in range(N):
                tile[:,j] = rasterBands[j].ReadAsArray(0,row,cols,1)
                tile[:,j] = 2*(tile[:,j]-minx[j])/(maxx[j]-minx[j]) - 1.0               
            cls, Ms = classifier.classify(tile)  
            outBand.WriteArray(np.reshape(cls,(1,cols)),0,row)
            if probfile:
                Ms = np.byte(Ms*255)
                for k in range(K):
                    probBands[k].WriteArray(np.reshape(Ms[k,:],(1,cols)),0,row)
        outBand.FlushCache()
        print 'elapsed time %s' %str(time.time()-start)
        outDataset = None
        inDataset = None      
        if probfile:
            for probBand in probBands:
                probBand.FlushCache() 
            probDataset = None
            print 'class probabilities written to: %s'%probfile   
        K =  ls.shape[1]+1                     
        print 'thematic map written to: %s'%outfile
    else:
        print 'an error occured' 
        return 
#  cross-validation
    start = time.time()
    rc = Client()   
    print 'submitting cross-validation to %i IPython engines'%len(rc)  
    m = np.shape(Gs)[0]
    traintest = []
    for i in range(10):
        sl = slice(i*m//10,(i+1)*m//10)
        traintest.append( (np.delete(Gs,sl,0),np.delete(ls,sl,0), \
                                     Gs[sl,:],ls[sl,:],L,trainalg) )
    v = rc[:]   
    v.execute('import auxil.supervisedclass as sc') 
    result = v.map(crossvalidate,traintest).get()   
    print 'parallel execution time: %s' %str(time.time()-start)      
    print 'misclassification rate: %f' %np.mean(result)
    print 'standard deviation:     %f' %np.std(result)         
Exemplo n.º 36
0
    def setup(self, *, map_size_x=map_size_x, map_size_y=map_size_y, n_obstacles=0, reset_always=True,
              max_timestep=1000, threshold_dist=40, threshold_vel=4, reward_sparsity=True):

        self.map_size_x = 160
        self.map_size_y = 240
        self.map_min_x = - self.map_size_x * 0.1
        self.map_min_y = - self.map_size_y * 0.1
        self.map_max_x = self.map_size_x * 1.1
        self.map_max_y = self.map_size_y * 1.1
        self.reset_always = False
        self.max_timestep = 1500
        self.threshold_dist = 15
        self.threshold_vel = 4

        self.n_obstacles = 9

        # True is sparse reward, False is a dense reward
        self.reward_sparsity = reward_sparsity
        self.dense_reward = not reward_sparsity

        self.get_observation = self.get_airsim_observation

        self._take_action = self._airsim_action  # Discrete cartesian

        # Obstacles of the block env
        scaled_points = \
            [[160., 0., 140., 20.],
             [20., 0., 0., 20.],
             [30., 60., 20., 70.],
             [130., 52.5, 110., 70.],
             [80., 80., 60., 100.],
             [160., 100., 120., 140.],
             [80., 140., 60., 160.],
             [30., 170., 20., 180.],
             [130., 170., 110., 190.],
             [160., 220., 140., 240.],
             [20., 220., 0., 240.]]

        for obstacle in scaled_points:
            x0, y0 = obstacle[2], obstacle[1]
            w = obstacle[0] - obstacle[2]
            h = obstacle[3] - obstacle[1]

            p1, p2, p3, p4 = [(x0, y0), (x0 + w, y0), (x0 + w, y0 + h), (x0, y0 + h)]
            # print([(x0, y0), (x0 + w, y0), (x0 + w, y0 + h), (x0, y0 + h)])
            self.add_obstacle(asPolygon([p1, p2, p3, p4]))




        # Added these 2 lines to reduce number of polys and increase performance
        # self.obstacles = cascaded_union(self.obstacles)
        # self.prep_obstacles = [prep(polygon) for polygon in self.obstacles]
        self.prep_obstacles = self.obstacles
        self.generate_map()

        self.s['origin_x'], self.s['origin_y'] = [35.], [117.39583333]  # Scaled old [0, 0]
        self.s['target_x'], self.s['target_y'] = [35.], [117.39583333]  # Scaled old [0, 0]

        self.client = MultirotorClient()
        self.client.confirmConnection()
        self.client.enableApiControl(True)
        self.client.armDisarm(True)

        self.move_to(35. , 117.39583333)

        self.client.hover()