def Count_Points(self):
        '''
        Compute the grid cell

        ROADMAP:
            Step 1. preselect points using KD-Tree
            Step 2. refine preselection using if statements (to get blocks)

        '''

        #time keeping
        start = time.time()

        radius = self.resolution*1.5
        grid_counter = np.zeros((len(self.yrange),len(self.xrange)))
        for idx_y,y in enumerate(self.yrange):
            for idx_x,x in enumerate(self.xrange):
                #step 1
                subset = self.kdtree.query_ball_point([x,y],r=radius)
                
                #step 2
                if not subset:
                    grid_counter[idx_y,idx_x] = 0
                else:
                    count = 0
                    for point in subset:
                        rdx,rdy = self.data.iloc[point].values
                        
                        if x-self.resolution < rdx and x+self.resolution > rdx and y-self.resolution < rdy and y+self.resolution > rdy:
                            count += 1
                    
                    grid_counter[idx_y,idx_x] = count
                
                lbrd = rijksdriehoek.Rijksdriehoek(x-self.resolution,y-self.resolution)
                rbrd = rijksdriehoek.Rijksdriehoek(x+self.resolution,y-self.resolution)
                rtrd = rijksdriehoek.Rijksdriehoek(x+self.resolution,y+self.resolution)
                ltrd = rijksdriehoek.Rijksdriehoek(x-self.resolution,y+self.resolution)

                leftbot = lbrd.to_wgs()
                rightbot = rbrd.to_wgs()
                righttop = rtrd.to_wgs()
                lefttop = ltrd.to_wgs()

                # leftbot = rijksdriehoek.rd_to_wgs(x-self.resolution,y-self.resolution)
                # rightbot = rijksdriehoek.rd_to_wgs(x+self.resolution,y-self.resolution)
                # righttop = rijksdriehoek.rd_to_wgs(x+self.resolution,y+self.resolution)
                # lefttop = rijksdriehoek.rd_to_wgs(x-self.resolution,y+self.resolution)

                self.grid_polygon.append(f'POLYGON (({leftbot[1]} {leftbot[0]},{rightbot[1]} {rightbot[0]},{righttop[1]} {righttop[0]},{lefttop[1]} {lefttop[0]}))')

        self.grid_counter = grid_counter
        print(f'Elapsed time is {time.time()-start} seconds...')
    def Load_Data(self):
        '''
        Method for loading the data
        '''
        #check if the intermediate file already exists
        if not self.use_intermediate_results:

            if isinstance(self.filename, list):
                data = pd.DataFrame(columns=self.header)

                #for file_in in self.filename:
                #TEST: progressbar
                print('\nStarted reading the data...')
                for i in progressbar.progressbar(range(len(self.filename))):
                    file_in = self.filename[i]
                    temp = pd.read_csv(file_in)

                    if 'pnt_rdx' not in list(temp) or 'pnt_rdy' not in list(
                            temp):
                        #creating the rd coordinates using lat/lon as input
                        rd = rijksdriehoek.Rijksdriehoek()
                        rd.from_wgs(temp['pnt_lat'], temp['pnt_lon'])

                        #create new dataframe swith rd coordinates
                        df = pd.DataFrame(np.array([rd.rd_x, rd.rd_y]).T,
                                          columns=['pnt_rdx', 'pnt_rdy'])
                        temp = temp.join(df)

                        data = data.append(temp[self.header],
                                           ignore_index=True)
                    else:
                        data = data.append(temp[self.header],
                                           ignore_index=True)
                    print(f'\nAppended {file_in} to the dataset')
            else:
                data = pd.read_csv(self.filename)

            # save intermediate results such that the datasets don't have to be combined every new run
            if not os.path.isdir('intermediate_data'):
                os.mkdir('intermediate_data')

                data[self.header].to_csv(
                    os.path.join('intermediate_data', self.save_data))
            return data

        else:
            print(f'Reading pre-computed data {self.save_data}')

            return pd.read_csv(
                os.path.join('intermediate_data', self.save_data))
    def Create_Points(self):
        '''
        method to test the error in the conversion
        '''
        X = self.data['pnt_rdx']
        Y = self.data['pnt_rdy']
        
        rd = rijksdriehoek.Rijksdriehoek(X,Y)

        # wgs = np.array(list(map(rijksdriehoek.rd_to_wgs,X,Y)))
        wgs = np.array(rd.to_wgs()).T
    
        lat = wgs[:,0]
        lon = wgs[:,1]

        coordinates = np.array([lon,lat])

        return pd.DataFrame(coordinates.T,columns=['lon','lat'])
    def Create_df(self):
        '''
        Output a dataframe that can be saved
        '''
        X = np.ravel(self.xv)
        Y = np.ravel(self.yv)
        Z = np.ravel(self.grid_counter)

        rd = rijksdriehoek.Rijksdriehoek(X,Y)

        wgs = np.array(rd.to_wgs()).T

        lat = wgs[:,0]
        lon = wgs[:,1]

        data_out = np.array([X,Y,lon,lat,Z,self.grid_polygon])

        return pd.DataFrame(data_out.T,columns=['rd_x','rd_y','lon','lat','Counts','wkt'])
    def Count_Points(self):
        '''
        Compute the grid cell
        '''
        #time keeping
        start = time.time()

        #A cell is a square. From the center to a corner the radius should be multiplied by sqrt(2).
        #The search radius is made 1% larger just in case.
        radius = np.sqrt(2) * self.cell_radius * 1.01

        grid_counter = np.zeros((len(self.yrange), len(self.xrange)))

        print('Started counting points per cell...')
        idx_y = 0
        idx_x = 0
        for idx in progressbar.progressbar(
                range(len(self.yrange) * len(self.xrange))):

            #update idx_y and yrange if needed
            if idx == idx_y * len(self.xrange):
                y = self.yrange[idx_y]
                idx_y += 1

            #update idx_x and xrange if needed
            if idx == 0:
                pass
            else:
                idx_x += 1

            if idx % len(self.xrange) == 0:
                idx_x = 0

            x = self.xrange[idx_x]

            subset = self.kdtree.query_ball_point([x, y], r=radius)

            if not subset:
                pass
            else:
                values = np.array(self.data[['pnt_rdx',
                                             'pnt_rdy']].iloc[subset].values)

                rdx = values[:, 0]
                rdy = values[:, 1]

                #created a function so it can potentially be accelerated by using a GPU (not implemented)
                def counter(rdx_in, rdy_in, cell_radius, x, y):
                    count = 0
                    for rdx, rdy in zip(rdx_in, rdy_in):
                        if x - cell_radius < rdx and x + cell_radius > rdx and y - cell_radius < rdy and y + cell_radius > rdy:
                            count += 1
                    return count

                count = counter(rdx, rdy, self.cell_radius, x, y)

                grid_counter[idx_y - 1, idx_x] = count

            #create the square polygon
            lbrd = rijksdriehoek.Rijksdriehoek(x - self.cell_radius,
                                               y - self.cell_radius)
            rbrd = rijksdriehoek.Rijksdriehoek(x + self.cell_radius,
                                               y - self.cell_radius)
            rtrd = rijksdriehoek.Rijksdriehoek(x + self.cell_radius,
                                               y + self.cell_radius)
            ltrd = rijksdriehoek.Rijksdriehoek(x - self.cell_radius,
                                               y + self.cell_radius)

            leftbot = lbrd.to_wgs()
            rightbot = rbrd.to_wgs()
            righttop = rtrd.to_wgs()
            lefttop = ltrd.to_wgs()

            self.grid_polygon.append(
                f'POLYGON (({leftbot[1]} {leftbot[0]},{rightbot[1]} {rightbot[0]},{righttop[1]} {righttop[0]},{lefttop[1]} {lefttop[0]}))'
            )

        self.grid_counter = grid_counter
        print(f'Elapsed time is {time.time()-start} seconds...')
