Exemple #1
0
    def test_split_list_empty(self):
        from shapeanalysis.process_data import split_list

        original = []
        split_indexes = []
        actual = split_list(original, split_indexes)
        expected = [[]]

        self.assertIsInstance(actual, types.GeneratorType)
        self.assertEqual(expected, list(actual))
Exemple #2
0
    def test_split_list_split2_no_leading(self):
        from shapeanalysis.process_data import split_list

        original = [1, 2, 3, 4, 5]
        split_indexes = [2, 4]
        actual = split_list(original, split_indexes)
        expected = [[1, 2], [3, 4], [5]]

        self.assertIsInstance(actual, types.GeneratorType)
        self.assertEqual(expected, list(actual))
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)