Esempio n. 1
0
    def getfeatureinfo(self, layer, request):
        with self.dataset() as nc:
            with self.topology() as topo:
                data_obj = nc.variables[layer.access_name]

                geo_index, closest_x, closest_y, start_time_index, end_time_index, return_dates = self.setup_getfeatureinfo(topo, data_obj, request)

                return_arrays = []
                z_value = None
                if isinstance(layer, Layer):
                    if len(data_obj.shape) == 4:
                        z_index, z_value = self.nearest_z(layer, request.GET['elevation'])
                        data = data_obj[start_time_index:end_time_index, z_index, geo_index[0], geo_index[1]]
                    elif len(data_obj.shape) == 3:
                        data = data_obj[start_time_index:end_time_index, geo_index[0], geo_index[1]]
                    elif len(data_obj.shape) == 2:
                        data = data_obj[geo_index[0], geo_index[1]]
                    else:
                        raise ValueError("Dimension Mismatch: data_obj.shape == {0} and time indexes = {1} to {2}".format(data_obj.shape, start_time_index, end_time_index))

                    return_arrays.append((layer.var_name, data))

                elif isinstance(layer, VirtualLayer):

                    # Data needs to be [var1,var2] where var are 1D (nodes only, elevation and time already handled)
                    for l in layer.layers:
                        if len(data_obj.shape) == 4:
                            z_index, z_value = self.nearest_z(layer, request.GET['elevation'])
                            data = data_obj[start_time_index:end_time_index, z_index, geo_index[0], geo_index[1]]
                        elif len(data_obj.shape) == 3:
                            data = data_obj[start_time_index:end_time_index, geo_index[0], geo_index[1]]
                        elif len(data_obj.shape) == 2:
                            data = data_obj[geo_index[0], geo_index[1]]
                        else:
                            raise ValueError("Dimension Mismatch: data_obj.shape == {0} and time indexes = {1} to {2}".format(data_obj.shape, start_time_index, end_time_index))
                        return_arrays.append((l.var_name, data))

                # Data is now in the return_arrays list, as a list of numpy arrays.  We need
                # to add time and depth to them to create a single Pandas DataFrame
                if len(data_obj.shape) == 4:
                    df = pd.DataFrame({'time': return_dates,
                                       'x': closest_x,
                                       'y': closest_y,
                                       'z': z_value})
                elif len(data_obj.shape) == 3:
                    df = pd.DataFrame({'time': return_dates,
                                       'x': closest_x,
                                       'y': closest_y})
                elif len(data_obj.shape) == 2:
                    df = pd.DataFrame({'x': closest_x,
                                       'y': closest_y})
                else:
                    df = pd.DataFrame()

                # Now add a column for each member of the return_arrays list
                for (var_name, np_array) in return_arrays:
                    df.loc[:, var_name] = pd.Series(np_array, index=df.index)

                return gfi_handler.from_dataframe(request, df)
Esempio n. 2
0
    def getfeatureinfo(self, layer, request):
        with self.dataset() as nc:
            with self.topology() as topo:
                data_obj = nc.variables[layer.access_name]
                data_location = data_obj.location
                # mesh_name = data_obj.mesh
                # Use local topology for pulling bounds data
                # ug = UGrid.from_ncfile(self.topology_file, mesh_name=mesh_name)

                geo_index, closest_x, closest_y, start_time_index, end_time_index, return_dates = self.setup_getfeatureinfo(
                    topo, data_obj, request, location=data_location)

                logger.info("Start index: {}".format(start_time_index))
                logger.info("End index: {}".format(end_time_index))
                logger.info("Geo index: {}".format(geo_index))

                return_arrays = []
                z_value = None
                if isinstance(layer, Layer):
                    if len(data_obj.shape) == 3:
                        z_index, z_value = self.nearest_z(
                            layer, request.GET['elevation'])
                        data = data_obj[start_time_index:end_time_index,
                                        z_index, geo_index]
                    elif len(data_obj.shape) == 2:
                        data = data_obj[start_time_index:end_time_index,
                                        geo_index]
                    elif len(data_obj.shape) == 1:
                        data = data_obj[geo_index]
                    else:
                        raise ValueError(
                            "Dimension Mismatch: data_obj.shape == {0} and time indexes = {1} to {2}"
                            .format(data_obj.shape, start_time_index,
                                    end_time_index))

                    return_arrays.append((layer.var_name, data))

                elif isinstance(layer, VirtualLayer):

                    # Data needs to be [var1,var2] where var are 1D (nodes only, elevation and time already handled)
                    for l in layer.layers:
                        data_obj = nc.variables[l.var_name]
                        if len(data_obj.shape) == 3:
                            z_index, z_value = self.nearest_z(
                                layer, request.GET['elevation'])
                            data = data_obj[start_time_index:end_time_index,
                                            z_index, geo_index]
                        elif len(data_obj.shape) == 2:
                            data = data_obj[start_time_index:end_time_index,
                                            geo_index]
                        elif len(data_obj.shape) == 1:
                            data = data_obj[geo_index]
                        else:
                            raise ValueError(
                                "Dimension Mismatch: data_obj.shape == {0} and time indexes = {1} to {2}"
                                .format(data_obj.shape, start_time_index,
                                        end_time_index))

                        return_arrays.append((l.var_name, data))

                # Data is now in the return_arrays list, as a list of numpy arrays.  We need
                # to add time and depth to them to create a single Pandas DataFrame
                if (len(data_obj.shape) == 3):
                    df = pd.DataFrame({
                        'time': return_dates,
                        'x': closest_x,
                        'y': closest_y,
                        'z': z_value
                    })
                elif (len(data_obj.shape) == 2):
                    df = pd.DataFrame({
                        'time': return_dates,
                        'x': closest_x,
                        'y': closest_y
                    })
                elif (len(data_obj.shape) == 1):
                    df = pd.DataFrame({'x': closest_x, 'y': closest_y})
                else:
                    df = pd.DataFrame()

                # Now add a column for each member of the return_arrays list
                for (var_name, np_array) in return_arrays:
                    df.loc[:, var_name] = pd.Series(np_array, index=df.index)

                return gfi_handler.from_dataframe(request, df)
