def main():
    option_parser, opts, args = parse_command_line_parameters(**script_info)

    data, comments = parse_mapping_file_to_dict(opts.input_path)
    column_headers = []
    if ',' not in opts.column:
        column_data = []
        column_name = opts.column
        for i in data:
            if column_name not in data[i]:
                raise ValueError(
                    "No column: '%s' in the mapping file. Existing columns are: %s" %
                    (column_name, data[i].keys()))

            try:
                column_data.append(float(data[i][opts.column]))
            except ValueError:
                raise ValueError(
                    "All the values in the column '%s' must be numeric but '%s' has '%s'" %
                    (column_name, i, data[i][column_name]))

            column_headers.append(i)
        dtx_mtx = compute_distance_matrix_from_metadata(column_data)
    else:
        latitudes = []
        longitudes = []
        try:
            latitude, longitude = opts.column.split(',')
        except ValueError:
            raise ValueError(
                "This script accepts a maximum of 2 colums separated by comma and you passed: %s" %
                (opts.column))

        for i in data:
            if latitude not in data[i] or longitude not in data[i]:
                raise ValueError(
                    "One of these columns or both do not exist: '%s' or '%s' in the mapping file. Existing columns are: %s" %
                    (latitude, longitude, data[i].keys()))

            try:
                latitudes.append(float(data[i][latitude]))
                longitudes.append(float(data[i][longitude]))
            except ValueError:
                raise ValueError(
                    "All the values in the columnd '%s' & '%s' must be numeric but '%s' has '%s'" %
                    (latitude, longitude, i, data[i][column_name]))

            column_headers.append(i)

        dtx_mtx = calculate_dist_vincenty(latitudes, longitudes)

    dtx_txt = format_distance_matrix(column_headers, dtx_mtx)

    outfilepath = os.path.join(opts.output_fp)
    f = open(outfilepath, 'w')
    f.write(dtx_txt)
    f.close()
