Example #1
0
def test_equivalent_proj():
    transformer = Transformer.from_proj(
        "+init=epsg:4326", pyproj.Proj(4326).crs.to_proj4(), skip_equivalent=True
    )
    assert transformer._transformer.skip_equivalent
    assert transformer._transformer.projections_equivalent
    assert not transformer._transformer.projections_exact_same
Example #2
0
def test_4d_transform():
    transformer = Transformer.from_pipeline("+init=ITRF2008:ITRF2000")
    assert_almost_equal(
        transformer.transform(
            xx=3513638.19380, yy=778956.45250, zz=5248216.46900, tt=2008.75
        ),
        (3513638.1999428216, 778956.4532640711, 5248216.453456361, 2008.75),
    )
Example #3
0
def test_4d_transform_crs_obs1():
    transformer = Transformer.from_proj(7789, 8401)
    assert_almost_equal(
        transformer.transform(
            xx=3496737.2679, yy=743254.4507, zz=5264462.9620, tt=2019.0
        ),
        (3496737.757717311, 743253.9940103051, 5264462.701132784, 2019.0),
    )
Example #4
0
def test_4d_transform_crs_obs2():
    transformer = Transformer.from_proj(4896, 7930)
    assert_almost_equal(
        transformer.transform(
            xx=3496737.2679, yy=743254.4507, zz=5264462.9620, tt=2019.0
        ),
        (3496737.7857162016, 743254.0394113371, 5264462.643659916, 2019.0),
    )
Example #5
0
def test_equivalent_pipeline():
    transformer = Transformer.from_pipeline(
        "+proj=pipeline +step +proj=longlat +ellps=WGS84 +step "
        "+proj=unitconvert +xy_in=rad +xy_out=deg"
    )
    assert not transformer._transformer.skip_equivalent
    assert not transformer._transformer.projections_equivalent
    assert not transformer._transformer.projections_exact_same
Example #6
0
def test_4d_itransform():
    transformer = Transformer.from_pipeline("+init=ITRF2008:ITRF2000")
    assert_almost_equal(
        list(
            transformer.itransform(
                [(3513638.19380, 778956.45250, 5248216.46900, 2008.75)]
            )
        ),
        [(3513638.1999428216, 778956.4532640711, 5248216.453456361, 2008.75)],
    )
Example #7
0
def test_3d_time_itransform():
    transformer = Transformer.from_pipeline("+init=ITRF2008:ITRF2000")
    assert_almost_equal(
        list(
            transformer.itransform(
                [(3513638.19380, 778956.45250, 2008.75)], time_3rd=True
            )
        ),
        [(3513638.1999428216, 778956.4532640711, 2008.75)],
    )
Example #8
0
def test_2d_with_time_transform_crs_obs2():
    transformer = Transformer.from_proj(4896, 7930)
    assert_almost_equal(
        transformer.transform(xx=3496737.2679, yy=743254.4507, tt=2019.0),
        (3496737.4105305015, 743254.1014318303, 2019.0),
    )
Example #9
0
import csv
import gzip
import os
import re
import sys

from pyproj import Transformer

csv.field_size_limit(sys.maxsize)

AMOUNT_REGEX = re.compile('Both Installment[\s\S]+\$([\d,\.]+)')

# California Zone 3
# https://epsg.io/2227
transformer = Transformer.from_crs(2227, 4326)


def get_val(row, key):
    val = row[key].strip()
    if not val:
        return 0
    return float(val)

with open('/home/ian/Downloads/Santa_Cruz_Assessor_Parcels.csv') as f_in, \
     open('./parse_output.csv', 'w') as f_out:
    reader = csv.DictReader(f_in)
    fieldnames = ['address', 'apn', 'longitude', 'latitude', 'tax', 'county']
    writer = csv.DictWriter(f_out, fieldnames=fieldnames)

    count = 0
Example #10
0
"""
Spyder Editor
"""

import xlwt
import time
start_time = time.perf_counter()
import pandas as pd
pd.set_option('display.max_columns', 500)
pd.set_option('display.width', 120)

from qgis.core import QgsDistanceArea, QgsPointXY
distance = QgsDistanceArea()
from pyproj import Transformer
transformer = Transformer.from_crs(
    "epsg:31467", "epsg:25832", always_xy=True
)  ##gauss_krueger_coordinate zone 3 (31467), UTM zone 32N (25832)

