在rasterVis中绘制具有重叠多边形的栅格时的范围问题(scoping issues while plotting a raster with an overlayed polygon in rasterVis)
我正在开发一个基于
rasterVis::levelplot
的绘图功能,用户可以只传递一个栅格对象,或一个栅格对象和一个sf
多边形对象。该函数相当复杂,但显示问题的最小子集读作:
library(sf) library(raster) library(rasterVis) myplot <- function(in_rast, in_poly = NULL) { rastplot <- rasterVis::levelplot(in_rast, margin = FALSE) polyplot <- layer(sp::sp.polygons(in_poly)) print(rastplot + polyplot) }
问题是我在测试时看到了一些奇怪的(对我来说)结果。 让我们定义一些虚拟数据 - 一个1000x1000光栅和一个带有四个多边形的
sf
POYGON对象,它们分割光栅 - :in_rast <- raster(matrix(nrow = 1000, ncol = 1000)) in_rast <- setValues(in_rast, seq(1:1000000)) my_poly <- structure(list(cell_id = 1:4, geometry = structure(list(structure(list( structure(c(0, 0.5, 0.5, 0, 0, 0, 0, 0.5, 0.5, 0), .Dim = c(5L, 2L))), class = c("XY", "POLYGON", "sfg")), structure(list( structure(c(0.5, 1, 1, 0.5, 0.5, 0, 0, 0.5, 0.5, 0), .Dim = c(5L, 2L))), class = c("XY", "POLYGON", "sfg")), structure(list( structure(c(0, 0.5, 0.5, 0, 0, 0.5, 0.5, 1, 1, 0.5), .Dim = c(5L, 2L))), class = c("XY", "POLYGON", "sfg")), structure(list( structure(c(0.5, 1, 1, 0.5, 0.5, 0.5, 0.5, 1, 1, 0.5), .Dim = c(5L, 2L))), class = c("XY", "POLYGON", "sfg"))), n_empty = 0L, class = c("sfc_POLYGON", "sfc"), precision = 0, crs = structure(list(epsg = NA_integer_, proj4string = NA_character_), .Names = c("epsg", "proj4string" ), class = "crs"), bbox = structure(c(0, 0, 1, 1), .Names = c("xmin", "ymin", "xmax", "ymax")))), .Names = c("cell_id", "geometry"), row.names = c(NA, 4L), class = c("sf", "data.frame"), sf_column = "geometry", agr = structure(NA_integer_, class = "factor", .Label = c("constant", "aggregate", "identity"), .Names = "cell_id"))
并测试功能。 从理论上讲,我认为这应该有效:
my_poly <- as(my_poly, "Spatial") # convert to spatial myplot(in_rast, in_poly = my_poly)
但我得到:
这样做:
in_poly <- my_poly in_poly <- as(in_poly, "Spatial") myplot(in_rast, in_poly = in_poly)
仍然失败,但结果不同:
我发现让它工作的唯一方法是给多边形对象赋予我从函数内部使用的相同名称(即
in_poly
):in_poly <- structure(list(cell_id = 1:4, geometry = structure(list(structure(list( structure(c(0, 0.5, 0.5, 0, 0, 0, 0, 0.5, 0.5, 0), .Dim = c(5L, 2L))), class = c("XY", "POLYGON", "sfg")), structure(list( structure(c(0.5, 1, 1, 0.5, 0.5, 0, 0, 0.5, 0.5, 0), .Dim = c(5L, 2L))), class = c("XY", "POLYGON", "sfg")), structure(list( structure(c(0, 0.5, 0.5, 0, 0, 0.5, 0.5, 1, 1, 0.5), .Dim = c(5L, 2L))), class = c("XY", "POLYGON", "sfg")), structure(list( structure(c(0.5, 1, 1, 0.5, 0.5, 0.5, 0.5, 1, 1, 0.5), .Dim = c(5L, 2L))), class = c("XY", "POLYGON", "sfg"))), n_empty = 0L, class = c("sfc_POLYGON", "sfc"), precision = 0, crs = structure(list(epsg = NA_integer_, proj4string = NA_character_), .Names = c("epsg", "proj4string" ), class = "crs"), bbox = structure(c(0, 0, 1, 1), .Names = c("xmin", "ymin", "xmax", "ymax")))), .Names = c("cell_id", "geometry"), row.names = c(NA, 4L), class = c("sf", "data.frame"), sf_column = "geometry", agr = structure(NA_integer_, class = "factor", .Label = c("constant", "aggregate", "identity"), .Names = "cell_id")) in_poly <- as(in_poly, "Spatial") myplot(in_rast, in_poly = in_poly)
谁能解释这里发生了什么? 这显然是(?)一个范围问题,但我真的不明白为什么函数表现得像这样!
提前致谢 !
I'm working on a plotting function based on
rasterVis::levelplot
to which the user can pass either just a raster object, or a raster object and ansf
polygon object.The function is rather complex, but a minimum subset showing the problem reads as:
library(sf) library(raster) library(rasterVis) myplot <- function(in_rast, in_poly = NULL) { rastplot <- rasterVis::levelplot(in_rast, margin = FALSE) polyplot <- layer(sp::sp.polygons(in_poly)) print(rastplot + polyplot) }
The problem is that I see some strange (for me) results while testing it. Let's define some dummy data - a 1000x1000 raster and a
sf
POYGON oject with four polygons which split the raster -:in_rast <- raster(matrix(nrow = 1000, ncol = 1000)) in_rast <- setValues(in_rast, seq(1:1000000)) my_poly <- structure(list(cell_id = 1:4, geometry = structure(list(structure(list( structure(c(0, 0.5, 0.5, 0, 0, 0, 0, 0.5, 0.5, 0), .Dim = c(5L, 2L))), class = c("XY", "POLYGON", "sfg")), structure(list( structure(c(0.5, 1, 1, 0.5, 0.5, 0, 0, 0.5, 0.5, 0), .Dim = c(5L, 2L))), class = c("XY", "POLYGON", "sfg")), structure(list( structure(c(0, 0.5, 0.5, 0, 0, 0.5, 0.5, 1, 1, 0.5), .Dim = c(5L, 2L))), class = c("XY", "POLYGON", "sfg")), structure(list( structure(c(0.5, 1, 1, 0.5, 0.5, 0.5, 0.5, 1, 1, 0.5), .Dim = c(5L, 2L))), class = c("XY", "POLYGON", "sfg"))), n_empty = 0L, class = c("sfc_POLYGON", "sfc"), precision = 0, crs = structure(list(epsg = NA_integer_, proj4string = NA_character_), .Names = c("epsg", "proj4string" ), class = "crs"), bbox = structure(c(0, 0, 1, 1), .Names = c("xmin", "ymin", "xmax", "ymax")))), .Names = c("cell_id", "geometry"), row.names = c(NA, 4L), class = c("sf", "data.frame"), sf_column = "geometry", agr = structure(NA_integer_, class = "factor", .Label = c("constant", "aggregate", "identity"), .Names = "cell_id"))
and test the function. In theory, I think this should work:
my_poly <- as(my_poly, "Spatial") # convert to spatial myplot(in_rast, in_poly = my_poly)
but I get:
doing this:
in_poly <- my_poly in_poly <- as(in_poly, "Spatial") myplot(in_rast, in_poly = in_poly)
still fails, but with a different outcome:
The only way I found to have it working is to give to the polygon object the same name that I use inside the function (i.e.,
in_poly
) from the beginning :in_poly <- structure(list(cell_id = 1:4, geometry = structure(list(structure(list( structure(c(0, 0.5, 0.5, 0, 0, 0, 0, 0.5, 0.5, 0), .Dim = c(5L, 2L))), class = c("XY", "POLYGON", "sfg")), structure(list( structure(c(0.5, 1, 1, 0.5, 0.5, 0, 0, 0.5, 0.5, 0), .Dim = c(5L, 2L))), class = c("XY", "POLYGON", "sfg")), structure(list( structure(c(0, 0.5, 0.5, 0, 0, 0.5, 0.5, 1, 1, 0.5), .Dim = c(5L, 2L))), class = c("XY", "POLYGON", "sfg")), structure(list( structure(c(0.5, 1, 1, 0.5, 0.5, 0.5, 0.5, 1, 1, 0.5), .Dim = c(5L, 2L))), class = c("XY", "POLYGON", "sfg"))), n_empty = 0L, class = c("sfc_POLYGON", "sfc"), precision = 0, crs = structure(list(epsg = NA_integer_, proj4string = NA_character_), .Names = c("epsg", "proj4string" ), class = "crs"), bbox = structure(c(0, 0, 1, 1), .Names = c("xmin", "ymin", "xmax", "ymax")))), .Names = c("cell_id", "geometry"), row.names = c(NA, 4L), class = c("sf", "data.frame"), sf_column = "geometry", agr = structure(NA_integer_, class = "factor", .Label = c("constant", "aggregate", "identity"), .Names = "cell_id")) in_poly <- as(in_poly, "Spatial") myplot(in_rast, in_poly = in_poly)
Can anyone explain what's happening here ? It's clearly (?) a scoping problem, but I really do not understand why the function behaves like this !
Thanks in advance !
原文:https://stackoverflow.com/questions/45196667
最满意答案
mysqldump命令行程序为您执行此操作 - 尽管文档对此非常不清楚。
需要注意的是〜/ output / dir必须由拥有mysqld的用户写入。 在Mac OS X上:
sudo chown -R _mysqld:_mysqld ~/output/dir mysqldump --user=dbuser --password --tab=~/output/dir dbname
运行上述之后,您将有一个tablename.sql文件,其中包含每个表的模式(create table statement)和tablename.txt文件,其中包含数据。
如果要仅使用模式转储,请添加--no-data标志:
mysqldump --user=dbuser --password --no-data --tab=~/output/dir dbname
The mysqldump command line program does this for you - although the docs are very unclear about this.
One thing to note is that ~/output/dir has to be writable by the user that owns mysqld. On Mac OS X:
sudo chown -R _mysqld:_mysqld ~/output/dir mysqldump --user=dbuser --password --tab=~/output/dir dbname
After running the above, you will have one tablename.sql file containing each table's schema (create table statement) and tablename.txt file containing the data.
If you want a dump with schema only, add the --no-data flag:
mysqldump --user=dbuser --password --no-data --tab=~/output/dir dbname
相关问答
更多-
有什么数据库专业书籍介绍?[2022-05-19]
mysql -
Usage: mysqldump [OPTIONS] database [tables] 即 mysqldump -u username -p db_name table1_name table2_name table3_name > dump.sql Usage: mysqldump [OPTIONS] database [tables] i.e. mysqldump -u username -p db_name table1_name table2_name table3_name > dump. ...
-
mysqldump命令行程序为您执行此操作 - 尽管文档对此非常不清楚。 需要注意的是〜/ output / dir必须由拥有mysqld的用户写入。 在Mac OS X上: sudo chown -R _mysqld:_mysqld ~/output/dir mysqldump --user=dbuser --password --tab=~/output/dir dbname 运行上述之后,您将有一个tablename.sql文件,其中包含每个表的模式(create table statement)和 ...
-
我猜你可以使用以下命令来调用mysqldump来忽略转储时的特定表,这样你就可以避免在转储后删除表的风险 mysqldump -u username -p database_name --ignore-table=database_name.table1 --ignore-table=database_name.table2 > test.sql I guess you could take a mysqldump using the following command to ignore the spe ...
-
据我记忆 - MySQL Dumps几乎完全是插入语句集。 您可以在映射器中解析它们并按原样处理...如果您只有几个表,则在Java中进行硬编码解析应该是微不足道的。 As far as I remember - MySQL Dumps are almost entirely is set of insert statements. You can parse them in your mapper and process as is... If you have only few tables hard ...
-
ILogEventSink ,需要使用WriteTo.Sink(...)添加自定义ILogEventSink 。 与Serilog一起提供的JsonFormatter类使我相当快地实现了我的想法。 A custom ILogEventSink added with WriteTo.Sink(...) is needed for this. The JsonFormatter class shipped with Serilog makes this fairly quick to implement I t ...
-
如何使用一个存储过程从单独的表中返回mysql列?(How can I return mysql columns from separate tables with one stored procedure?)[2021-11-27]
你要做的事情的关键是双重的: 您需要定义多个OUT或INOUT变量以将输出结果放入。 您需要在PHP端检索那些输出变量。 对于1,请参见此处的第一个示例: CREATE PROCEDURE p (OUT ver_param VARCHAR(25), INOUT incr_param INT) BEGIN # Set value of OUT parameter SELECT VERSION() INTO ver_param; # Increment value of INOUT paramete ... -
我认为你的命令行参数的顺序是错误的(重定向到文件应该是结束),并且你需要一个额外的xargs参数,所以我们可以指定mysqldump的数据库名称。 此外,您需要附加>>转储数据,否则您将覆盖每个表的mydump.sql文件: mysqldump -u root -pmypassword mydb --no-data > mydump.sql mysql -u root -pmypassword -N < showtables.txt | xargs -I {} mysqldump -v -u root -p ...
-
您可以使用append方法,就像我们附加常规文件一样 eg: cat file2 >> file1 您可以使用“>>”而不是“>”转储 “>”覆盖“>>”附加到目标文件的目标文件。 mysqldump -u... -p... mydb t1 t2 > mydb_tables.sql mysqldump -u... -p... mydb t3 t4 >> mydb_tables.sql You can use append method, like we append the regular ...
-
.bz2扩展名通常表示BZ2压缩存档 。 要解压缩 : bzip2 -d foo.sql.bz2 # produces file "foo.sql" 结合find ,魔术发生: find /path/to/dump/directory -name "*.sql.bz2" | xargs bzip2 -cd {} | mysql [options] The .bz2 extension usually denotes a BZ2-compressed archive. To decompress: bzi ...