Example #1
0
 def test_polar2centroids(self):
     r = np.array([10000., 10100.])
     az = np.array([45., 90.])
     sitecoords = (9., 48.)
     self.assertTrue(np.allclose(georef.polar2centroids(r, az, sitecoords),
                                 tuple((np.array([[9.09469143, 9.09564428],
                                                  [9.13374952, 9.13509373]]),
                                        np.array([[48.06324434, 48.06387957],
                                                  [47.99992237, 47.9999208]])))))
Example #2
0
 def test_polar2centroids(self):
     r = np.array([10000., 10100.])
     az = np.array([45., 90.])
     sitecoords = (9., 48.)
     self.assertTrue(np.allclose(georef.polar2centroids(r, az, sitecoords),
                                 tuple((np.array([[9.09469143, 9.09564428],
                                                  [9.13374952,
                                                   9.13509373]]),
                                        np.array(
                                            [[48.06324434, 48.06387957],
                                             [47.99992237, 47.9999208]])))))
Example #3
0
def ex_georef():
    # --------------------------------------------------------------------------
    # EXAMPLE 1: Full workflow for georeferencing radar data

    # 1st step: generate the centroid coordinates of the radar bins
    #   define the polar coordinates and the site coordinates in lat/lon
    r = np.arange(1, 129) * 1000
    az = np.linspace(0, 360, 361)[0:-1]
    #   drs:  51.12527778 ; fbg: 47.87444444 ; tur: 48.58611111 ; muc: 48.3372222
    #   drs:  13.76972222 ; fbg: 8.005 ; tur: 9.783888889 ; muc: 11.61277778
    sitecoords = (9.7839, 48.5861)

    #   these are the polgon vertices of the radar bins
    polygons = georef.polar2polyvert(r, az, sitecoords)

    #   these are the corresponding centroids
    cent_lon, cent_lat = georef.polar2centroids(r, az, sitecoords)

    # plot the vertices and the centroids in one plot
    fig = pl.figure(figsize=(8, 8))
    ax = fig.add_subplot(111)
    polycoll = mpl.collections.PolyCollection(polygons, closed=True, facecolors='None')
    ax.add_collection(polycoll, autolim=True)
    ax.plot(cent_lon, cent_lat, 'r+')
    ax.axis('tight')
    pl.title('Zoom in to compare polygons and centroids.')
    pl.show()

    # 2nd step: reproject the centroid coordinates to Gauss-Krueger Zone 3
    #   by using the EPSG-Number 31467
    # use it for projecting the centroids to Gauss-Krueger 3
    proj_gk3 = georef.epsg_to_osr(31467)
    x, y = georef.reproject(cent_lon, cent_lat, projection_targe=proj_gk3)

    # export the projected centroid coordinates
    # f = open('centroids.tab', 'w')
    f = 'centroids.tab'
    # f.write('x\ty\n')
    np.savetxt(f, np.hstack((x.reshape((-1, 1)), y.reshape((-1, 1)))), fmt='%.2f', header='x\ty', delimiter='\t')
    # f.close()

    print('Exit.')
Example #4
0
    def setUp(self):
        global skip
        # setup test grid and catchment
        lon = 7.071664
        lat = 50.730521
        r = np.array(range(50, 100 * 1000 + 50, 100))
        a = np.array(range(0, 360, 1))
        rays = a.shape[0]
        bins = r.shape[0]
        # create polar grid polygon vertices in lat,lon
        radar_ll = georef.polar2polyvert(r, a, (lon, lat))

        # create polar grid centroids in lat,lon
        rlon, rlat = georef.polar2centroids(r, a, (lon, lat))
        radar_llc = np.dstack((rlon, rlat))

        # setup OSR objects
        self.proj_gk = osr.SpatialReference()
        self.proj_gk.ImportFromEPSG(31466)
        self.proj_ll = osr.SpatialReference()
        self.proj_ll.ImportFromEPSG(4326)

        # project ll grids to GK2
        self.radar_gk = georef.reproject(radar_ll,
                                         projection_source=self.proj_ll,
                                         projection_target=self.proj_gk)
        self.radar_gkc = georef.reproject(radar_llc,
                                          projection_source=self.proj_ll,
                                          projection_target=self.proj_gk)

        # reshape
        self.radar_gk.shape = (rays, bins, 5, 2)
        self.radar_gkc.shape = (rays, bins, 2)

        self.box0 = np.array([[2600000., 5630000.], [2600000., 5630100.],
                              [2600100., 5630100.], [2600100., 5630000.],
                              [2600000., 5630000.]])

        self.box1 = np.array([[2600100., 5630000.], [2600100., 5630100.],
                              [2600200., 5630100.], [2600200., 5630000.],
                              [2600100., 5630000.]])

        self.data = np.array([self.box0, self.box1])

        # create catchment bounding box
        buffer = 5000.
        bbox = zonalstats.get_bbox(self.data[..., 0], self.data[..., 1])
        bbox = dict(left=bbox['left'] - buffer,
                    right=bbox['right'] + buffer,
                    bottom=bbox['bottom'] - buffer,
                    top=bbox['top'] + buffer)

        mask, shape = zonalstats.mask_from_bbox(self.radar_gkc[..., 0],
                                                self.radar_gkc[..., 1],
                                                bbox,
                                                polar=True)

        self.radar_gkc = self.radar_gkc[mask, :]
        self.radar_gk = self.radar_gk[mask]

        self.zdpoly = zonalstats.ZonalDataPoly(self.radar_gk,
                                               self.data,
                                               srs=self.proj_gk)
        # self.zdpoly.dump_vector('test_zdpoly')
        self.zdpoint = zonalstats.ZonalDataPoint(self.radar_gkc,
                                                 self.data,
                                                 srs=self.proj_gk)
        # self.zdpoint.dump_vector('test_zdpoint')

        isec_poly0 = np.array([
            np.array([[2600000., 5630000.], [2600000., 5630057.83273596],
                      [2600018.65014816, 5630000.], [2600000., 5630000.]]),
            np.array([[2600000., 5630057.83273596], [2600000., 5630100.],
                      [2600091.80406488,
                       5630100.], [2600100., 5630074.58501104],
                      [2600100., 5630000.], [2600018.65014816, 5630000.],
                      [2600000., 5630057.83273596]]),
            np.array([[2600091.80406488, 5630100.], [2600100., 5630100.],
                      [2600100., 5630074.58501104],
                      [2600091.80406488, 5630100.]])
        ])
        isec_poly1 = np.array([
            np.array([[2600100., 5630000.], [2600100., 5630074.58501104],
                      [2600124.05249566, 5630000.], [2600100., 5630000.]]),
            np.array([[2600100., 5630074.58501104], [2600100., 5630100.],
                      [2600197.20644071,
                       5630100.], [2600200., 5630091.33737992],
                      [2600200., 5630000.], [2600124.05249566, 5630000.],
                      [2600100., 5630074.58501104]]),
            np.array([[2600197.20644071, 5630100.], [2600200., 5630100.],
                      [2600200., 5630091.33737992],
                      [2600197.20644071, 5630100.]])
        ])

        isec_point0 = np.array([[2600062.31245173, 5630031.20266055]])
        isec_point1 = np.array([[2600157.8352244, 5630061.85098382]])

        self.isec_poly = np.array([isec_poly0, isec_poly1])
        self.isec_point = np.array([isec_point0, isec_point1])
Example #5
0
    def setUp(self):
        global skip
        # setup test grid and catchment
        lon = 7.071664
        lat = 50.730521
        r = np.array(range(50, 100 * 1000 + 50, 100))
        a = np.array(range(0, 360, 1))
        rays = a.shape[0]
        bins = r.shape[0]
        # create polar grid polygon vertices in lat,lon
        radar_ll = georef.polar2polyvert(r, a, (lon, lat))

        # create polar grid centroids in lat,lon
        rlon, rlat = georef.polar2centroids(r, a, (lon, lat))
        radar_llc = np.dstack((rlon, rlat))

        # setup OSR objects
        self.proj_gk = osr.SpatialReference()
        self.proj_gk.ImportFromEPSG(31466)
        self.proj_ll = osr.SpatialReference()
        self.proj_ll.ImportFromEPSG(4326)

        # project ll grids to GK2
        self.radar_gk = georef.reproject(radar_ll, projection_source=self.proj_ll,
                                         projection_target=self.proj_gk)
        self.radar_gkc = georef.reproject(radar_llc, projection_source=self.proj_ll,
                                          projection_target=self.proj_gk)

        # reshape
        self.radar_gk.shape = (rays, bins, 5, 2)
        self.radar_gkc.shape = (rays, bins, 2)

        self.box0 = np.array([[2600000., 5630000.], [2600000., 5630100.],
                              [2600100., 5630100.], [2600100., 5630000.],
                              [2600000., 5630000.]])

        self.box1 = np.array([[2600100., 5630000.], [2600100., 5630100.],
                              [2600200., 5630100.], [2600200., 5630000.],
                              [2600100., 5630000.]])

        self.data = np.array([self.box0, self.box1])

        # create catchment bounding box
        buffer = 5000.
        bbox = zonalstats.get_bbox(self.data[..., 0], self.data[..., 1])
        bbox = dict(left=bbox['left'] - buffer, right=bbox['right'] + buffer,
                    bottom=bbox['bottom'] - buffer, top=bbox['top'] + buffer)

        mask, shape = zonalstats.mask_from_bbox(self.radar_gkc[..., 0],
                                                self.radar_gkc[..., 1],
                                                bbox,
                                                polar=True)

        self.radar_gkc = self.radar_gkc[mask, :]
        self.radar_gk = self.radar_gk[mask]

        self.zdpoly = zonalstats.ZonalDataPoly(self.radar_gk, self.data, srs=self.proj_gk)
        # self.zdpoly.dump_vector('test_zdpoly')
        self.zdpoint = zonalstats.ZonalDataPoint(self.radar_gkc, self.data, srs=self.proj_gk)
        # self.zdpoint.dump_vector('test_zdpoint')

        isec_poly0 = np.array([np.array([[2600000., 5630000.],
                                         [2600000., 5630057.83273596],
                                         [2600018.65014816, 5630000.],
                                         [2600000., 5630000.]]),
                               np.array([[2600000., 5630057.83273596],
                                         [2600000., 5630100.],
                                         [2600091.80406488, 5630100.],
                                         [2600100., 5630074.58501104],
                                         [2600100., 5630000.],
                                         [2600018.65014816, 5630000.],
                                         [2600000., 5630057.83273596]]),
                               np.array([[2600091.80406488, 5630100.],
                                         [2600100., 5630100.],
                                         [2600100., 5630074.58501104],
                                         [2600091.80406488, 5630100.]])])
        isec_poly1 = np.array([np.array([[2600100., 5630000.],
                                         [2600100., 5630074.58501104],
                                         [2600124.05249566, 5630000.],
                                         [2600100., 5630000.]]),
                               np.array([[2600100., 5630074.58501104],
                                         [2600100., 5630100.],
                                         [2600197.20644071, 5630100.],
                                         [2600200., 5630091.33737992],
                                         [2600200., 5630000.],
                                         [2600124.05249566, 5630000.],
                                         [2600100., 5630074.58501104]]),
                               np.array([[2600197.20644071, 5630100.],
                                         [2600200., 5630100.],
                                         [2600200., 5630091.33737992],
                                         [2600197.20644071, 5630100.]])])

        isec_point0 = np.array([[2600062.31245173, 5630031.20266055]])
        isec_point1 = np.array([[2600157.8352244, 5630061.85098382]])

        self.isec_poly = np.array([isec_poly0, isec_poly1])
        self.isec_point = np.array([isec_point0, isec_point1])