from pathlib import Path
f = open(Path.home() / 'python32' / 'python_dir.txt', mode='r')
path = Path.joinpath(Path(r'C:' + f.readline()), 'PT_data', 'VISUM_FAN.txt')
f = path.read_text().split('\n')

##connection to file
df_FAN = pd.read_excel(r'C:' + f[0], sheet_name=f[1])
df_VISUM = pd.read_excel(r'C:' + f[2], sheet_name=f[3])

wb = xlwt.Workbook()
ws = wb.add_sheet(f[5])
results = 'C:' + f[4]
    def __init__(self,
                 tier=1,
                 show_mode=False,
                 augment=False,
                 small_subset=False,
                 crop_size=1024,
                 resize_size=224,
                 window_random_shift=2048,
                 data_dir="./",
                 baseline_mode=False,
                 transform=None,
                 val=False,
                 val_split=0.1,
                 split_seed=42,
                 sampling_mode="polygons"):
        super().__init__()

        self.crop_size = crop_size
        self.resize_size = resize_size

        self.data_dir = data_dir

        random.seed(42)

        # Load TIF and geojson file names
        # from train_metadata.csv
        self.img_ids_to_tif = dict()
        self.img_ids_to_geojson = dict()
        with open(data_dir + "/train_metadata.csv", 'r') as f:
            csvreader = csv.reader(f)
            header = next(csvreader)
            imgs = []
            for row in csvreader:
                if int(row[3]) <= tier:
                    imgs.append(row)
            random.shuffle(imgs)
            n_train = int(len(imgs) * (1.0 - val_split))
            if val:
                imgs = imgs[n_train:]
            else:
                imgs = imgs[:n_train]
            for row in imgs:
                imgid = row[0].split("/")[2]
                self.img_ids_to_tif[imgid] = row[0]
                self.img_ids_to_geojson[imgid] = row[1]

        # Load all geojson files
        print("Loading geojson files")

        self.geojson_data = dict()
        self.all_polygons = []
        self.feat_to_img_id = []
        for ids, geojson_f in tqdm(self.img_ids_to_geojson.items()):
            self.geojson_data[ids] = self._load_geojsonfile(data_dir + "/" +
                                                            geojson_f)
            self.all_polygons += self.geojson_data[ids]["features"]
            for f in self.geojson_data[ids]["features"]:
                self.feat_to_img_id.append(ids)

        print("Number of training polygons:", len(self.all_polygons))

        # Open all tif files
        # and get corresponding transformers
        print("Opening rasters")
        #self.rasters = dict()
        img_to_transformer_dict = dict()
        for ids, img_f in tqdm(self.img_ids_to_tif.items()):
            with rasterio.open(data_dir + "/" + img_f) as raster:
                img_to_transformer_dict[ids] = Transformer.from_crs(
                    CRS.from_proj4("+proj=latlon"), raster.crs)

        print("Reading means and std")
        self.stats = dict()
        for ids, tifs in self.img_ids_to_tif.items():
            with open(
                    data_dir + "/" +
                    ".".join([tifs.split(".")[0], "tif.stats.json"]),
                    "r") as statfile:
                self.stats[ids] = json.load(statfile)

        print("Reprojecting polygons")

        self.img_id_to_polys = dict()
        for k, v in self.img_ids_to_tif.items():
            self.img_id_to_polys[k] = []

        for i in tqdm(range(len(self.all_polygons))):
            self.convert_poly(i, img_to_transformer_dict)
            self.img_id_to_polys[self.feat_to_img_id[i]].append(
                self.all_polygons[i])

        if small_subset:
            self.all_polygons = self.all_polygons[:200]

        self.show_mode = show_mode
        self.baseline_mode = baseline_mode

        self.aug_transforms = transforms.Compose([
            transforms.RandomVerticalFlip(),
            transforms.RandomHorizontalFlip(),
            transforms.RandomAffine((180), (0.2, 0.2), (0.5, 2)),
            transforms.ColorJitter(0.2, 0.2, 0.5, 0.2),
            transforms.Resize((resize_size, resize_size)),
            transforms.ToTensor(),
            # transforms.RandomErasing(),
            transforms.ToPILImage()
        ])

        self.noaug_transforms = transforms.Compose(
            [transforms.Resize((resize_size, resize_size))])

        self.mask_transforms = transforms.Compose([
            transforms.Resize((resize_size, resize_size)),
            transforms.ToTensor()
        ])

        self.img_transforms = transforms.Compose([
            transforms.ToTensor(),
            transforms.Normalize(mean=[0.485, 0.456, 0.406],
                                 std=[0.229, 0.224, 0.225])
        ])

        self.wow_transforms = transform

        self.augment = augment

        self.window_random_shift = window_random_shift

        self.sampling_mode = sampling_mode
