def main():
    args = parse_arguments(sys.argv[1:])
    configure_logger()

    # Read in the given shapefile
    logger.info('Reading file...')
    sf = shapefile.Reader(args.shapefile)
    shapeRecs = sf.shapeRecords()

    # Process the shape records
    logger.info('Processing...')
    # Get individual shape points separated, as each shape can include multiple parts
    point_list = [(points, rec) for rec in shapeRecs
                  for points in split_list(rec.shape.points, rec.shape.parts)
                  ][:100]
    total = len(point_list)
    matches = []
    for i, (points, rec) in enumerate(point_list):
        logger.debug(f'Processing: {i}/{total}')
        # Check if the shape matches the search criteria
        sig_points = significant_points(points, args.inline_tolerance)
        if has_box(points, args.inline_tolerance, args.angle_tolerance):
            matches.append((rec, sig_points))

    rec_data = []
    centroid_points = []
    for match_rec, points in matches:
        centroid_point = centroid(points)
        centroid_points.append(centroid_point)
        rec_data.append([match_rec, centroid_point])

    distances = nearest_distances(centroid_points, 2)

    main = []
    for i in range(len(rec_data)):
        pid = rec_data[i][0].record[0]
        near_dists = distances[rec_data[i][1].astype(np.float).tobytes()]
        main.append((pid, near_dists[0], near_dists[1]))

    # for _, c_point in rec_data:
    #     rec_data
    # for i in range(len(rec_data)):
    #     rec_data[i] += list(distances[rec_data[i][1].astype(np.float).tobytes()])

    # import csv
    # with open('output.csv', 'w', newline='') as outputfile:
    #     csv_writer = csv.writer(outputfile)
    #     for x in rec_data:
    #         csv_writer.writerow([x[0].record[0]] + [str(tuple(x[1]))] + x[2:])

    with database.connection(args.output) as conn:
        database.create_database(conn)
        database.insert_main(conn, main)
Beispiel #2
0
    def test_has_box_one_right_angle_found(self):
        from shapeanalysis.process_data import has_box

        input_points = [
            (0, 0),
            (15, 0),
            (0, 15),
            (0, 0),
        ]
        tolerance = 0.6
        angle_tolerance = 0.03

        actual = has_box(input_points, tolerance, angle_tolerance)
        self.assertFalse(actual)
Beispiel #3
0
    def test_has_box_parallelogram(self):
        from shapeanalysis.process_data import has_box

        input_points = [
            (0, 1),
            (15, 0),
            (30, 1),
            (15, 2),
            (0, 1),
        ]
        tolerance = 0.6
        angle_tolerance = 0.03

        actual = has_box(input_points, tolerance, angle_tolerance)
        self.assertFalse(actual)
Beispiel #4
0
    def test_has_box_semicircle_single(self):
        from shapeanalysis.process_data import has_box

        input_points = [
            (0, 0),
            (15, 0),
            (15, 15),
            (0, 15),
            (0, 0),
        ]
        tolerance = 0.6
        angle_tolerance = 0.03

        actual = has_box(input_points, tolerance, angle_tolerance)
        self.assertTrue(actual)
Beispiel #5
0
    def test_has_box_two_found_both_out_of_size(self):
        from shapeanalysis.process_data import has_box

        input_points = [
            (0, 0),
            (0, 85),
            (1, 85),
            (2, 1),
            (2, 0),
            (0, 0),
        ]
        tolerance = 0.6
        angle_tolerance = 0.03

        actual = has_box(input_points, tolerance, angle_tolerance)
        self.assertFalse(actual)
Beispiel #6
0
    def test_has_box_two_found(self):
        from shapeanalysis.process_data import has_box

        input_points = [
            (0, 0),
            (0, 20),
            (10, 20),
            (20, 10),
            (20, 0),
            (0, 0),
        ]
        tolerance = 0.6
        angle_tolerance = 0.03

        actual = has_box(input_points, tolerance, angle_tolerance)
        self.assertTrue(actual)
Beispiel #7
0
    def test_has_box_non_found(self):
        from shapeanalysis.process_data import has_box

        input_points = [
            (0.5858, 1.414),
            (2, 2),
            (3.414, 1.414),
            (4, 0),
            (0, 0),
            (0.5858, 1.414),
        ]
        tolerance = 0.6
        angle_tolerance = 0.03

        actual = has_box(input_points, tolerance, angle_tolerance)
        self.assertFalse(actual)
Beispiel #8
0
    def test_has_box_two_found_both_out_change_bounds(self):
        from shapeanalysis.process_data import has_box

        input_points = [
            (0, 0),
            (0, 40),
            (20, 40),
            (30, 20),
            (30, 0),
            (0, 0),
        ]
        tolerance = 0.6
        angle_tolerance = 0.03
        low = 32
        high = 38

        actual = has_box(input_points, tolerance, angle_tolerance, low, high)
        self.assertFalse(actual)
Beispiel #9
0
    def test_has_box_two_found_high_in_change_bounds(self):
        from shapeanalysis.process_data import has_box

        input_points = [
            (0, 0),
            (0, 90),
            (5, 90),
            (6, 20),
            (6, 0),
            (0, 0),
        ]
        tolerance = 0.6
        angle_tolerance = 0.03
        low = 10
        high = 100

        actual = has_box(input_points, tolerance, angle_tolerance, low, high)
        self.assertTrue(actual)