qgreenland/util/vector.py
def points_txt_to_shape(
        in_filepath, out_filepath, *,
        header, delimiter, field_names, x_field, y_field
):
    """Convert a textfile of points to a vector data file.

    TODO: `ogr2ogr` can operate on delimited text files, but not without a
    header. Perhaps this task could add header to files that don't have one? Or
    pass on the correct info to `ogr2ogr` about what each column represents
    positionally? An example ogr2ogr command:

    $ ogr2ogr -oo X_POSSIBLE_NAMES=lon -oo Y_POSSIBLE_NAMES=lat -a_srs "EPSG:4326" out.shp in.csv  # noqa
    """
    df = pd.read_table(in_filepath, header=header, delimiter=delimiter)

    # If the header is 'none', there is no header and it must be defiend via
    # `field_names`
    if header is None:
        if not field_names or len(field_names) != len(df.columns):
            raise QgrRuntimeError(
                '`field_names` must be a list of field names corresponding to each of the'
                f' {len(df.columns)} columns in {in_filepath}. Got {field_names=}'
            )

        df = df.rename(columns={idx: field_names[idx] for idx in range(len(field_names))})

    gdf = gpd.GeoDataFrame(df, geometry=gpd.points_from_xy(df[x_field], df[y_field]))
    gdf.to_file(out_filepath, driver='GPKG')
Convert a textfile of points to a vector data file.

    TODO: `ogr2ogr` can operate on delimited text files, but not without a
    header. Perhaps this task could add header to files that don't have one? Or
    pass on the correct info to `ogr2ogr` about what each column represents
    positionally? An example ogr2ogr command:

    $ ogr2ogr -oo X_POSSIBLE_NAMES=lon -oo Y_POSSIBLE_NAMES=lat -a_srs "EPSG:4326" out.shp in.csv  # noqa
    
Convert a textfile of points to a vector data file.
TODO: `ogr2ogr` can operate on delimited text files, but not without a
header. Perhaps this task could add header to files that don't have one. Or
pass on the correct info to `ogr2ogr` about what each column represents
positionally. An example ogr2ogr command.

$ ogr2ogr -oo X_POSSIBLE_NAMES=lon -oo Y_POSSIBLE_NAMES=lat -a_srs "EPSG:4326" out.shp in.csv  # noqa
Convert a textfile of points to a vector data file . TODO : ` ogr2ogr ` can operate on delimited text files but not without a header . Perhaps this task could add header to files that don ' t have one . Or pass on the correct info to ` ogr2ogr ` about what each column represents positionally . An example ogr2ogr command . $ ogr2ogr - oo X_POSSIBLE_NAMES = lon - oo Y_POSSIBLE_NAMES = lat - a_srs " EPSG : 4326 " out . shp in . csv # noqa
def points_txt_to_shape(
        in_filepath, out_filepath, *,
        header, delimiter, field_names, x_field, y_field
):
    df = pd.read_table(in_filepath, header=header, delimiter=delimiter)
    if header is None:
        if not field_names or len(field_names) != len(df.columns):
            raise QgrRuntimeError(
                '`field_names` must be a list of field names corresponding to each of the'
                f' {len(df.columns)} columns in {in_filepath}. Got {field_names=}'
            )
        df = df.rename(columns={idx: field_names[idx] for idx in range(len(field_names))})
    gdf = gpd.GeoDataFrame(df, geometry=gpd.points_from_xy(df[x_field], df[y_field]))
    gdf.to_file(out_filepath, driver='GPKG')
def points_txt_to_shape ( in_filepath , out_filepath , * , header , delimiter , field_names , x_field , y_field ) : df = pd . read_table ( in_filepath , header = header , delimiter = delimiter ) if header is None : if not field_names or len ( field_names ) != len ( df . columns ) : raise QgrRuntimeError ( '`field_names` must be a list of field names corresponding to each of the' f' {len(df.columns)} columns in {in_filepath}. Got {field_names=}' ) df = df . rename ( columns = { idx : field_names [ idx ] for idx in range ( len ( field_names ) ) } ) gdf = gpd . GeoDataFrame ( df , geometry = gpd . points_from_xy ( df [ x_field ] , df [ y_field ] ) ) gdf . to_file ( out_filepath , driver = 'GPKG' )
Convert a textfile of points to a vector data file.
Convert a textfile of points to a vector data file .
"""Convert a textfile of points to a vector data file.

    TODO: `ogr2ogr` can operate on delimited text files, but not without a
    header. Perhaps this task could add header to files that don't have one? Or
    pass on the correct info to `ogr2ogr` about what each column represents
    positionally? An example ogr2ogr command:

    $ ogr2ogr -oo X_POSSIBLE_NAMES=lon -oo Y_POSSIBLE_NAMES=lat -a_srs "EPSG:4326" out.shp in.csv  # noqa
    """ # If the header is 'none', there is no header and it must be defiend via # `field_names`
in_filepath
out_filepath
header
delimiter
field_names
x_field
y_field