Example #12
0
def test_equivalent_proj__disabled():
    with pytest.warns(UserWarning):
        transformer = Transformer.from_proj(3857, pyproj.Proj(3857).crs.to_proj4())
    assert not transformer._transformer.skip_equivalent
    assert transformer._transformer.projections_equivalent
    assert not transformer._transformer.projections_exact_same
Example #13
0
def test_equivalent_crs__disabled():
    transformer = Transformer.from_crs("epsg:4326", 4326)
    assert not transformer._transformer.skip_equivalent
    assert transformer._transformer.projections_equivalent
    assert transformer._transformer.projections_exact_same
Example #14
0
def test_transformer__area_of_interest__invalid(aoi_data_directory):
    with pytest.raises(ProjError):
        Transformer.from_crs(
            4326, 2964, area_of_interest=(-136.46, 49.0, -60.72, 83.17)
        )
Example #15
0
def test_transform_direction__invalid():
    transformer = Transformer.from_crs(4326, 3857)
    with pytest.raises(ValueError, match="Invalid value"):
        transformer.transform(-33, 24, direction="WHEREVER")
Example #16
0
def test_always_xy__transformer():
    transformer = Transformer.from_crs(2193, 4326, always_xy=True)
    assert_almost_equal(
        transformer.transform(1625350, 5504853),
        (173.29964730317386, -40.60674802693758),
    )
Example #17
0
def test_transform_no_exception():
    # issue 249
    with pytest.warns(FutureWarning):
        transformer = Transformer.from_proj("+init=epsg:4326", "+init=epsg:27700")
    transformer.transform(1.716073972, 52.658007833, errcheck=True)
    transformer.itransform([(1.716073972, 52.658007833)], errcheck=True)
Example #18
0
def test_2d_with_time_transform_crs_obs2():
    transformer = Transformer.from_proj(4896, 7930)
    assert_almost_equal(
        transformer.transform(xx=3496737.2679, yy=743254.4507, tt=2019.0),
        (3496737.4105305015, 743254.1014318303, 2019.0),
    )
Example #19
0
def test_2d_with_time_transform():
    transformer = Transformer.from_pipeline("+init=ITRF2008:ITRF2000")
    assert_almost_equal(
        transformer.transform(xx=3513638.19380, yy=778956.45250, tt=2008.75),
        (3513638.1999428216, 778956.4532640711, 2008.75),
    )
Example #20
0
def test_equivalent_proj__different():
    transformer = Transformer.from_proj(3857, 4326, skip_equivalent=True)
    assert transformer._transformer.skip_equivalent
    assert not transformer._transformer.projections_equivalent
    assert not transformer._transformer.projections_exact_same
Example #21
0
parser.add_argument('-f', '--data', help='NetCDF data with lon and lat coordinates will be changed', required=True)
parser.add_argument('-o', '--output', help='Output remapped NetCDF data on Web Mercator grids')
args = parser.parse_args()

if not os.path.isfile(args.data):
	print(f'[Error]: File {args.data} does not exist!')
	exit(1)

f1 = Dataset(args.data, 'r')
lon1 = f1.variables['lon'][:]
lat1 = f1.variables['lat'][:]

crs1 = CRS.from_proj4('+proj=latlon')
crs2 = CRS.from_epsg(3857)

t12 = Transformer.from_crs(crs1, crs2)
t21 = Transformer.from_crs(crs2, crs1)

min_x2, min_y2 = t12.transform(lon1[ 0], lat1[ 0])
max_x2, max_y2 = t12.transform(lon1[-1], lat1[-1])

x2 = np.linspace(min_x2, max_x2, len(lon1))
y2 = np.linspace(min_y2, max_y2, len(lat1))

lon2 = [t21.transform(x2[i], y2[0])[0] for i in range(len(x2))]
lat2 = [t21.transform(x2[0], y2[j])[1] for j in range(len(y2))]

if not args.output:
	args.output = os.path.join(os.path.dirname(args.data), os.path.basename(args.data).replace('.nc', '') + '_web.nc')