Пример #2
0
def main():
    option_parser, opts, args = parse_command_line_parameters(**script_info)

    data, comments = parse_mapping_file_to_dict(opts.input_path)
    column_headers = []
    if ',' not in opts.column:
        column_data = []
        column_name = opts.column
        for i in data:
            if column_name not in data[i]:
                raise ValueError(
                    "No column: '%s' in the mapping file. Existing columns are: %s"
                    % (column_name, data[i].keys()))

            try:
                column_data.append(float(data[i][opts.column]))
            except ValueError:
                raise ValueError(
                    "All the values in the column '%s' must be numeric but '%s' has '%s'"
                    % (column_name, i, data[i][column_name]))

            column_headers.append(i)
        dtx_mtx = compute_distance_matrix_from_metadata(column_data)
    else:
        latitudes = []
        longitudes = []
        try:
            latitude, longitude = opts.column.split(',')
        except ValueError:
            raise ValueError(
                "This script accepts a maximum of 2 colums separated by comma and you passed: %s"
                % (opts.column))

        for i in data:
            if latitude not in data[i] or longitude not in data[i]:
                raise ValueError(
                    "One of these columns or both do not exist: '%s' or '%s' in the mapping file. Existing columns are: %s"
                    % (latitude, longitude, data[i].keys()))

            try:
                latitudes.append(float(data[i][latitude]))
                longitudes.append(float(data[i][longitude]))
            except ValueError:
                raise ValueError(
                    "All the values in the columnd '%s' & '%s' must be numeric but '%s' has '%s'"
                    % (latitude, longitude, i, data[i][column_name]))

            column_headers.append(i)

        dtx_mtx = calculate_dist_vincenty(latitudes, longitudes)

    dtx_txt = format_distance_matrix(column_headers, dtx_mtx)

    outfilepath = os.path.join(opts.output_fp)
    f = open(outfilepath, 'w')
    f.write(dtx_txt)
    f.close()
    def test_calculate_dist_vincenty(self):
        exp_out = array([
            [
                0.0, 10709578.387, 0.0, 0.0, 7154900.607, 7094106.828,
                6681852.331, 6626434.332, 7154900.607, 7154900.607
            ],
            [
                10709578.387, 0.0, 10709578.387, 10709578.387, 5877643.846,
                5831009.412, 7789599.475, 7718017.604, 5877643.846, 5877643.846
            ],
            [
                0.0, 10709578.387, 0.0, 0.0, 7154900.607, 7094106.828,
                6681852.331, 6626434.332, 7154900.607, 7154900.607
            ],
            [
                0.0, 10709578.387, 0.0, 0.0, 7154900.607, 7094106.828,
                6681852.331, 6626434.332, 7154900.607, 7154900.607
            ],
            [
                7154900.607, 5877643.846, 7154900.607, 7154900.607, 0.0,
                110574.389, 10001965.729, 9890271.864, 0.0, 0.0
            ],
            [
                7094106.828, 5831009.412, 7094106.828, 7094106.828, 110574.389,
                0.0, 9891391.341, 9779697.476, 110574.389, 110574.389
            ],
            [
                6681852.331, 7789599.475, 6681852.331, 6681852.331,
                10001965.729, 9891391.341, 0.0, 111693.865, 10001965.729,
                10001965.729
            ],
            [
                6626434.332, 7718017.604, 6626434.332, 6626434.332,
                9890271.864, 9779697.476, 111693.865, 0.0, 9890271.864,
                9890271.864
            ],
            [
                7154900.607, 5877643.846, 7154900.607, 7154900.607, 0.0,
                110574.389, 10001965.729, 9890271.864, 0.0, 0.0
            ],
            [
                7154900.607, 5877643.846, 7154900.607, 7154900.607, 0.0,
                110574.389, 10001965.729, 9890271.864, 0.0, 0.0
            ]
        ])

        res_out = calculate_dist_vincenty(self.latitudes, self.longitudes)

        assert_almost_equal(res_out, exp_out)
 def test_calculate_dist_vincenty(self):
   exp_out = array([[0.0, 10709578.387, 0.0, 0.0, 7154900.607, 7094106.828, 6681852.331, 6626434.332, 7154900.607, 7154900.607],
       [10709578.387, 0.0, 10709578.387, 10709578.387, 5877643.846, 5831009.412, 7789599.475, 7718017.604, 5877643.846, 5877643.846],
       [0.0, 10709578.387, 0.0, 0.0, 7154900.607, 7094106.828, 6681852.331, 6626434.332, 7154900.607, 7154900.607],
       [0.0, 10709578.387, 0.0, 0.0, 7154900.607, 7094106.828, 6681852.331, 6626434.332, 7154900.607, 7154900.607],
       [7154900.607, 5877643.846, 7154900.607, 7154900.607, 0.0, 110574.389, 10001965.729, 9890271.864, 0.0, 0.0],
       [7094106.828, 5831009.412, 7094106.828, 7094106.828, 110574.389, 0.0, 9891391.341, 9779697.476, 110574.389, 110574.389],
       [6681852.331, 7789599.475, 6681852.331, 6681852.331, 10001965.729, 9891391.341, 0.0, 111693.865, 10001965.729, 10001965.729],
       [6626434.332, 7718017.604, 6626434.332, 6626434.332, 9890271.864, 9779697.476, 111693.865, 0.0, 9890271.864, 9890271.864],
       [7154900.607, 5877643.846, 7154900.607, 7154900.607, 0.0, 110574.389, 10001965.729, 9890271.864, 0.0, 0.0],
       [7154900.607, 5877643.846, 7154900.607, 7154900.607, 0.0, 110574.389, 10001965.729, 9890271.864, 0.0, 0.0]])
   
   res_out = calculate_dist_vincenty(self.latitudes, self.longitudes)
   
   self.assertFloatEqual(res_out, exp_out)