爱气象,爱气象家园! 

气象家园

 找回密码
 立即注册

QQ登录

只需一步,快速开始

新浪微博登陆

只需一步, 快速开始

搜索
查看: 2344|回复: 6

[分享资料] grd转存shp文件的写出问题

[复制链接]

新浪微博达人勋

发表于 2016-10-21 17:45:11 | 显示全部楼层 |阅读模式

登录后查看更多精彩内容~

您需要 登录 才可以下载或查看,没有帐号?立即注册 新浪微博登陆

x
本帖最后由 Neco8101 于 2016-10-22 10:12 编辑

如图,系统已经提示将数据写入shapefile rain,但是这个rain.shp怎么输出呢?主要是用什么命令以及怎么设置路径的问题。谢谢大家!
QQ截图20161021174343.jpg
密码修改失败请联系微信:mofangbao

新浪微博达人勋

发表于 2016-10-22 10:25:18 | 显示全部楼层
活学活用的本领还挺厉害,fwrite命令是输出数据的命令,它本身怎么又能是 变量呢。建议你好好看下grads关于shp的官方文档吧。

The Shapefile Interface in GrADS

Introduction
What is a shapefile?
Drawing shapefiles
Querying shapefiles
Relevant Environment Variables
Writing shapefiles
Examples
Caveat

line

Introduction
GrADS version 2.0.a8 introduced the new capability to draw and query the contents of a shapefile; version 2.0.a9 added the capability to create new shapefiles from GrADS gridded or station data sets. This documentation page serves as an overview of the GrADS shapefile inteface, providing guidance on what shapefiles are, how to find out what they contain, how to draw their contents, and how to create new shapefiles.

What is a Shapefile?
The shapefile format (promulgated by ESRI) stores non-topological geometry and attribute information for the spatial features in a data set. Shapefiles are used extensively in Geographic Information Systems (GIS) and they typically contain data related to coastlines, political boundaries, state or county boundaries, climate zones, roads, rivers, topography, etc. The geometry of each spatial feature or 'shape' is stored as a set of vector coordinates. Shapes can be points, lines, or polygons (areas). An ESRI shapefile actually consists of several files: a main file (*.shp), an index file (*.shx), a dBASE file (*.dbf), and an optional projection file (*.prj). The main file describes a set of shapes with a list of their vertices. The index file contains offsets pointing to the locations of each shape in the main file. The dBASE file contains attribute data for each shape in the main file. The projection file contains specifics about the spherical geometry; it is an optional but useful component of a shapefile.

Some resources for freely available shapefiles are:

National Weather Service Shapefile Database
National Atlas
DIVA-GIS
Natural Earth
Drawing Shapefiles
GrADS will draw the contents of a shapefile as an overlay on top of an existing plot with the command draw shp. It is necessary to draw a plot first in order to establish the dimensions and scaling of the display. Shapefiles contain 2-dimensional spatial features, so your plot must be varying in the X-Y (lon/lat) domain.

A shapefile may contain one of three kinds of graphical elements: points, lines, or polygons.

For shapefiles that contain points, GrADS will draw a mark at each point location. The mark type and size are controlled by the set shpopts command, and the color is controlled by the set line command.
For shapefiles that contain lines, GrADS will draw the line elements using the color, style, and thickness settings that are controlled by the set line command.
For shapefiles that contain polygons, the default behavior of GrADS is draw only the perimeter of each polygon element.The color, style, and thickness of the polygon perimeters are controlled by the set line command. Use the set shpopts command to draw filled polygons and set the fill color.
The draw shp command draws all the shapes in the shapefile by default, but it is possible to specify a single shape or a range of shapes to draw. Using these extra arguments to the draw shp command, you can control the line and color characteristics of individual shapes. The q shpopts command lists the current settings for drawing shapefiles.

Querying Shapefiles
The GrADS shapefile interface also allows the user to discover the contents of a shapefile. The q shp command returns information about the shapes in the file (number of vertices and lon/lat bounds), and the q dbf command returns all the attribute information for each shape in the file.

The first line of the q shp output contains the shapefile type, the number of shapes in the file, and the X and Y bounds of actual extent of the shapes in the file. Subsequent lines list information about each shape element in the file: the identification number, the shape type, the number of parts, the number of vertices, and the bounds of the shape in the X, Y, Z, and M (measure) dimesions. The GrADS interface ignores the Z or M values of a shape, only it's position in X,Y (lon,lat) space is relevant.

The first line of the q dbf output contains a comma-delimited list of the names of all the attributes for each record in the database. Subsequent lines contain the comma-delimited list of all the attribute values for each record.

Please refer to the reference pages for q shp and q dbf for additional information and examples of the output.