print(f'[Notice]: Create file {args.output} ...')
f2 = Dataset(args.output, 'w', format='NETCDF3_CLASSIC')
Example #22
0
import cv2
import numpy as np
import os
import logging
from time import time
from pyproj import Transformer

import config

transform_sheet_to_out = Transformer.from_proj(config.proj_sheets,
                                               config.proj_out,
                                               skip_equivalent=True,
                                               always_xy=True)


def register_ECC(query_image,
                 reference_image,
                 warp_matrix=None,
                 warp_mode=cv2.MOTION_AFFINE):
    # adapted from https://www.learnopencv.com/image-alignment-ecc-in-opencv-c-python/

    logging.debug("starting registration...")

    if warp_matrix is None:
        # Define 2x3 or 3x3 matrices and initialize the matrix to identity
        if warp_mode == cv2.MOTION_HOMOGRAPHY:
            warp_matrix = np.eye(3, 3, dtype=np.float32)
        else:
            warp_matrix = np.eye(2, 3, dtype=np.float32)
    else:
        if warp_mode != cv2.MOTION_HOMOGRAPHY:
Example #23
0
def test_from_pipeline__non_transform_input():
    with pytest.raises(ProjError, match="Input is not a transformation"):
        Transformer.from_pipeline("epsg:4326")
Example #24
0
def test_equivalent_crs():
    transformer = Transformer.from_crs("epsg:4326", 4326, skip_equivalent=True)
    assert transformer._transformer.projections_equivalent
    assert transformer._transformer.projections_exact_same
    assert transformer._transformer.skip_equivalent
Example #25
0
def test_non_supported_initialization():
    with pytest.raises(ProjError, match="Transformer must be initialized using"):
        Transformer()
Example #26
0
def test_equivalent_crs__different():
    transformer = Transformer.from_crs("epsg:4326", 3857, skip_equivalent=True)
    assert transformer._transformer.skip_equivalent
    assert not transformer._transformer.projections_equivalent
    assert not transformer._transformer.projections_exact_same
Example #27
0
def test_to_wkt():
    transformer = Transformer.from_crs(4326, 3857)
    assert transformer.to_wkt().startswith(
        'CONVERSION["Popular Visualisation Pseudo-Mercator"'
    )
 def point4326to3857(self, x, y):
     transformer = Transformer.from_crs("epsg:4326", "epsg:3857", always_xy=True)
     return transformer.transform(x, y)
Example #29
0
def test_str():
    assert str(Transformer.from_crs(4326, 3857)).startswith("proj=pipeline")
Example #30
0
        mask[i, :, :] = np.loadtxt(f, skiprows=6, dtype=np.int32)

    dims = ("time", "latitude", "longitude")
    data = {
        "latitude": (("latitude", ), lats),
        "longitude": (("longitude", ), lons),
        "time": (("time", ), times),
        "precip_rate": (dims, precip_rate),
        "mask": (dims, mask),
        "radar_quality_index": (dims, rqi)
    }

    return xr.Dataset(data).sortby(["time"])


_WGS84_TO_ECEF = Transformer.from_crs("epsg:4326", "epsg:4978")
_ECEF_TO_WGS84 = Transformer.from_crs("epsg:4978", "epsg:4326")


def run_preprocessor(l1c_file, output_file):
    """
    Run preprocessor on L1C GMI file.

    Args:
        l1c_file: Path of the L1C file for which to extract the input data
             using the preprocessor.
    """
    output_file = Path(output_file)
    jobid = str(os.getpid()) + "_pp"
    prodtype = "CLIMATOLOGY"
    prepdir = "/qdata2/archive/ERA5/"
Example #31
0
def test_transform_no_exception():
    # issue 249
    transformer = Transformer.from_proj("+init=epsg:4326", "+init=epsg:27700")
    transformer.transform(1.716073972, 52.658007833, errcheck=True)
    transformer.itransform([(1.716073972, 52.658007833)], errcheck=True)
Example #32
0
def test_2d_with_time_transform():
    transformer = Transformer.from_pipeline("+init=ITRF2008:ITRF2000")
    assert_almost_equal(
        transformer.transform(xx=3513638.19380, yy=778956.45250, tt=2008.75),
        (3513638.1999428216, 778956.4532640711, 2008.75),
    )
