Exemplo n.º 1
0
def main():
    parser = get_parser()
    args = parser.parse_args()

    grid_name = args.grid_name
    clon = args.center_longitude
    clat = args.center_latitude
    pixel_size_x = args.pixel_size_x
    pixel_size_y = args.pixel_size_y
    if pixel_size_y > 0 and not args.dont_touch_ysize: pixel_size_y *= -1
    grid_width = args.grid_width
    grid_height = args.grid_height

    if clon < -180 or clon > 180:
        print "ERROR: Center longitude must be between -180 and 180 degrees"
        return -1
    if clat < -90 or clat > 90:
        print "ERROR: Center latitude must be between -90 and 90 degrees"
        return -1

    proj_str = determine_projection(clon, clat, args.proj_str)
    p = Proj(proj_str)
    origin_x = p(clon, clat)[0] + (grid_width / 2.0 * pixel_size_x * -1)
    origin_y = p(clon, clat)[1] + (grid_height / 2.0 * pixel_size_y * -1)
    if p.is_latlong():
        # Origin is in degrees so we need to add a unit string to it
        origin_x = "%0.3fdeg" % origin_x
        origin_y = "%0.3fdeg" % origin_y
    else:
        origin_lon, origin_lat = p(origin_x, origin_y, inverse=True)
        origin_x = "%0.3fdeg" % origin_lon
        origin_y = "%0.3fdeg" % origin_lat

    valid_config_line = CONFIG_LINE_FORMAT % {
        "grid_name": grid_name,
        "proj4_str": proj_str,
        "origin_x": origin_x,
        "origin_y": origin_y,
        "pixel_size_x": pixel_size_x,
        "pixel_size_y": pixel_size_y,
        "width": grid_width,
        "height": grid_height,
    }
    print valid_config_line
Exemplo n.º 2
0
def main():
    parser = get_parser()
    args = parser.parse_args()

    grid_name = args.grid_name
    clon = args.center_longitude
    clat = args.center_latitude
    pixel_size_x = args.pixel_size_x
    pixel_size_y = args.pixel_size_y
    if pixel_size_y > 0 and not args.dont_touch_ysize: pixel_size_y *= -1
    grid_width = args.grid_width
    grid_height = args.grid_height

    if clon < -180 or clon > 180:
        print("ERROR: Center longitude must be between -180 and 180 degrees")
        return -1
    if clat < -90 or clat > 90:
        print("ERROR: Center latitude must be between -90 and 90 degrees")
        return -1

    proj_str = determine_projection(clon, clat, args.proj_str)
    p = Proj(proj_str)
    origin_x = p(clon, clat)[0] + (grid_width / 2.0 * pixel_size_x * -1)
    origin_y = p(clon, clat)[1] + (grid_height / 2.0 * pixel_size_y * -1)
    if p.is_latlong():
        # Origin is in degrees so we need to add a unit string to it
        origin_x = "%0.5fdeg" % origin_x
        origin_y = "%0.5fdeg" % origin_y
    else:
        origin_lon, origin_lat = p(origin_x, origin_y, inverse=True)
        origin_x = "%0.5fdeg" % origin_lon
        origin_y = "%0.5fdeg" % origin_lat

    valid_config_line = CONFIG_LINE_FORMAT % {
            "grid_name": grid_name,
            "proj4_str": proj_str,
            "origin_x": origin_x,
            "origin_y": origin_y,
            "pixel_size_x": pixel_size_x,
            "pixel_size_y": pixel_size_y,
            "width": grid_width,
            "height": grid_height,
            }
    print(valid_config_line)
Exemplo n.º 3
0
def parse_proj4_config_line(grid_name, parts):
    """Return a dictionary of information for a specific PROJ.4 grid from
    a grid configuration line. ``parts`` should be every comma-separated
    part of the line including the ``grid_name``.
    """
    info = {}

    proj4_str = parts[2]
    # Test to make sure the proj4_str is valid in pyproj's eyes
    try:
        p = Proj(proj4_str)
        del p
    except StandardError:
        LOG.error("Invalid proj4 string in '%s' : '%s'" % (grid_name, proj4_str))
        raise

    # Some parts can be None, but not all
    try:
        static = True
        if parts[3] == "None" or parts[3] == "":
            static = False
            grid_width = None
        else:
            grid_width = int(parts[3])

        if parts[4] == "None" or parts[4] == "":
            static = False
            grid_height = None
        else:
            grid_height = int(parts[4])

        if parts[5] == "None" or parts[5] == "":
            LOG.warning("Grid '%s' may not process properly due to unspecified pixel size", grid_name)
            static = False
            pixel_size_x = None
        else:
            pixel_size_x = float(parts[5])

        if parts[6] == "None" or parts[6] == "":
            LOG.warning("Grid '%s' may not process properly due to unspecified pixel size", grid_name)
            static = False
            pixel_size_y = None
        else:
            pixel_size_y = float(parts[6])

        convert_xorigin_to_meters = False
        if parts[7] == "None" or parts[7] == "":
            static = False
            grid_origin_x = None
        else:
            grid_origin_x, convert_xorigin_to_meters = _parse_meter_degree_param(parts[7])

        convert_yorigin_to_meters = False
        if parts[8] == "None" or parts[8] == "":
            static = False
            grid_origin_y = None
        else:
            grid_origin_y, convert_yorigin_to_meters = _parse_meter_degree_param(parts[8])
    except StandardError:
        LOG.error("Could not parse proj4 grid configuration: '%s'" % (grid_name,))
        raise

    if (pixel_size_x is None and pixel_size_y is not None) or (pixel_size_x is not None and pixel_size_y is None):
        LOG.error("Both or neither pixel sizes must be specified for '%s'" % grid_name)
        raise ValueError("Both or neither pixel sizes must be specified for '%s'" % grid_name)
    if (grid_width is None and grid_height is not None) or (grid_width is not None and grid_height is None):
        LOG.error("Both or neither grid sizes must be specified for '%s'" % grid_name)
        raise ValueError("Both or neither grid sizes must be specified for '%s'" % grid_name)
    if (grid_origin_x is None and grid_origin_y is not None) or (grid_origin_x is not None and grid_origin_y is None):
        LOG.error("Both or neither grid origins must be specified for '%s'" % grid_name)
        raise ValueError("Both or neither grid origins must be specified for '%s'" % grid_name)
    if grid_width is None and pixel_size_x is None:
        LOG.error("Either grid size or pixel size must be specified for '%s'" % grid_name)
        raise ValueError("Either grid size or pixel size must be specified for '%s'" % grid_name)
    if convert_xorigin_to_meters != convert_yorigin_to_meters:
        LOG.error("Grid origin parameters must be in the same units (meters vs degrees)")
        raise ValueError("Grid origin parameters must be in the same units (meters vs degrees)")

    # Convert any parameters from degrees to meters (we already made sure both need to be converted above)
    p = Proj(proj4_str)
    if convert_xorigin_to_meters and not p.is_latlong():
        meters_x, meters_y = p(grid_origin_x, grid_origin_y)
        LOG.debug(
            "Converted grid '%s' origin from (lon: %f, lat: %f) to (x: %f, y: %f)",
            grid_name,
            grid_origin_x,
            grid_origin_y,
            meters_x,
            meters_y,
        )
        grid_origin_x, grid_origin_y = meters_x, meters_y
    elif not convert_xorigin_to_meters and (grid_origin_x is not None and p.is_latlong()):
        LOG.error("Lat/Lon grid '%s' must have its origin in degrees", grid_name)
        raise ValueError("Lat/Lon grid '%s' must have its origin in degrees" % (grid_name,))

    proj4_dict = get_proj4_info(proj4_str)

    info.update(**proj4_dict)
    info["grid_kind"] = "proj4"
    info["static"] = static
    info["proj4_str"] = proj4_str
    info["pixel_size_x"] = pixel_size_x
    info["pixel_size_y"] = pixel_size_y
    info["grid_origin_x"] = grid_origin_x
    info["grid_origin_y"] = grid_origin_y
    info["grid_width"] = grid_width
    info["grid_height"] = grid_height

    return info
Exemplo n.º 4
0
def parse_proj4_config_line(grid_name, parts):
    """Return a dictionary of information for a specific PROJ.4 grid from
    a grid configuration line. ``parts`` should be every comma-separated
    part of the line including the ``grid_name``.
    """
    info = {}

    proj4_str = parts[2]
    # Test to make sure the proj4_str is valid in pyproj's eyes
    try:
        p = Proj(proj4_str)
        del p
    except ValueError:
        LOG.error("Invalid proj4 string in '%s' : '%s'" % (grid_name, proj4_str))
        raise

    # Some parts can be None, but not all
    try:
        static = True
        if parts[3] == "None" or parts[3] == '':
            static = False
            grid_width = None
        else:
            grid_width = int(parts[3])

        if parts[4] == "None" or parts[4] == '':
            static = False
            grid_height = None
        else:
            grid_height = int(parts[4])

        if parts[5] == "None" or parts[5] == '':
            LOG.warning("Grid '%s' may not process properly due to unspecified pixel size", grid_name)
            static = False
            pixel_size_x = None
        else:
            pixel_size_x = float(parts[5])

        if parts[6] == "None" or parts[6] == '':
            LOG.warning("Grid '%s' may not process properly due to unspecified pixel size", grid_name)
            static = False
            pixel_size_y = None
        else:
            pixel_size_y = float(parts[6])

        convert_xorigin_to_meters = False
        if parts[7] == "None" or parts[7] == '':
            static = False
            grid_origin_x = None
        else:
            grid_origin_x, convert_xorigin_to_meters = _parse_meter_degree_param(parts[7])

        convert_yorigin_to_meters = False
        if parts[8] == "None" or parts[8] == '':
            static = False
            grid_origin_y = None
        else:
            grid_origin_y, convert_yorigin_to_meters = _parse_meter_degree_param(parts[8])
    except ValueError:
        LOG.error("Could not parse proj4 grid configuration: '%s'" % (grid_name,))
        raise

    if (pixel_size_x is None and pixel_size_y is not None) or \
            (pixel_size_x is not None and pixel_size_y is None):
        LOG.error("Both or neither pixel sizes must be specified for '%s'" % grid_name)
        raise ValueError("Both or neither pixel sizes must be specified for '%s'" % grid_name)
    if (grid_width is None and grid_height is not None) or \
            (grid_width is not None and grid_height is None):
        LOG.error("Both or neither grid sizes must be specified for '%s'" % grid_name)
        raise ValueError("Both or neither grid sizes must be specified for '%s'" % grid_name)
    if (grid_origin_x is None and grid_origin_y is not None) or \
            (grid_origin_x is not None and grid_origin_y is None):
        LOG.error("Both or neither grid origins must be specified for '%s'" % grid_name)
        raise ValueError("Both or neither grid origins must be specified for '%s'" % grid_name)
    if grid_width is None and pixel_size_x is None:
        LOG.error("Either grid size or pixel size must be specified for '%s'" % grid_name)
        raise ValueError("Either grid size or pixel size must be specified for '%s'" % grid_name)
    if convert_xorigin_to_meters != convert_yorigin_to_meters:
        LOG.error("Grid origin parameters must be in the same units (meters vs degrees)")
        raise ValueError("Grid origin parameters must be in the same units (meters vs degrees)")

    # Convert any parameters from degrees to meters (we already made sure both need to be converted above)
    p = Proj(proj4_str)
    if convert_xorigin_to_meters and not p.is_latlong():
        meters_x, meters_y = p(grid_origin_x, grid_origin_y)
        LOG.debug("Converted grid '%s' origin from (lon: %f, lat: %f) to (x: %f, y: %f)",
                  grid_name, grid_origin_x, grid_origin_y, meters_x, meters_y)
        grid_origin_x, grid_origin_y = meters_x, meters_y
    elif not convert_xorigin_to_meters and (grid_origin_x is not None and p.is_latlong()):
        LOG.error("Lat/Lon grid '%s' must have its origin in degrees", grid_name)
        raise ValueError("Lat/Lon grid '%s' must have its origin in degrees" % (grid_name,))

    proj4_dict = get_proj4_info(proj4_str)

    info.update(**proj4_dict)
    info["grid_kind"] = "proj4"
    info["static"] = static
    info["proj4_str"] = proj4_str
    info["pixel_size_x"] = pixel_size_x
    info["pixel_size_y"] = pixel_size_y
    info["grid_origin_x"] = grid_origin_x
    info["grid_origin_y"] = grid_origin_y
    info["grid_width"] = grid_width
    info["grid_height"] = grid_height

    return info