Esempio n. 3
0
    def getfeatureinfo(self, layer, request):
        with self.dataset() as nc:
            data_obj = nc.variables[layer.access_name]

            geo_index, closest_x, closest_y, start_time_index, end_time_index, return_dates = self.setup_getfeatureinfo(
                layer, request)

            return_arrays = []
            z_value = None
            if isinstance(layer, Layer):
                if len(data_obj.shape) == 4:
                    z_index, z_value = self.nearest_z(layer,
                                                      request.GET['elevation'])
                    data = data_obj[start_time_index:end_time_index, z_index,
                                    geo_index[0], geo_index[1]]
                elif len(data_obj.shape) == 3:
                    data = data_obj[start_time_index:end_time_index,
                                    geo_index[0], geo_index[1]]
                elif len(data_obj.shape) == 2:
                    data = data_obj[geo_index[0], geo_index[1]]
                else:
                    raise ValueError(
                        "Dimension Mismatch: data_obj.shape == {0} and time indexes = {1} to {2}"
                        .format(data_obj.shape, start_time_index,
                                end_time_index))

                return_arrays.append((layer.var_name, data))

            elif isinstance(layer, VirtualLayer):

                # Data needs to be [var1,var2] where var are 1D (nodes only, elevation and time already handled)
                for l in layer.layers:
                    if len(data_obj.shape) == 4:
                        z_index, z_value = self.nearest_z(
                            layer, request.GET['elevation'])
                        data = data_obj[start_time_index:end_time_index,
                                        z_index, geo_index[0], geo_index[1]]
                    elif len(data_obj.shape) == 3:
                        data = data_obj[start_time_index:end_time_index,
                                        geo_index[0], geo_index[1]]
                    elif len(data_obj.shape) == 2:
                        data = data_obj[geo_index[0], geo_index[1]]
                    else:
                        raise ValueError(
                            "Dimension Mismatch: data_obj.shape == {0} and time indexes = {1} to {2}"
                            .format(data_obj.shape, start_time_index,
                                    end_time_index))
                    return_arrays.append((l.var_name, data))

            # Data is now in the return_arrays list, as a list of numpy arrays.  We need
            # to add time and depth to them to create a single Pandas DataFrame
            if len(data_obj.shape) == 4:
                df = pd.DataFrame({
                    'time': return_dates,
                    'x': closest_x,
                    'y': closest_y,
                    'z': z_value
                })
            elif len(data_obj.shape) == 3:
                df = pd.DataFrame({
                    'time': return_dates,
                    'x': closest_x,
                    'y': closest_y
                })
            elif len(data_obj.shape) == 2:
                df = pd.DataFrame({'x': closest_x, 'y': closest_y})
            else:
                df = pd.DataFrame()

            # Now add a column for each member of the return_arrays list
            for (var_name, np_array) in return_arrays:
                df.loc[:, var_name] = pd.Series(np_array, index=df.index)

            return gfi_handler.from_dataframe(request, df)