Example #33
0
def test_equivalent_crs__different():
    with pytest.warns(UserWarning):
        transformer = Transformer.from_crs("epsg:4326", 3857, skip_equivalent=True)
    assert transformer._transformer.skip_equivalent
    assert not transformer._transformer.projections_equivalent
    assert not transformer._transformer.projections_exact_same
Example #34
0
INSERT_ROW = 1000
FILE_SIZE = 1000000
TYPE = args.type
COLLECTION = args.collection
BLOCK = map(int, args.block)

collist = DB_CONN.list_collection_names()
if COLLECTION not in collist:
    if TYPE == 'point':
        COLLECTION = DB_CONN['small_point']
    elif TYPE == 'multipoint':
        COLLECTION = DB_CONN['small_multipoint']

input_crs = CRS.from_epsg(28356)
output_crs = CRS.from_epsg(4326)
transformer = Transformer.from_crs(input_crs, output_crs)


class XYZTOMongoDB:
    def __init__(self, file_path, split_dir):
        self.file_path = file_path
        self.split_dir = split_dir

    def gen_line(self):
        with open(self.file_path, mode='r', encoding='utf-8') as f:
            while (True):
                line = f.readline()
                if not line:
                    break
                yield line
Example #35
0
def test_equivalent_proj__disabled():
    transformer = Transformer.from_proj(3857, pyproj.Proj(3857).crs.to_proj4())
    assert not transformer._transformer.skip_equivalent
    assert not transformer._transformer.projections_equivalent
    assert not transformer._transformer.projections_exact_same
Example #36
0
def test_to_json_dict():
    transformer = Transformer.from_crs(4326, 3857)
    json_dict = transformer.to_json_dict()
    assert json_dict["type"] == "Conversion"
Example #37
0
 def Initialize(self):
     if (self.TheTransform == None):
         self.TheTransform = Transformer.from_crs(self.FromCRS,
                                                  self.ToCRS,
                                                  always_xy=True)
Example #38
0
def test_transform_exception():
    transformer = Transformer.from_proj("+init=epsg:4326", "+init=epsg:27700")
    with pytest.raises(ProjError):
        transformer.transform(100000, 100000, errcheck=True)
Example #39
0
def test_to_json():
    transformer = Transformer.from_crs(4326, 3857)
    json_data = transformer.to_json()
    assert "Conversion" in json_data
    assert "\n" not in json_data
Example #40
0
def test_equivalent_crs__disabled():
    with pytest.warns(UserWarning):
        transformer = Transformer.from_crs("epsg:4326", 4326)
    assert not transformer._transformer.skip_equivalent
    assert transformer._transformer.projections_equivalent
    assert transformer._transformer.projections_exact_same
Example #41
0
def test_to_json__pretty__indenation():
    transformer = Transformer.from_crs(4326, 3857)
    json_data = transformer.to_json(pretty=True, indentation=4)
    assert "Conversion" in json_data
    assert json_data.startswith('{\n    "')
Example #42
0
def test_transformer__operations_missing():
    assert Transformer.from_crs(7789, 8401).operations == ()
Example #43
0
def test_transformer_proj__area_of_interest(aoi_data_directory):
    transformer = Transformer.from_proj(
        4326, 2964, area_of_interest=AreaOfInterest(-136.46, 49.0, -60.72, 83.17)
    )
    assert transformer.description == "Inverse of NAD27 to WGS 84 (13) + Alaska Albers"
Example #44
0
def test_equivalent_proj__different():
    transformer = Transformer.from_proj(3857, 4326, skip_equivalent=True)
    assert transformer._transformer.skip_equivalent
    assert not transformer._transformer.projections_equivalent
    assert not transformer._transformer.projections_exact_same
Example #45
0
##
## CSV Input
##

in_filenames = sorted(glob.glob('codepo_gb/Data/CSV/*.csv'))

# 10 columns but we only need the first 4
in_fieldnames = ['postcode', 'positional_quality_indicator', 'eastings', 'northings']


##
## Transform
##
# We convert OSGB 1936 (https://epsg.io/27700) to WGS 84 (https://epsg.io/4326)
transformer = Transformer.from_crs('EPSG:27700', 'EPSG:4326')


##
## CSV Output
##
out_filenames = ['postcode', 'lat', 'lon']

csv_writer = DictWriter(sys.stdout, fieldnames=out_filenames, lineterminator="\n")
csv_writer.writeheader()




for fieldname in in_filenames:
    with open(fieldname) as file: