[GRASS-SVN] r65323 - in grass/trunk: include/defs lib/raster
svn_grass at osgeo.org
svn_grass at osgeo.org
Tue May 26 04:52:15 PDT 2015
Author: glynn
Date: 2015-05-26 04:52:14 -0700 (Tue, 26 May 2015)
New Revision: 65323
Modified:
grass/trunk/include/defs/raster.h
grass/trunk/lib/raster/close.c
grass/trunk/lib/raster/open.c
Log:
Fix bugs in r65272 (compressed nulls)
Add Rast__open_null_write, Rast__close_null
Modified: grass/trunk/include/defs/raster.h
===================================================================
--- grass/trunk/include/defs/raster.h 2015-05-26 11:51:00 UTC (rev 65322)
+++ grass/trunk/include/defs/raster.h 2015-05-26 11:52:14 UTC (rev 65323)
@@ -100,6 +100,7 @@
void Rast_close(int);
void Rast_unopen(int);
void Rast__unopen_all(void);
+void Rast__close_null(int);
/* color_compat.c */
void Rast_make_ryg_colors(struct Colors *, CELL, CELL);
@@ -454,6 +455,7 @@
int Rast_open_new(const char *, RASTER_MAP_TYPE);
int Rast_open_new_uncompressed(const char *, RASTER_MAP_TYPE);
void Rast_set_quant_rules(int, struct Quant *);
+int Rast__open_null_write(const char *);
/* put_cellhd.c */
void Rast_put_cellhd(const char *, struct Cell_head *);
@@ -463,7 +465,6 @@
void Rast_put_c_row(int, const CELL *);
void Rast_put_f_row(int, const FCELL *);
void Rast_put_d_row(int, const DCELL *);
-int Rast__open_null_write(int);
void Rast__write_null_bits(int, const unsigned char *, int, int, int);
/* put_title.c */
Modified: grass/trunk/lib/raster/close.c
===================================================================
--- grass/trunk/lib/raster/close.c 2015-05-26 11:51:00 UTC (rev 65322)
+++ grass/trunk/lib/raster/close.c 2015-05-26 11:52:14 UTC (rev 65323)
@@ -344,10 +344,6 @@
if (fcb->gdal)
return close_new_gdal(fd, ok);
- if (fcb->null_fd >= 0)
- close(fcb->null_fd);
- fcb->null_fd = -1;
-
if (ok) {
switch (fcb->open_mode) {
case OPEN_NEW_COMPRESSED:
@@ -433,6 +429,10 @@
close(fcb->data_fd);
fcb->open_mode = -1;
+ if (fcb->null_fd >= 0)
+ close(fcb->null_fd);
+ fcb->null_fd = -1;
+
if (fcb->data != NULL)
G_free(fcb->data);
@@ -479,6 +479,47 @@
return stat;
}
+void Rast__close_null(int fd)
+{
+ struct fileinfo *fcb = &R__.fileinfo[fd];
+ char path[GPATH_MAX];
+
+ if (fcb->null_row_ptr) { /* compressed nulls */
+ fcb->null_row_ptr[fcb->cellhd.rows] = lseek(fcb->null_fd, 0L, SEEK_CUR);
+ Rast__write_null_row_ptrs(fd, fcb->null_fd);
+ G_free(fcb->null_row_ptr);
+ }
+
+ if (fcb->null_fd >= 0)
+ close(fcb->null_fd);
+ fcb->null_fd = -1;
+
+ /* create path : full null file name */
+ G__make_mapset_element_misc("cell_misc", fcb->name);
+ G_file_name_misc(path, "cell_misc", NULL_FILE, fcb->name, G_mapset());
+ remove(path);
+ G_file_name_misc(path, "cell_misc", NULL2_FILE, fcb->name, G_mapset());
+ remove(path);
+
+ G_file_name_misc(path, "cell_misc",
+ fcb->null_row_ptr ? NULL2_FILE : NULL_FILE,
+ fcb->name, G_mapset());
+
+ if (rename(fcb->null_temp_name, path))
+ G_warning(_("Unable to rename null file '%s' to '%s': %s"),
+ fcb->null_temp_name, path, strerror(errno));
+ remove(fcb->null_temp_name);
+
+ G_free(fcb->null_temp_name);
+
+ G_free(fcb->name);
+ G_free(fcb->mapset);
+
+ G_free(fcb->null_bits);
+
+ fcb->open_mode = -1;
+}
+
/* returns 0 on success, 1 on failure */
static void write_fp_format(int fd)
{
Modified: grass/trunk/lib/raster/open.c
===================================================================
--- grass/trunk/lib/raster/open.c 2015-05-26 11:51:00 UTC (rev 65322)
+++ grass/trunk/lib/raster/open.c 2015-05-26 11:52:14 UTC (rev 65323)
@@ -332,27 +332,21 @@
fcb->null_row_ptr = NULL;
if (!gdal) {
- if (!G_find_file2_misc("cell_misc", NULL_FILE, r_name, r_mapset)) {
- /* G_warning("unable to find [%s]",path); */
- fcb->null_file_exists = 0;
- }
- else {
- /* First, check for compressed null file */
- fcb->null_fd = G_open_old_misc("cell_misc", NULL_FILE, r_name, r_mapset);
- if (fcb->null_fd < 0) {
- fcb->null_fd = G_open_old_misc("cell_misc", NULL2_FILE, r_name, r_mapset);
- if (fcb->null_fd >= 0) {
- fcb->null_row_ptr = G_calloc(fcb->cellhd.rows + 1, sizeof(off_t));
- if (Rast__read_null_row_ptrs(fd, fcb->null_fd) < 0) {
- close(fcb->null_fd);
- fcb->null_fd = -1;
- G_free(fcb->null_row_ptr);
- fcb->null_row_ptr = NULL;
- }
+ /* First, check for compressed null file */
+ fcb->null_fd = G_open_old_misc("cell_misc", NULL_FILE, r_name, r_mapset);
+ if (fcb->null_fd < 0) {
+ fcb->null_fd = G_open_old_misc("cell_misc", NULL2_FILE, r_name, r_mapset);
+ if (fcb->null_fd >= 0) {
+ fcb->null_row_ptr = G_calloc(fcb->cellhd.rows + 1, sizeof(off_t));
+ if (Rast__read_null_row_ptrs(fd, fcb->null_fd) < 0) {
+ close(fcb->null_fd);
+ fcb->null_fd = -1;
+ G_free(fcb->null_row_ptr);
+ fcb->null_row_ptr = NULL;
}
}
- fcb->null_file_exists = fcb->null_fd >= 0;
}
+ fcb->null_file_exists = fcb->null_fd >= 0;
}
return fd;
@@ -716,6 +710,60 @@
return fd;
}
+int Rast__open_null_write(const char *name)
+{
+ char xname[GNAME_MAX], xmapset[GMAPSET_MAX];
+ struct fileinfo *fcb;
+ int fd;
+ char *tempname;
+ char *map;
+ char *mapset;
+
+ Rast__init();
+
+ if (!G_find_raster2(name, G_mapset()))
+ G_fatal_error(_("Raster map <%s> does not exist in the current mapset (%s)"),
+ name, G_mapset());
+
+ if (G_unqualified_name(name, G_mapset(), xname, xmapset) < 0)
+ G_fatal_error(_("Raster map <%s> is not in the current mapset (%s)"),
+ name, G_mapset());
+ map = G_store(xname);
+ mapset = G_store(xmapset);
+
+ fd = new_fileinfo();
+ fcb = &R__.fileinfo[fd];
+
+ G_zero(fcb, sizeof(*fcb));
+
+ fcb->name = map;
+ fcb->mapset = mapset;
+
+ Rast_get_cellhd(map, mapset, &fcb->cellhd);
+
+ /* open a null tempfile name */
+ tempname = G_tempfile();
+ fcb->null_fd = creat(tempname, 0666);
+ if (fcb->null_fd < 0) {
+ G_free(tempname);
+ G_free(fcb->name);
+ G_free(fcb->mapset);
+ G_fatal_error(_("No temp files available: %s"), strerror(errno));
+ }
+ fcb->null_temp_name = tempname;
+
+ if (R__.compress_nulls) {
+ fcb->null_row_ptr = G_calloc(fcb->cellhd.rows + 1, sizeof(off_t));
+ G_zero(fcb->row_ptr, (fcb->cellhd.rows + 1) * sizeof(off_t));
+ Rast__write_null_row_ptrs(fd, fcb->null_fd);
+ }
+
+ /* allocate null bitstream buffer for writing */
+ fcb->null_bits = Rast__allocate_null_bits(fcb->cellhd.cols);
+
+ return fd;
+}
+
/*!
\brief Set raster map floating-point data format.
More information about the grass-commit
mailing list