Exemple #6
0
    def build_design_matrix(self):
        '''
        ROADMAP:
        1. Get locations of the points
        1.1. Get grid point
        1.2. filter all point out larger than 1.5*R using kdtree
        2. Compute the r vector
        3. Set up the A matrix
        4. compute A matrix condition number
        '''

        #time keeping
        start = time.time()

        kdtree = spatial.cKDTree(self.data.values)

        for R in self.R:
            radius = R
            grid_counter = np.zeros((len(self.yrange), len(self.xrange)))

            p = np.zeros((len(self.xrange), len(self.yrange), 3))
            #step 1.1
            for idx_y, y in enumerate(self.yrange):
                for idx_x, x in enumerate(self.xrange):
                    #step 1.2
                    subset = kdtree.query_ball_point([x, y], r=radius)

                    #step 2
                    if not subset:
                        r = 0
                    else:
                        rd = self.data.iloc[subset].values
                        r = np.sqrt((rd[:, 0] - x)**2 + (rd[:, 1] - y)**2)

                        #step 3

                    def kinematicModel(R, r):
                        return np.array(
                            (1 / R**2) * np.exp(-np.pi * ((r**2) / (R**2))))

                    # A = kinematicModel(R,r)

                    #step 4
                    if isinstance(r, int) or len(r) == 1:
                        grid_counter[idx_y, idx_x] = 0
                    else:
                        #intermezzo. Create A matrix with multiple R values
                        t = np.arange(5)
                        R_list = [R]  #,20,30]
                        n_R_list = len(R_list)
                        n_r = len(r)

                        temp = np.zeros(
                            (len(t) * n_r * n_R_list, n_R_list * 3))

                        for i, iR in enumerate(R_list):

                            # iR = 20

                            for it in t:
                                temp1 = np.array([
                                    kinematicModel(iR, r) * (it**2),
                                    kinematicModel(iR, r) * it,
                                    kinematicModel(iR, r)
                                ]).T

                                if it == 0:
                                    temp2 = temp1
                                else:
                                    temp2 = np.concatenate((temp2, temp1),
                                                           axis=0)
                                # print(temp2)
                                # input()

                            # print(n_r,n_R_list)
                            # print(temp2)
                            # print(temp2.shape)
                            # print(temp.shape)

                            # print(i)
                            # input()

                            temp[i * n_r * len(t):n_r * len(t) +
                                 i * n_r * len(t), i * 3:3 + i * 3] = temp2

                            # temp = temp.T

                            # print(temp)
                            # input()

                            # if i == 0:
                            #     A_matrix = temp
                            # else:
                            #     A_matrix = np.concatenate((A_matrix,temp),axis=1)

                            # print(temp)
                            # input()

                        a = np.linalg.cond(temp)

                        # print(a)
                        # print(temp2)
                        # input()
                        if a > 1 / sys.float_info.epsilon:
                            grid_counter[idx_y, idx_x] = 0
                        else:
                            grid_counter[idx_y, idx_x] = 1

                        # grid_counter[idx_y,idx_x] = a

                    #create a polygon of the entry
                    lbrd = rsd.Rijksdriehoek(x - self.resolution,
                                             y - self.resolution)
                    rbrd = rsd.Rijksdriehoek(x + self.resolution,
                                             y - self.resolution)
                    rtrd = rsd.Rijksdriehoek(x + self.resolution,
                                             y + self.resolution)
                    ltrd = rsd.Rijksdriehoek(x - self.resolution,
                                             y + self.resolution)

                    leftbot = lbrd.to_wgs()
                    rightbot = rbrd.to_wgs()
                    righttop = rtrd.to_wgs()
                    lefttop = ltrd.to_wgs()

                    self.grid_polygon.append(
                        f'POLYGON (({leftbot[1]} {leftbot[0]},{rightbot[1]} {rightbot[0]},{righttop[1]} {righttop[0]},{lefttop[1]} {lefttop[0]}))'
                    )

        self.grid_counter = grid_counter
        print(f'Elapsed time is {time.time()-start} seconds...')