Relevant Environment Variables
When drawing and querying shapefiles, if you put the set of component files (*.shp, *.shx, *.dbf) in the GrADS data directory (specified by the environment variable GADDIR), then it is not necessary to include the full path when referring to the shapefile. Starting with GrADS version 2.0.0, a new environment variable was introduced, GASHP, for specifiying a list of directories that contain shapefiles. Multiple directories may be included in the GASHP variable -- acceptable delimters are a space, a comma, a semi-colon, and a colon. GrADS will search for the requested shapefile in the directories listed in the GASHP environment variable before it looks in the GADDIR directory. Note that GrADS does not use the projection information (*.prj) when drawing the contents of a shapefile.

Writing Shapefiles
GrADS version 2.0.a9 adds the capability of writing out shapefiles based on gridded or station data sets. The shape types currently supported are points and lines. Point shapes may be created from station data or from individual grid point values. The GrADS station data format is a natural fit for point shapefiles, but grid point values are also useful in the shapefile format -- they can be used to create a stippled overlay on top of shaded contour plot (see Examples below). Line shapes are created by applying the contouring algorithm to a grid expression. The settings to control contour levels apply when creating line shapefiles. Both point and line shape types are 'measured' in that the data value at each point (or the contour level) are included in the shapefile along with the geographic coordinates.

To write out a shapefile, there is a new graphics output option, set gxout shp. This graphics option is used in conjunction with the set shp command which allows the user to set the filename root for shapefile output, control the type of shapefile to be created, and set formatting options for numeric values. The display command will then create the four shapefile output files instead of drawing a plot.

The dBase file created with the shapefile contains metadata fields or attributes for each shape. There are three data types for attributes: string, integer, or double. Attributes may be static (the attribute value is the same for each shape) or dynamic (the attribute value varies with shape). A set of attributes (both static and dynamic) are automatically generated by GrADS for all shapes. Attributes for point shapes from station data are: GrADS version, longitude, latitude, station ID, and the station value. Attributes for point shapes from gridded are: GrADS version, longitude, latitude, and the grid value. Attributes for line shapes (contours) are: GrADS version and the contour value. Additional static attributes for each shape may be assigned by the user with the set shpattr command. Numerical attribute values (integers and doubles) are written to the dBase file in text format. The set shp command controls the formatting of the numbers, by specifiying the length of the number (total number of columns) and the precision (number of places to the right of the decimal place, which is only meaningful for attributes of type double).

The q shpopts command lists the current settings for shapefile output, including the output file name root, the type of shapefile to be created, the formatting of any numeric output, and any user-provided attributes.

The clear shp command releases all user-defined shapefile attributes from memory, and resets the output filename root and shapefile type to their default values. The reset and reinit commands will do the same thing -- use clear shp if you do not want to reset all the other user settings.

Examples

Here are two examples showing a series of commands to create shapefiles:

set gxout shp
set shp -pt -fmt 8 4 gridptm
set shpattr AUTHOR string JMA
set shpattr TYPE string grid points
set shpattr DESC string land surface temperature
d maskout(tsfc,landmask-1)
  
set gxout shp
clear shp
set shp -ln -fmt 8 4 linem
set shpattr AUTHOR string JMA
set shpattr TYPE string grid contours
set shpattr DESC string surface temperature
d tsfc

This example shows how to use a semi-transparent color to fill in the land areas and avoid cluttering your graphic with a map outline.
Transparent colors will only work with GrADS version 2.1+.

'open model.ctl'
'set lon -180 180'
'set mproj robinson'
'set mpdraw off'
'set gxout shaded'
'd z(lev=300)'
'set rgb 20 1 1 1 -70'
'set line 20'
'set shpopts 20'
'draw shp grads_lowres_land'
shp demo 4





Caveat
The ESRI Shapefile Techncial Description states that "A polygon consists of one or more rings. A ring is a connected sequence of four or more points that form a closed, non-self-intersecting loop." A polygon with two rings would look something like a flat donut -- the area between the two rings forms the interior of the polygon, and the area enclosed by the inner ring is actually in the exterior of the polygon. The GrADS interface does not draw polygons with more than one ring properly. Each ring in a polygon is handled separately; inner rings are drawn as new polygons overlapping the outer ring. If you are drawing a shapefile that contains polygons with more than one ring, and if you use the set shpopts command to draw filled polygons, then GrADS will draw both the inner and outer rings as filled polygons. In other words, your donuts will be drawn with filled holes.

The rings of a polygon are referred to as its parts, and the number of parts in each polygon is included in the ouput of the q shp command. So it is possible to discover whether this limitation is affecting your plot.
密码修改失败请联系微信:mofangbao

新浪微博达人勋

 楼主| 发表于 2016-10-22 10:30:50 | 显示全部楼层
river 发表于 2016-10-22 10:25
活学活用的本领还挺厉害,fwrite命令是输出数据的命令,它本身怎么又能是 变量呢。建议你好好看下grads关于 ...

谢谢!这个文档我正在研究,但是还是不明白已经写入shp的文件到底要怎么导出,在文档里好像也没找到在哪里写入硬盘。本人是学GIS的,最近刚接触GrADS,可能有时候对GrADS理解不是很到位~
密码修改失败请联系微信:mofangbao

新浪微博达人勋

 楼主| 发表于 2016-10-22 10:39:23 | 显示全部楼层
river 发表于 2016-10-22 10:25
活学活用的本领还挺厉害,fwrite命令是输出数据的命令,它本身怎么又能是 变量呢。建议你好好看下grads关于 ...

我这两天一直在这个文档里面绕来绕去,觉得他都没有给出一个输出的命令,就是没有给出命令+路径的形式,所以即使数据已经写入shp但是还是不知道数据在哪里
密码修改失败请联系微信:mofangbao

新浪微博达人勋

发表于 2016-10-22 11:37:25 | 显示全部楼层
Neco8101 发表于 2016-10-22 10:39
我这两天一直在这个文档里面绕来绕去,觉得他都没有给出一个输出的命令,就是没有给出命令+路径的形式, ...

set shp

set shp <-type> <-fmt n m > fnameroot

Sets the filename root for shapefile output and has additional arguments to control the type of shapefile created and the formatting of numerical values in the dBase file. This command is used in conjunction with set gxout shp and display to create four complementary files:

fnameroot.shp    The main binary file in which each record describes a shape with a list of vertices (default is grads.shp)
fnameroot.shx    The index file contains the offsets for each record in the main file (default is grads.shx)
fnameroot.dbf    A dBase table that contains feature attributes for each record in the main file (default is grads.dbf)
fnameroot.prj    A text file that describes the default projection: the WGS84 lon-lat spheroid (default is grads.prj)

Where type may be one of the following:

ln | line        (default) Shapefile output will contain shapes of the type PolyLineM (measured contour lines)
pt | point       Shapefile output will contain shapes of the type PointM (measured points)
poly             Shapefile output will contain polygons of the type PolygonM (measured polygons)

-fmt n m         Optional controls for the formatting of the numerical values in the dBase file.
                 n is the maximum number of digits to be used when formatting integers and doubles in the data base entry (default is 12)
                 m is the number of precsion decimal places to be used when formatting doubles in the data base entry (default is 6)
                 Both m and n must be provided when using the -fmt option



Usage Notes

This command is available in GrADS v2.0.a9 or later. The -poly option is available in version 2.0.0 or later.

The files fnameroot.* will be replaced if they exist.

When creating a shapefile with contour lines, the cterp setting is ignored -- contours are written out as if cterp is set to off (i.e. no spline fitting).

Use the q shpopts command to get information on the status of the shapefile options; use the clear shp command to reset shapefile options to their default values.

The output in shapefile format is intended for use with GIS tools, and can also be drawn with GrADS. Please see the documentation page on shapefiles for more information.

Examples

set gxout shp
set shp -ln -fmt 8 3 my_shapefile_name
d my_var

set shp -poly mypolyfile
set shpattr author string your_name
d my_var
Note: The GrADS expression my_var must be a 2-dimensional grid that varies in X and Y (longitude and latitude). In this example, GrADS will use the format string %8.3f to write out double precision numbers to the dBase file.

fnameroot前面应该是可以直接加路径的,你试一下。如果加路径不行的话,你设置一个名字,然后在grads的安装文件夹下搜索就应该可以找到的,不出意外应该默认在~\OpenGrADS\Contents\Resources\SampleDatasets
密码修改失败请联系微信:mofangbao

新浪微博达人勋

 楼主| 发表于 2016-10-23 22:07:03 | 显示全部楼层
river 发表于 2016-10-22 11:37
set shp

set shp   fnameroot

已经可以了,用fwrite函数输出。不过能输出了老师又说用IDL写批处理……无语……
密码修改失败请联系微信:mofangbao

新浪微博达人勋

发表于 2016-10-24 00:09:54 | 显示全部楼层
Neco8101 发表于 2016-10-23 22:07
已经可以了,用fwrite函数输出。不过能输出了老师又说用IDL写批处理……无语……

好吧,之前怎么会出问题呢
密码修改失败请联系微信:mofangbao
您需要登录后才可以回帖 登录 | 立即注册 新浪微博登陆

本版积分规则

Copyright ©2011-2014 bbs.06climate.com All Rights Reserved.  Powered by Discuz! (京ICP-10201084)

本站信息均由会员发表,不代表气象家园立场,禁止在本站发表与国家法律相抵触言论

快速回复 返回顶部 返回列表