예제 #1
0
def main():
    tr = TrLib.CoordinateTransformation('geoEetrs89', 'geoHetrs89_h_dnn')

    lons = np.arange(LONMIN, LONMAX + DLON, DLON)
    lats = np.arange(LATMIN, LATMAX + DLAT, DLAT)

    cols = len(lons)
    rows = len(lats)

    grid = np.ndarray((rows, cols))

    for i, lat in enumerate(lats):
        for j, lon in enumerate(lons):
            I = rows - i - 1
            try:
                grid[I, j] = -tr.Transform(lon, lat, z=0.0)[2]
                if grid[I, j] > 1000.0:
                    grid[I, j] = NODATA
            except:
                grid[I, j] = NODATA

    ds = gdal.GetDriverByName('GTX').Create('dnn.gtx', cols, rows, 1,
                                            gdal.GDT_Float32)
    ds.SetGeoTransform((LONMIN, DLON, 0, LATMAX, 0, -DLAT))
    band = ds.GetRasterBand(1)
    band.SetNoDataValue(NODATA)
    band.WriteArray(grid)

    srs = osr.SpatialReference()
    srs.ImportFromEPSG(4258)
    ds.SetProjection(srs.ExportToWkt())
    band.FlushCache()
예제 #2
0
def RandomTests(TESTS, dim=3, N=10000, repeat=3, log_file=sys.stdout):
    nerr = 0
    id = str(threading.current_thread().name)
    log_file.write("%s\n" % LINE_SPLIT)
    log_file.write("Thread: %s, Transforming %d %dd-points...\n" %
                   (id, N, dim))
    for test in TESTS:
        if len(test) == 6:
            label_in, label_out, x, y, z, scale = test
            geoid = ""
        else:
            label_in, label_out, x, y, z, scale, geoid = test

        log_file.write("%s\n" % LINE_SPLIT)
        log_file.write("Label_in: %s, Label_out: %s\n" % (label_in, label_out))
        xyz = RandomPoints(N, dim, x, y, z, scale)
        if N <= 10:
            log_file.write("in:\n%s\n" % repr(xyz))
        elif HAS_NUMPY:
            log_file.write("Center of mass: %s\n" % xyz.mean(axis=0).tolist())
        nok = 0
        try:
            ct = TrLib.CoordinateTransformation(label_in, label_out, geoid)
        except Exception, msg:
            log_file.write(repr(msg) + "\n")
            sys.stderr.write(repr(msg) + "\n")
            msg = "Failed to open %s->%s, geoid: %s, last error: %d\n" % (
                label_in, label_out, geoid, TrLib.GetLastError())
            log_file.write(msg)
            sys.stderr.write(msg)
            nerr += 1
            if THREAD_TEST:
                return nerr
            continue
        for i in range(repeat):
            tstart = time.clock()
            try:
                xyz_out = ct.Transform(xyz)
            except Exception, msg:
                log_file.write(repr(msg) + "\n")
                sys.stderr.write(repr(msg) + "\n")
                msg = "From: %s To: %s, Last error: %d\n" % (
                    label_in, label_out, TrLib.GetLastError())
                log_file.write(msg)
                sys.stderr.write(msg)
                if N < 10:
                    sys.stderr.write("Input: %s\n" % repr(xyz))
                nerr += 1
                if THREAD_TEST:
                    return nerr
            else:
                nok += 1
                log_file.write("Forward running time: %.5f s\n" %
                               (time.clock() - tstart))
예제 #3
0
 def run(self):
     tstart = time.clock()
     if self.log_file is None:
         fp = sys.stdout
     else:
         fp = open(self.log_file, "w")
     if self.flag is not None:
         self.flag.clear()
     fp.write(
         "Hello world! This is thread %d. I perform %dD-tests. Mode is: %d"
         % (self.id, int(self.mode > 0) + 2, self.mode))
     if self.mode != 3:  #turn up the heat! Test having an open transformation in between lots of other stuff
         ct = TrLib.CoordinateTransformation("utm32Hwgs84_h_dvr90",
                                             "geoHed50_h_dvr90")
         fp.write(" *** %d ***" % self.id)
         x, y, z = ct.Transform(512200.1, 6143200.1, 100.0)
         fp.write(" %d: 1st done ***\n" % self.id)
     n = self.N  #+something random??
     while self.iterations > 0:
         fp.write(
             "This is thread %d, todo: %d. I perform %dD-tests. Mode is: %d\n"
             %
             (self.id, self.iterations, int(self.mode > 0) + 2, self.mode))
         if self.mode == 0:
             nerr = RandomTests.RandomTests_3D(n, log_file=NoOut())
         elif self.mode == 1:
             nerr = RandomTests.RandomTests_2D(n, log_file=NoOut())
         else:
             nerr = RandomTests.RandomTests(RandomTests.FH_TEST,
                                            3,
                                            n,
                                            log_file=NoOut())
         if nerr > 0:
             fp.write("Thread %d, mode %d, encountered %d errors!\n" %
                      (self.id, self.mode, nerr))
         time.sleep(0.01)
         self.iterations -= 1
     if self.mode != 3:
         x, y, z = ct.Transform(512200.1, 6143200.1, 100.0)
         ct.Close()
     if self.mode == 3:
         time.sleep(0.01)
     TrLib.TerminateThread()
     if self.mode == 3:
         time.sleep(0.01)
     if self.flag is not None:
         self.flag.set()
     tend = time.clock()
     fp.write("Thread %i finished. Running time %.2f s\n" %
              (self.id, tend - tstart))
     if self.log_file is not None:
         fp.close()
     return
예제 #4
0
    def doBesselHelmert(self):
        # Check if we need to update data....
        is_custom = self.chb_bshlm_custom_ellipsoid.isChecked()
        self.cache.is_valid = False
        self.logBshlm("", clear=True)
        if is_custom or str(self.cb_bshlm_system.currentText()) != self.cache.mlb:
            self.onBshlmSystemChanged(False)
        if not self.cache.valid_label:
            self.logBshlm("Invalid input label...", "red")
            self.clearOutput()
            return
        is_mode1 = self.rdobt_bshlm_mode1.isChecked()
        # Get needed input
        mlb = self.cache.mlb
        geo_mlb = self.cache.geo_mlb
        is_geo_in = is_custom or TrLib.IsGeographic(mlb)
        if is_mode1:
            coords, msg = WidgetUtils.getInput(
                self.input_bshlm, is_geo_in, z_fields=[], angular_unit=self.geo_unit)
            if len(coords) != 4:
                self.logBshlm("Input coordinate %d not OK.\n%s" %
                              (len(coords) + 1, msg), "red")
                self.input_bshlm[len(coords)].setFocus()
                self.clearOutput()
                return
            x1, y1, x2, y2 = coords

        else:
            coords, msg = WidgetUtils.getInput(
                self.input_bshlm[0:2], is_geo_in, z_fields=[], angular_unit=self.geo_unit)
            if len(coords) != 2:
                self.logBshlm("Station1 coordinates not OK.\n%s" % msg, "red")
                self.input_bshlm[len(coords)].setFocus()
                self.clearOutput()
                return
            input_data, msg = WidgetUtils.getInput(self.input_bshlm_azimuth, True, z_fields=[
                                                   0], angular_unit=self.geo_unit_derived)
            if len(input_data) != 2:
                self.logBshlm(
                    "Input distance and azimuth not OK.\n%s" % msg, "red")
                self.input_bshlm_azimuth[len(input_data)].setFocus()
                self.clearOutput()
                return
            x1, y1 = coords

            dist, a1 = input_data
        a = self.cache.axis
        f = self.cache.flattening
        #end get needed input#
        #transform to geo coords if needed#
        if not is_custom:
            try:
                ct = TrLib.CoordinateTransformation(mlb, geo_mlb)
            except:
                self.logBshlm("Input label not OK!", "red")
                self.clearOutput()
                return
            try:
                x1, y1, z = ct.Transform(x1, y1)
            except:
                msg = ""
                err = TrLib.GetLastError()
                if err in ERRORS:
                    msg = "\n%s" % ERRORS[err]
                self.logBshlm(
                    "Error in transformation of coords for station1" + msg, "red")
                self.clearOutput()
                return
        # display output of first transformation, x1,y1 should now alwyas be in
        # geo-coords#
        WidgetUtils.setOutput([x1, y1], self.output_bshlm_geo[
                              :2], True, z_fields=[], angular_unit=self.geo_unit)

        # Now get the other output from bshlm and transformations....
        if is_mode1:
            if not is_custom:
                try:  # transform to geographic
                    x2, y2, z = ct.Transform(x2, y2)
                except:
                    msg = ""
                    err = TrLib.GetLastError()
                    if err in ERRORS:
                        msg = "\n%s" % ERRORS[err]
                    self.logBshlm(
                        "Error in transformation of coords for station2" + msg, "red")
                    self.clearOutput()
                    return
            data = TrLib.BesselHelmert(a, f, x1, y1, x2, y2)
            if data[0] is not None:
                a1, a2 = data[1:]
                # WidgetUtils.setOutput(data,self.output_bshlm_azimuth,True,z_fields=[0],angular_unit=self.geo_unit_derived)
                self.output_bshlm_azimuth[0].setText("%.3f m" % data[0])
                self.output_bshlm_azimuth[1].setText(translateFromDegrees(
                    data[1], self.geo_unit_derived, precision=1))
                self.output_bshlm_azimuth[2].setText(translateFromDegrees(
                    data[2], self.geo_unit_derived, precision=1))
            else:
                self.message("Error: could not calculate azimuth!")
                self.clearOutput()
                return
        else:
            data = TrLib.InverseBesselHelmert(a, f, x1, y1, a1, dist)
            if data[0] is not None:
                x2, y2, a2 = data
                if not is_custom:
                    try:
                        x2_out, y2_out, z2 = ct.InverseTransform(x2, y2)
                    except:
                        msg = ""
                        err = TrLib.GetLastError()
                        if err in ERRORS:
                            msg = "\n%s" % ERRORS[err]
                        self.logBshlm(
                            "Error in transformation of coords for station2" + msg, "red")
                        self.clearOutput()
                        return
                else:
                    x2_out = x2
                    y2_out = y2
                # display result...
                WidgetUtils.setOutput([x2_out, y2_out], self.input_bshlm[
                                      2:], is_geo_in, z_fields=[], angular_unit=self.geo_unit)
                self.txt_bshlm_azimuth2.setText(translateFromDegrees(
                    a2, self.geo_unit_derived, precision=1))
            else:
                self.message(
                    "Error: could not do inverse Bessel Helmert calculation")
                self.clearOutput()
                return
        # always display ouput in geo field - even if not transformed
        self.cache.a1 = a1
        self.cache.a2 = a2
        WidgetUtils.setOutput([x2, y2],
                              self.output_bshlm_geo[2:], True, z_fields=[], angular_unit=self.geo_unit)
        self.cache.is_valid = True
        self.cache.mode = int(not is_mode1)