def srs_from_epsg(epsg):
    """Returns an ``osr.SpatialReference`` instance from the given EPSG code.

    Raises a ``ValueError`` if ``epsg`` is not a valid EPSG code.
    """
    from osgeo import osr
    srs = osr.SpatialReference()
    try:
        srs.ImportFromEPSG(int(epsg))
    except (TypeError, ValueError):
        raise ValueError('Invalid EPSG: {}'.format(epsg))
    return srs
