gislib.dox 80 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711171217131714171517161717171817191720172117221723172417251726172717281729173017311732173317341735173617371738173917401741174217431744174517461747174817491750175117521753175417551756175717581759176017611762176317641765176617671768176917701771177217731774177517761777177817791780178117821783178417851786178717881789179017911792179317941795179617971798179918001801180218031804180518061807180818091810181118121813181418151816181718181819182018211822182318241825182618271828182918301831183218331834183518361837183818391840184118421843184418451846184718481849185018511852185318541855185618571858185918601861186218631864186518661867186818691870187118721873187418751876187718781879188018811882188318841885188618871888188918901891189218931894189518961897189818991900190119021903190419051906190719081909191019111912191319141915191619171918191919201921192219231924192519261927192819291930193119321933193419351936193719381939194019411942194319441945194619471948194919501951195219531954195519561957195819591960196119621963196419651966196719681969197019711972197319741975197619771978197919801981198219831984198519861987198819891990199119921993199419951996199719981999200020012002200320042005200620072008200920102011201220132014201520162017201820192020202120222023202420252026202720282029203020312032203320342035203620372038203920402041204220432044204520462047204820492050205120522053205420552056205720582059206020612062206320642065206620672068206920702071207220732074207520762077207820792080208120822083208420852086208720882089209020912092209320942095209620972098209921002101210221032104210521062107210821092110211121122113211421152116211721182119212021212122212321242125212621272128212921302131213221332134213521362137213821392140214121422143214421452146214721482149215021512152215321542155215621572158215921602161216221632164216521662167216821692170217121722173217421752176217721782179218021812182218321842185218621872188218921902191219221932194219521962197219821992200220122022203220422052206220722082209221022112212221322142215221622172218221922202221222222232224222522262227222822292230223122322233223422352236223722382239224022412242224322442245224622472248224922502251225222532254225522562257225822592260226122622263226422652266226722682269227022712272227322742275227622772278227922802281228222832284228522862287228822892290229122922293229422952296229722982299230023012302230323042305230623072308230923102311231223132314231523162317231823192320232123222323232423252326232723282329233023312332233323342335233623372338233923402341234223432344234523462347234823492350235123522353235423552356235723582359236023612362236323642365236623672368236923702371237223732374237523762377237823792380238123822383238423852386238723882389239023912392239323942395239623972398239924002401240224032404240524062407240824092410241124122413241424152416241724182419242024212422242324242425242624272428242924302431243224332434
  1. /*! \page gislib GRASS GIS Library
  2. <!-- doxygenized from "GRASS 5 Programmer's Manual"
  3. by M. Neteler 2/2004, 2005, 2006
  4. -->
  5. by GRASS Development Team (http://grass.osgeo.org)
  6. This chapter is divided as follows:
  7. - \subpage gislibintro
  8. - \subpage init
  9. - \subpage diag
  10. - \subpage envir
  11. - \subpage dbaseaccess
  12. - \subpage Fully_Qualified_File_Names
  13. - \subpage finding
  14. - \subpage Legal_File_Names
  15. - \subpage Opening_an_Existing_Database_File_for_Reading
  16. - \subpage Opening_an_Existing_Database_File_for_Update
  17. - \subpage Creating_and_Opening_a_New_Database_File
  18. - \subpage Database_File_Management
  19. - \subpage Region
  20. - \subpage Database_Region
  21. - \subpage Active_Module_Region
  22. - \subpage Projection_Information
  23. - \subpage Latitude_Longitude_Databases
  24. - \subpage Coordinates
  25. - \subpage Global_Wraparound
  26. - \subpage Miscellaneous
  27. - \subpage Calculations
  28. - \subpage Raster_Area_Calculations
  29. - \subpage Polygonal_Area_Calculations
  30. - \subpage Distance_Calculations
  31. - \subpage General_Plotting_Routines
  32. - \subpage Temporary_Files
  33. - \subpage Command_Line_Parsing
  34. - \subpage Description
  35. - \subpage Structures
  36. - \subpage Option_structure
  37. - \subpage Flag_structure
  38. - \subpage Parser_Routines
  39. - \subpage Parser_Programming_Examples
  40. - \subpage Step_by_Step_Use_of_the_Parser
  41. - \subpage Full_Module_Example
  42. - \subpage Complete_Structure_Members_Table
  43. - \subpage Description_of_Complex_Structure_Members
  44. - \subpage Answer_member_of_the_Flag_and_Option_structures
  45. - \subpage Multiple_and_Answers_Members
  46. - \subpage key_desc_Member
  47. - \subpage gisprompt_Member
  48. - \subpage Common_Questions
  49. - \subpage String_Manipulation_Functions
  50. - \subpage Enhanced_UNIX_Routines
  51. - \subpage Running_in_the_Background
  52. - \subpage Partially_Interruptible_System_Call
  53. - \subpage ENDIAN_test
  54. - \subpage Miscellaneous
  55. - \subpage GIS_Library_Data_Structures
  56. - \subpage struct_Cell_head
  57. - \subpage struct_Categories
  58. - \subpage struct_Colors
  59. - \subpage struct_History
  60. - \subpage struct_Range
  61. - \subpage Loading_the_GIS_Library
  62. - \subpage TimeStamp_functions
  63. - \subpage Memory_Allocation
  64. \section gislibintro Introduction
  65. The <em>GIS Library</em> is the primary programming library provided
  66. with the GRASS system. <b>Programs must use this libary to access the
  67. GIS database.</b> It contains the routines which locate, create, open,
  68. rename, and remove GRASS database files. It contains the routines
  69. which read and write raster files. It contains routines which
  70. interface the user to the database, including prompting the user,
  71. listing available files, validating user access, etc. It also has
  72. some general purpose routines (string manipulation, user information,
  73. etc.) which are not tied directly to database processing.
  74. It is assumed that the reader has read \ref location for a general
  75. description of GRASS databases, \ref Raster_File_Processing for
  76. details about raster map layers in GRASS, and Region_and_Mask
  77. (<b>???</b>) which discusses regions and masks. The routines in the
  78. <em>GIS Library</em> are presented in functional groupings, rather
  79. than in alphabetical order. The order of presentation will, it is
  80. hoped, provide a better understanding of how the library is to be
  81. used, as well as show the interrelationships among the various
  82. routines. Note that a good way to understand how to use these routines
  83. is to look at the source code for GRASS modules which use them. Most
  84. routines in this library require that the header file <b>grass/gis.h</b> be
  85. included in any code using these routines. Therefore, programmers
  86. should always include this file when writing code using routines from
  87. this library:
  88. \code
  89. #include <grass/gis.h>
  90. \endcode
  91. <b>Note:</b> All routines and global variables in this library,
  92. documented or undocumented, start with the prefix <b>G_</b>. To avoid
  93. name conflicts, programmers should not create variables or routines in
  94. their own modules which use this prefix.
  95. \subsection init Library Initialization
  96. It is <B>mandatory</B> that the system be initialized before any other
  97. library routines are called.
  98. G_gisinit() initialize GIS library.
  99. This routine reads the user's GRASS environment file into memory and
  100. makes sure that the user has selected a valid database and mapset. It
  101. also initializes hidden variables used by other routines. If the
  102. user's database information is invalid, an error message is printed
  103. and the module exits. The <b>program_name</b> is stored for later
  104. recall by G_program_name(). It is recommended that argv[0] be
  105. used for the program_name:
  106. \code
  107. int main (int argc, char **argv)
  108. {
  109. G_gisinit(argv[0]);
  110. }
  111. \endcode
  112. \subsection diag Diagnostic Messages
  113. The following routines are used by other routines in the library to
  114. report warning and error messages. They may also be used directly by
  115. GRASS programs.
  116. - G_fatal_error() print error message and exit
  117. - G_debug() print debug message
  118. - G_warning() print warning message and continue
  119. These routines report errors to the user. The normal mode is to write
  120. the <em>message</em> to the screen (on the standard error
  121. output). G_warning() will return and G_fatal_error() will exit.
  122. If the standard error output is not a tty device, then the message is
  123. mailed to the user instead.
  124. If the file GIS_ERROR_LOG exists (with write permission), in either
  125. the user's home directory or in the <tt>$GISBASE</tt> directory, the
  126. messages will also be logged to this file.
  127. While most applications will find the normal error reporting quite
  128. adequate, there will be times when different handling is needed. For
  129. example, graphics modules may want the messages displayed graphically
  130. instead of on the standard error output. If the programmer wants to
  131. handle the error messages differently, the following routines can be
  132. used to modify the error handling:
  133. - G_set_error_routine() change error handling
  134. This routine provides a different error handler for G_fatal_error()
  135. and G_warning(). The <m>handler</em> routine must be defined as
  136. follows:
  137. \code
  138. int handler(char *message, int fatal)
  139. \endcode
  140. where <i>message</i> is the message to be handled and <i>fatal</i>
  141. indicates the type of error:
  142. - 1 (fatal error)
  143. - or 0 (warning).
  144. <b>Note:</b> The handler only provides a way to send the message
  145. somewhere other than to the error output. If the error is fatal, the
  146. module will exit after the handler returns.
  147. - G_unset_error_routine() reset normal error handling
  148. This routine resets the error handling for G_fatal_error() and
  149. G_warning() back to the default action.
  150. - G_sleep_on_error() sleep on error
  151. - G_suppress_warnings() suppress warnings
  152. \subsection envir Environment and Database Information
  153. The following routines return information about the current database
  154. selected by the user. Some of this information is retrieved from the
  155. user's GRASS environment file. Some of it comes from files in the
  156. database itself. See \ref Environment_Variables for a discussion of
  157. the GRASS environment.
  158. The following four routines can be used freely by the programmer:
  159. - G_location() current location name
  160. Returns the name of the current database location. This routine should
  161. be used by modules that need to display the current location to the
  162. user. See \ref Locations for an explanation of locations.
  163. - G_mapset() current mapset name
  164. Returns the name of the current mapset in the current location. This
  165. routine is often used when accessing files in the current mapset. See
  166. \ref Mapsets for an explanation of mapsets.
  167. - G_myname() location title
  168. Returns a one line title for the database location. This title is read
  169. from the file MYNAME in the PERMANENT mapset. See also \ref
  170. Permanent_Mapset for a discussion of the PERMANENT mapset.
  171. - G_gisbase() top level module directory
  172. Returns the full path name of the top level directory for GRASS
  173. programs. This directory will have subdirectories which will contain
  174. modules and files required for the running of the system. Some of
  175. these directories are:
  176. \verbatim
  177. bin commands run by the user
  178. etc modules and data files used by GRASS commands
  179. html help files
  180. \endverbatim
  181. The use of G_gisbase() to find these subdirectories enables GRASS modules
  182. to be written independently of where the GRASS system is actually installed
  183. on the machine. For example, to run the module <i>sroff</i> in the GRASS
  184. <tt>etc</tt> directory:
  185. \code
  186. char command[200];
  187. sprintf(command, "%s/etc/sroff", G_gisbase());
  188. G_spawn(command, "sroff", NULL);
  189. \endcode
  190. The following two routines return full path UNIX directory names. They
  191. should be used only in special cases. They are used by other routines
  192. in the library to build full UNIX file names for database
  193. files. <B>The programmer should not use the next two routines to
  194. bypass the normal database access routines.</B>
  195. - G_gisdbase() top level database directory
  196. Returns the full UNIX path name of the directory which holds the
  197. database locations. See \ref GISDBASE for a full explanation of this
  198. directory.
  199. - G_location_path() current location directory
  200. Returns the full UNIX path name of the current database location. For
  201. example, if the user is working in location <I>spearfish</I> in the
  202. <tt>/home/user/grassdata</tt> database directory, this routine will
  203. return a string which looks like
  204. <tt>/home/user/grassdata/spearfish</tt>.
  205. These next routines provide the low-level management of the
  206. information in the user's GRASS environment file. <B>They should not
  207. be used in place of the higher level interface routines described
  208. above.</B>
  209. - G_getenv()
  210. - G__getenv()
  211. These routines look up the variable <em>name</em> in the GRASS
  212. environment and return its value (which is a character string). If
  213. <em>name</em> is not set, G_getenv() issues an error message and calls
  214. exit(). G__setenv() just returns the NULL pointer.
  215. - G_setenv ()
  216. - G__setenv()
  217. These routines set the the GRASS environment variable <em>name</em> to
  218. <em>value</em>. If <em>value</em> is NULL, the <em>name</em> is unset.
  219. Both routines set the value in module memory, but only G_setenv()
  220. writes the new value to the user's GRASS environment file.
  221. \section dbaseaccess Fundamental Database Access Routines
  222. The routines described in this section provide the low-level interface
  223. to the GRASS database. They search the database for files, prompt the
  224. user for file names, open files for reading or writing, etc. The
  225. programmer should never bypass this level of database interface. These
  226. routines must be used to access the GRASS database unless there <b>are
  227. other higher level library routines which perform the same
  228. function</b>. For example, routines to process raster files (see \ref
  229. Raster_File_Processing), vector files (see \ref
  230. Vector_File_Processing), etc., should be used instead.
  231. In the descriptions below, the term database <i>element</i> is used.
  232. Elements are subdirectories within a mapset and are associated with a
  233. specific GRASS data type. For example, raster files live in the "cell"
  234. and "fcell" element. See \ref Elements for more details.
  235. \subsection Fully_Qualified_File_Names Fully Qualified File Names
  236. All GRASS routines which access database files must be given both the
  237. file name and the mapset where the file resides. Often the name and
  238. the mapset are 2 distinct character strings. However, there is a need
  239. for a single character string which contains both the name and the
  240. mapset (e.g., for interactive interfacing to command-line
  241. programs). This form of the name is known as the <em>fully qualified
  242. file name</em> and is built by the following routine:
  243. - G_fully_qualified_name()
  244. Returns a fully qualified name for the file <i>name</i> in
  245. <i>mapset</i> Currently this string is in the form <i>name@mapset</i>,
  246. but the programmer should pretend not to know this and always call
  247. this routine to get the fully qualified name.
  248. The following example shows how an interactive version of <tt>d.rast</tt>
  249. interfaces with the command-line version of <tt>d.rast</tt>:
  250. \code
  251. #include <grass/gis.h>
  252. int main(char *argc, char **argv)
  253. {
  254. char name[GNAME_MAX], *mapset, *fqn;
  255. char command[1024];
  256. G_gisinit(argv[0]);
  257. mapset = G_ask_cell_old("", name, "");
  258. if (mapset == NULL)
  259. exit(EXIT_SUCCESS);
  260. fqn = G_fully_qualified_name(name, mapset);
  261. sprintf(command, "d.rast map='%s'", fqn);
  262. G_spawn(command, "d.rast", NULL);
  263. }
  264. \endcode
  265. \subsection finding Finding Files in the Database
  266. Command line driven module requires a database file name as one of the
  267. command arguments. In this case, the programmer must search the
  268. database to find the mapset where the file resides.
  269. The following routines search the database for files:
  270. - G_find_file()
  271. Look for the file <i>name</i> under the specified <i>element</i> in
  272. the database. The <i>mapset</i> parameter can either be the empty
  273. string "", which means search all the mapsets in the user's current
  274. mapset search path, or it can be a specific mapset, which means. Look
  275. for the file only in this one mapset (for example, in the current
  276. mapset).
  277. If found, the mapset where the file lives is returned. If not found,
  278. the NULL pointer is returned.
  279. If the user specifies a fully qualified file name, (i.e, a name that
  280. also contains the mapset; see \ref Fully_Qualified_File_Names) then
  281. G_find_file() modifies <em>name</em> by eliminating the mapset from
  282. the <em>name</em>.
  283. For example, to find a "paint/labels" file anywhere in the database:
  284. \code
  285. char name[GNAME_MAX];
  286. char *mapset;
  287. if ((mapset = G_find_file("paint/labels", name, "")) == NULL)
  288. /* not found */
  289. \endcode
  290. To check that the file exists in the current mapset:
  291. \code
  292. char name[GNAME_MAX];
  293. if (G_find_file("paint/labels", name, G_mapset()) == NULL)
  294. /* not found */
  295. \endcode
  296. \subsection Legal_File_Names Legal File Names
  297. Not all names that a user may enter will be legal files for the GRASS
  298. databases. The routines which create new files require that the new
  299. file have a legal name. If the name is obtained from the command line,
  300. the programmer must check that the name is legal. The following
  301. routine checks for legal file names:
  302. - G_legal_filename()
  303. \subsection Opening_an_Existing_Database_File_for_Reading Opening an Existing Database File for Reading
  304. The following routines open the file <i>name</i> in <i>mapset</i> from
  305. the specified database <i>element</i> for <b>reading</b> (but not for
  306. writing). The file <i>name</i> and <i>mapset</i> can be obtained using
  307. G_find_file().
  308. The database file <i>name</i> under the <i>element</i> in the
  309. specified <i>mapset</i> is opened for reading (but not for writing).
  310. - G_open_old()
  311. The UNIX open() routine is used to open the file. If the file does not
  312. exist, -1 is returned. Otherwise the file descriptor from the open()
  313. is returned.
  314. - G_fopen_old()
  315. The UNIX fopen() routine, with "r" read mode, is used to open the
  316. file. If the file does not exist, the NULL pointer is
  317. returned. Otherwise the file descriptor from the fopen() is returned.
  318. \subsection Opening_an_Existing_Database_File_for_Update Opening an Existing Database File for Update
  319. The following routines open the file <i>name</i> in the current mapset
  320. from the specified database <i>element</i> for <b>writing</b>. The
  321. file must exist. Its <i>name</i> can be obtained using G_find_file().
  322. The database file <i>name</i> under the <i>element</i> in the current
  323. mapset is opened for reading and writing.
  324. - G_open_update()
  325. The UNIX open() routine is used to open the file. If the file does not
  326. exist, -1 is returned. Otherwise the file is positioned at the end of
  327. the file and the file descriptor from the open() is returned.
  328. - G_fopen_append()
  329. The UNIX fopen() routine, with "a" append mode, is used to open the
  330. file. If the file does not exist, the NULL pointer is
  331. returned. Otherwise the file is positioned at the end of the file and
  332. the file descriptor from the fopen() is returned.
  333. \subsection Creating_and_Opening_a_New_Database_File Creating and Opening a New Database File
  334. The following routines create the new file <i>name</i> in the current
  335. mapset (GRASS does not allow files to be created outside the current
  336. mapset; see \ref Database_Access_Rules) under the specified database
  337. <i>element</i> and open it for writing. The database <i>element</i> is
  338. created, if it does not already exist.
  339. The file <i>name</i> is obtained noninteractively (e.g., from the
  340. command line), G_legal_filename() should be called first to make sure
  341. that <i>name</i> is a valid GRASS file name. <b>Warning:</b> It is not
  342. an error for <i>name</i> to already exist. However, the file will be
  343. removed and recreated empty. G_find_file() could be used to see if
  344. <i>name</i> exists.
  345. The database file <i>name</i> under the <i>element</i> in the current
  346. mapset is created and opened for writing (but not reading).
  347. - G_open_new()
  348. The UNIX open() routine is used to open the file. If the file does not
  349. exist, -1 is returned. Otherwise the file is positioned at the end of
  350. the file and the file descriptor from the open() is returned.
  351. - G_fopen_new()
  352. The UNIX fopen() routine, with "w" write mode, is used to open the
  353. file. If the file does not exist, the NULL pointer is
  354. returned. Otherwise the file is positioned at the end of the file and
  355. the file descriptor from the fopen() is returned.
  356. \subsection Database_File_Management Database File Management
  357. The following routines allow the renaming and removal of database
  358. files in the current mapset (These functions only apply to the current
  359. mapset since GRASS does permit users to modify things in mapsets other
  360. than the current mapset; see \ref Database_Access_Rules).
  361. - G_rename() rename a database file
  362. - G_remove() remove a database file
  363. <b>Note:</b> These functions only apply to the specific <i>element</i>
  364. and not to other "related" elements. For example, if <i>element</i> is
  365. "cell", then the specified raster file will be removed (or renamed),
  366. but the other support files, such as "cellhd" or "cats", will not. To
  367. remove these other files as well, specific calls must be made for each
  368. related <i>element</i>.
  369. \section Region Region
  370. The region concept is explained in \ref Region. It can be thought of as a
  371. two-dimensional matrix with known boundaries and rectangular cells.
  372. There are logically two different regions. The first is the database
  373. region that the user has set in the current mapset. The other is the
  374. region that is active in the module. This active module region is what
  375. controls reading and writing of raster file data. The vector map
  376. export does not take care for the active region settings.
  377. The routines described below use a GRASS data structure
  378. <tt>Cell_head</tt> to hold region information. This structure is
  379. defined in the "gis.h" header file. It is discussed in detail under
  380. \ref GIS_Library_Data_Structures.
  381. \subsection Database_Region Database Region
  382. Reading and writing the user's database region are done by the
  383. following routines:
  384. Note: Previous versions of GRASS called this the "window". Due to
  385. overuse of this term (database window, graphics window, etc.), the
  386. term was changed to "region". However, to maintain compatibility with
  387. existing programs, library routine names were not changed - hence the
  388. term "window" is used in the routine name (where "region" should
  389. probably be used instead).
  390. - G_get_window()
  391. Reads the database region as stored in the WIND file in the user's
  392. current mapset into region.
  393. An error message is printed and exit() is called if there is a problem
  394. reading the region.
  395. <b>Note:</b> GRASS applications that read or write raster files should
  396. not use this routine since its use implies that the active module
  397. region will not be used. Programs that read or write raster file data
  398. (or vector data) can query the active module region using
  399. G_window_rows() and G_window_cols().
  400. - G_put_window()
  401. Writes the database region file (WIND) in the user's current mapset
  402. from region.
  403. <b>Warning:</b> Since this routine actually changes the database
  404. region, it should only be called by modules which the user knows will
  405. change the region. It is probably fair to say that only the
  406. <tt>g.region</tt> should call this routine.
  407. There is another database region. This region is the default region
  408. for the location. The default region provides the user with a
  409. "starting" region, i.e., a region to begin with and return to as a
  410. reference point. The GRASS modules <tt>g.region</tt> allow the user to
  411. set their database region from the default region (see \ref
  412. Permanent_Mapset for a discussion of the default region). The
  413. following routine reads this region:
  414. - G_get_default_window()
  415. \subsection Active_Module_Region Active Module Region
  416. The <em>active module region</em> is the one that is used when reading
  417. and writing raster file data. This region determines the resampling
  418. when reading raster data. It also determines the extent and resolution
  419. of new raster files.
  420. Initially the active module region and the user's database region are
  421. the same, but the programmer can make them different. The following
  422. routines manage the active module region.
  423. - G_window_rows() number of rows in active region
  424. - G_window_cols() number of columns in active region
  425. These routines return the number of rows and columns (respectively) in
  426. the active module region. Before raster files can be read or written,
  427. it is necessary to known how many rows and columns are in the active
  428. region. For example:
  429. \code
  430. int nrows, cols;
  431. int row, col;
  432. nrows = G_window_rows();
  433. ncols = G_window_cols();
  434. for (row = 0; row < nrows; row++) {
  435. /* read row ... */
  436. for (col = 0; col < ncols; col++) {
  437. /* process col ... */
  438. }
  439. }
  440. \endcode
  441. - G_set_window() set the active region
  442. This routine sets the active region from given region. Setting the
  443. active region does not change the WIND file in the database. It simply
  444. changes the region for the duration of the module.
  445. However, the new region setting is not retained across the UNIX exec()
  446. call. This implies that G_set_window() cannot be used to set the
  447. region for a module to be executed using the system() or popen()
  448. routines.
  449. <b>Note:</b> This routine overrides the region as set by the user. Its
  450. use should be very limited since it changes what the user normally
  451. expects to happen. If this routine is not called, then the active
  452. region will be the same as what is in the user's WIND file.
  453. <b>Warning:</b> Calling this routine with already opened raster files
  454. has some side effects. If there are raster files which are open for
  455. reading, they will be read into the newly set region, not the region
  456. that was active when they were opened. However, CELL buffers allocated
  457. for reading the raster files are not automatically reallocated. The
  458. module must reallocate them explicitly. Also, this routine does not
  459. change the region for raster files which are open for writing. The
  460. region that was active when the open occurred still applies to these
  461. files.
  462. - G_get_set_window() get the active region
  463. Gets the values of the currently active region into region. If
  464. G_set_window() has been called, then the values set by that call are
  465. retrieved. Otherwise the user's database region is retrieved.
  466. <b>Note:</b> For modules that read or write raster data, and really
  467. need the full region information, this routine is preferred over
  468. G_get_window(). However, since G_window_rows() and G_window_cols()
  469. return the number of rows and columns in the active region, the
  470. programmer should consider whether or not the full region information
  471. is really needed before using this routine.
  472. - G_align_window() align two regions
  473. Modifies the input region to align to the reference region. The
  474. resolutions in region are set to match those in refefence region and
  475. the region edges (north, south, east, west) are modified to align with
  476. the grid of the reference region.
  477. The <i>region</i> may be enlarged if necessary to achieve the
  478. alignment. The north is rounded northward, the south southward, the
  479. east eastward and the west westward.
  480. - G_col_to_easting()
  481. Converts a column relative to a region to an easting.
  482. <b>Note:</b> col is a double: col+0.5 will return the easting for the
  483. center of the column; col+0.0 will return the easting for the western
  484. edge of the column; and col+1.0 will return the easting for the
  485. eastern edge of the column.
  486. - G_row_to_northing()
  487. Converts a row relative to a region to a northing.
  488. <b>Note:</b> row is a double: row+0.5 will return the northing for the
  489. center of the row; row+0.0 will return the northing for the northern
  490. edge of the row; and row+1.0 will return the northing for the southern
  491. edge of the row.
  492. - G_easting_to_col()
  493. Converts an easting relative to a region to a column.
  494. <b>Note:</b> The result is a double. Casting it to an integer will
  495. give the column number.
  496. - G_northing_to_row()
  497. Converts a northing relative to a region to a row.
  498. <b>Note:</b> the result is a double. Casting it to an integer will
  499. give the row number.
  500. \section Projection_Information Projection Information
  501. The following routines return information about the cartographic
  502. projection and zone. See \ref Region for more information about these
  503. values.
  504. - G_projection()
  505. This routine returns a code indicating the projection for the active
  506. region. The current values are:
  507. - PROJECTION_XY - unreferenced x,y (imagery data)
  508. - PROJECTION_UTM - UTM
  509. - PROJECTION_SP - State Plane
  510. - PROJECTION_LL - Latitude-Longitude
  511. - PROJECTION_OTHER - Other (more than 121 projections are supported)
  512. - G_database_projection_name()
  513. Returns a pointer to a string which is a printable name for projection
  514. code (as returned by G_projection()).
  515. - G_database_unit_name()
  516. Returns a string describing the database grid units.
  517. - G_database_units_to_meters_factor()
  518. Returns a factor which converts the grid unit to meters (by
  519. multiplication). If the database is not metric (eg. imagery) then 0.0
  520. is returned.
  521. - G_zone()
  522. This routine returns the zone for the active region. The meaning for
  523. the zone depends on the projection. For example zone 18 for projection
  524. type 1 would be UTM zone 18.
  525. \subsection Latitude_Longitude_Databases Latitude-Longitude GIS Databases
  526. GRASS supports databases in a longitude-latitude grid using a
  527. projection where the x coordinate is the longitude and the y
  528. coordinate is the latitude. This projection is called the Equidistant
  529. Cylindrical Projection (also known as Plate Carree). ECP has the
  530. property that <em>where am I</em> and <em>row-column</em> calculations
  531. are identical to those in planimetric grids (like UTM, Universal
  532. Transverse Mercator Projection). This implies that normal GRASS
  533. registration and overlay functions will work without any special
  534. considerations or modifications to existing code. However, the
  535. projection is not planimetric. This means that distance and area
  536. calculations are no longed Euclidean.
  537. Also, since the world is round, maps may not have edges in the
  538. east-west direction, especially for global databases. Maps may have
  539. the same longitude at both the east and west edges of the
  540. display. This feature, called global wraparound, must be accounted for
  541. by GRASS modules (particularly vector based functions, like plotting).
  542. What follows is a description of the GIS Library routines that are
  543. available to support latitude-longitude databases.
  544. \subsection Coordinates Coordinates
  545. Latitudes and longitudes are specified in degrees. Northern latitudes
  546. range from 0 to 90 degrees, and southern latitudes from 0 to
  547. -90. Longitudes have no limits since longitudes ±360 degrees are
  548. equivalent.
  549. Coordinates are represented in ASCII using the format
  550. <tt>dd:mm:ssN</tt> or <tt>dd:mm:ssS</tt> for latitudes,
  551. <tt>ddd:mm:ssE</tt> or <tt>ddd.mm.ssW</tt> for longitudes, and
  552. <tt>dd.mm.ss</tt> for grid resolution. For example, 80:30:24N
  553. represents a northern latitude of 80 degrees, 30 minutes, and 24
  554. seconds. 120:15W represents a longitude 120 degrees and 15 minutes
  555. west of the prime meridian. 30:15 represents a resolution of 30
  556. degrees and 15 minutes. These next routines convert between ASCII
  557. representations and the machine representation for a coordinate. They
  558. work both with latitude-longitude projections and planimetric
  559. projections.
  560. <b>Note:</b> In each subroutine, the programmer must specify the
  561. projection number. If the projection number is PROJECTION_LL (defined
  562. in "gis.h"), then latitude-longitude ASCII format is invoked.
  563. Otherwise, a standard floating-point to ASCII conversion is made.
  564. - G_format_easting() easting to ASCII
  565. - G_format_northing() northing to ASCII
  566. Converts the double representation of the given coordinate to its
  567. ASCII representation.
  568. - G_format_resolution()
  569. Converts the double representation of the resolution to its
  570. ASCII representation.
  571. - G_scan_easting() ASCII easting to double
  572. - G_scan_northing() ASCII northing to double
  573. Converts the ASCII coordinate string in to its double representation.
  574. - G_scan_resolution() ASCII resolution to double
  575. Converts the ASCII "resolution" string to its double representation
  576. (into resolution).
  577. The following are examples of how these routines are used.
  578. \code
  579. double north;
  580. char buf[50];
  581. G_scan_northing(buf, north, G_projection()); /* ASCII to double */
  582. G_format_northing(north, buf, G_projection()); /* double to ASCII */
  583. G_format_northing(north, buf, -1); /* double to ASCII */
  584. /* This last example forces floating-point ASCII format */
  585. \endcode
  586. \subsection Global_Wraparound Global Wraparound
  587. These next routines provide a mechanism for determining the relative
  588. position of a pair of longitudes. Since longitudes of ±360 are
  589. equivalent, but GRASS requires the east to be bigger than the west,
  590. some adjustment of coordinates is necessary.
  591. - G_adjust_easting()
  592. Returns east larger than west. If the region projection is
  593. PROJECTION_LL, then this routine returns an equivalent <i>east</i>
  594. that is larger, but no more than 360 degrees larger, than the
  595. coordinate for the western edge of the region. Otherwise no adjustment
  596. is made and the original <i>east</i> is returned.
  597. - G_adjust_east_longitude()
  598. This routine returns an equivalent <i>east</i> that is larger, but no
  599. more than 360 larger than the <i>west</i> coordinate.
  600. This routine should be used only with latitude-longitude coordinates.
  601. - G_shortest_way()
  602. Returns shortest way between eastings.
  603. \subsection Miscellaneous Miscellaneous
  604. - G_ellipsoid_name()
  605. This routine returns a pointer to a string containing the name for the
  606. ellipsoid in the GRASS ellipsoid table. It can be used as follows:
  607. \code
  608. int n;
  609. char *name;
  610. for(n = 0; name = G_ellipsoid_name(n); n++)
  611. fprintf(stdout, "%s\n", name);
  612. \endcode
  613. - G_get_ellipsoid_by_name()
  614. This routine returns the semi-major axis (in meters) and
  615. eccentricity squared for the named ellipsoid.
  616. - G_get_ellipsoid_parameters()
  617. This routine returns the semi-major axis (in meters) and the
  618. eccentricity squared for the ellipsoid associated with the
  619. database. If there is no ellipsoid explicitly associated with the
  620. database, it returns the values for the WGS 84 ellipsoid.
  621. - G_meridional_radius_of_curvature()
  622. Returns the meridional radius of curvature at a given longitude:
  623. \f$
  624. \rho = \frac{a (1-e^2)}{(1-e^2\sin^2 lon)^{3/2}}
  625. \f$
  626. - G_transverse_radius_of_curvature()
  627. Returns the transverse radius of curvature at a given longitude:
  628. \f$
  629. \nu = \frac{a}{(1-e^2\sin^2 lon)^{1/2}}
  630. \f$
  631. - G_radius_of_conformal_tangent_sphere()
  632. Returns the radius of the conformal sphere tangent to ellipsoid at a
  633. given longitude:
  634. \f$
  635. r = \frac{a (1-e^2)^{1/2}}{(1-e^2\sin^2 lon)}
  636. \f$
  637. - G_pole_in_polygon()
  638. For latitude-longitude coordinates, this routine determines if the
  639. polygon contains one of the poles.
  640. \section Calculations Calculations
  641. \subsection Raster_Area_Calculations Raster Area Calculations
  642. The following routines perform area calculations for raster maps.
  643. They are based on the fact that while the latitude-longitude grid is
  644. not planimetric, the size of the grid cell at a given latitude is
  645. constant. The first routines work in any projection.
  646. - G_begin_cell_area_calculations()
  647. This routine must be called once before any call to
  648. G_area_of_cell_at_row(). It can be used in either planimetric
  649. projections or the latitude-longitude projection.
  650. - G_area_of_cell_at_row()
  651. This routine returns the area in square meters of a cell in the
  652. specified row. This value is constant for planimetric grids and varies
  653. with the row if the projection is latitude-longitude.
  654. - G_begin_zone_area_on_ellipsoid()
  655. Initializes raster area calculations for an ellipsoid.
  656. - G_area_for_zone_on_ellipsoid()
  657. Returns the area between latitudes scaled by the factor passed to
  658. G_begin_zone_area_on_ellipsoid().
  659. - G_begin_zone_area_on_sphere()
  660. Initializes raster area calculations for a sphere.
  661. - G_area_for_zone_on_sphere()
  662. Returns the area between latitudes.
  663. \subsection Polygonal_Area_Calculations Polygonal Area Calculations
  664. These next routines provide area calculations for polygons. Some of
  665. the routines are specifically for latitude-longitude, while others
  666. will function for all projections.
  667. However, there is an issue for latitude-longitude that does not occur
  668. with planimetric grids. Vector/polygon data is described as a series
  669. of x,y coordinates. The lines connecting the points are not stored but
  670. are inferred. This is a simple, straight-forward process for
  671. planimetric grids, but it is not simple for latitude-longitude. What
  672. is the shape of the line that connects two points on the surface of a
  673. globe?
  674. One choice (among many) is the shortest path from <tt>x1,y1</tt> to
  675. <tt>x2,y2</tt>, known as the geodesic. Another is a straight line on
  676. the grid. The area routines described below assume the
  677. latter. Routines to work with the former have not yet been developed.
  678. - G_begin_polygon_area_calculations()
  679. This initializes the polygon area calculation routines. It is used
  680. both for planimetric and latitude-longitude projections.
  681. - G_area_of_polygon()
  682. Returns the area in square meters of the polygon. It is used both for
  683. planimetric and latitude-longitude projections.
  684. <B>Note.</B> If the database is planimetric with the non-meter grid,
  685. this routine performs the required unit conversion to produce square
  686. meters.
  687. - G_planimetric_polygon_area()
  688. Return the area in map units of the polygon,
  689. - G_begin_ellipsoid_polygon_area()
  690. This initializes the polygon area calculations for the ellipsoid.
  691. - G_ellipsoid_polygon_area()
  692. Returns the area in square meters of the polygon for latitude-longitude
  693. grids.
  694. <b>Note:</b> This routine assumes grid lines on the connecting the
  695. vertices (as opposed to geodesics).
  696. \subsection Distance_Calculations Distance Calculations
  697. Two routines perform distance calculations for any projection.
  698. - G_begin_distance_calculations()
  699. Initializes the distance calculations. It is used both for the
  700. planimetric and latitude-longitude projections.
  701. - G_distance()
  702. This routine computes the distance between two points in meters.
  703. - G_begin_geodesic_distance()
  704. Initializes the distance calculations for the ellipsoid. It is used
  705. only for the latitude-longitude projection.
  706. - G_geodesic_distance() geodesic distance
  707. Calculates the geodesic distance between two points in meters.
  708. The calculation of the geodesic distance is fairly costly. These next
  709. three routines provide a mechanism for calculating distance with two
  710. fixed latitudes and varying longitude separation.
  711. - G_set_geodesic_distance_lat1() set the first latitude.
  712. - G_set_geodesic_distance_lat2() set the second latitude.
  713. - G_geodesic_distance_lon_to_lon()
  714. Calculates the geodesic distance between two points set by
  715. G_set_geodesic_distance_latl() and G_set_geodesic_distance_lat2().
  716. \section General_Plotting_Routines General Plotting Routines
  717. The following routines form the foundation of a general purpose line
  718. and polygon plotting capability.
  719. - G_bresenham_line()
  720. Draws a line from one point to another using Bresenham's
  721. algorithm. A routine to plot points must be provided, as is defined
  722. as: <tt>point(x, y)</tt> plot a point at x,y.
  723. This routine does not require a previous call to G_setup_plot() to
  724. function correctly, and is independent of all following routines.
  725. - G_setup_plot()
  726. Initializes the plotting capability. This routine must be called once
  727. before calling the G_plot_*() routines described below.
  728. - G_plot_line()
  729. Plots line between latlon coordinates. This routine handles global
  730. wrap-around for latitude-longitude databases. See G_setup_plot() for
  731. the required coordinate initialization procedure.
  732. - G_plot_polygon()
  733. Plots filled polygon with n vertices. See G_setup_plot() for the
  734. required coordinate initialization procedure.
  735. - G_plot_area()
  736. Plots multiple polygons. Like G_plot_polygon(), except it takes a set
  737. of polygons, each with n vertices, where the number of polygons is
  738. specified with the <i>rings</i> argument. It is especially useful for
  739. plotting vector areas with interior islands.
  740. - G_plot_where_en()
  741. The pixel coordinates <i>x,y</i> are converted to map coordinates
  742. east,north</i>. See G_setup_plot() for the required coordinate
  743. initialization procedure.
  744. - G_plot_where_xy()
  745. The map coordinates <i>east,north</i> are converted to pixel
  746. coordinates <i>x,y.</i>. See G_setup_plot() for the required
  747. coordinate initialization procedure.
  748. - G_plot_fx()
  749. \section Temporary_Files Temporary Files
  750. Often it is necessary for modules to use temporary files to store
  751. information that is only useful during the module run. After the
  752. module finishes, the information in the temporary file is no longer
  753. needed and the file is removed. Commonly it is required that
  754. temporary file names be unique from invocation to invocation of the
  755. module. It would not be good for a fixed name like "/tmp/mytempfile"
  756. to be used. If the module were run by two users at the same time, they
  757. would use the same temporary file. In addition systematic use of the
  758. /tmp directory could leave the system vulnerable to symlink attacks.
  759. The following routine generates temporary file names which are unique
  760. within the module and across all GRASS programs.
  761. - G_tempfile()
  762. This routine returns a pointer to a string containing a unique file
  763. name that can be used as a temporary file within the
  764. module. Successive calls to G_tempfile() will generate new names.
  765. Only the file name is generated. The file itself is not created. To
  766. create the file, the module must use standard UNIX functions which
  767. create and open files, e.g., creat() or fopen().
  768. The programmer should take reasonable care to remove (unlink) the file
  769. before the module exits. However, GRASS database management will
  770. eventually remove all temporary files created by G_tempfile() that
  771. have been left behind by the modules which created them.
  772. <b>Note:</b> The temporary files are created in the GRASS database
  773. rather than under <tt>/tmp</tt>. This is done for two reasons. The
  774. first is to increase the likelihood that enough disk is available for
  775. large temporary files since /tmp may be a very small file system. The
  776. second is so that abandoned temporary files can be automatically
  777. removed (but see the warning below).
  778. <b>Warning:</b> The temporary files are named, in part, using the
  779. process id of the module. GRASS database management will remove these
  780. files only if the module which created them is no longer
  781. running. However, this feature has a subtle trap. Programs which
  782. create child processes (using the UNIX fork(), see also G_fork()
  783. routine) should let the child call G_tempfile(). If the parent does it
  784. and then exits, the child may find that GRASS has removed the
  785. temporary file since the process which created it is no longer
  786. running.
  787. \section Command_Line_Parsing Command Line Parsing
  788. The following routines provide a standard mechanism for command line
  789. parsing. Use of the provided set of routines will standardize GRASS
  790. commands that expect command line arguments, creating a family of
  791. GRASS modules that is easy for users to learn. As soon as a GRASS user
  792. familiarizes himself with the general form of command line input as
  793. defined by the parser, it will greatly simplify the necessity of
  794. remembering or at least guessing the required command line arguments
  795. for any GRASS command. It is strongly recommended that GRASS
  796. programmers use this set of routines for all command line
  797. parsing. With their use, the programmer is freed from the burden of
  798. generating user interface code for every command. The parser will
  799. limit the programmer to a pre-defined look and feel, but limiting the
  800. interface is well worth the shortened user learning curve.
  801. \subsection Description Description
  802. The GRASS parser is a collection of five subroutines which use two
  803. structures that are defined in the GRASS "gis.h" header file. These
  804. structures allow the programmer to define the options and flags that
  805. make up the valid command line input of a GRASS command.
  806. The parser routines behave in one of three ways:
  807. # If no command line arguments are entered by the user, the parser
  808. searches for a completely interactive version of the command. If the
  809. interactive version is found, control is passed over to this
  810. version.
  811. # If command line arguments are entered but they are a subset of the
  812. options and flags that the programmer has defined as required
  813. arguments, three things happen. The parser will pass an error message
  814. to the user indicating which required options and/or flags were
  815. missing from the command line, the parser will then display a complete
  816. usage message for that command, and finally the parser cancels
  817. execution of the command.
  818. # If all necessary options and flags are entered on the command line
  819. by the user, the parser executes the command with the given options
  820. and flags.
  821. \subsection Structures Structures
  822. The parser routines described below use two structures as defined in
  823. the GRASS "gis.h" header file.
  824. This is a basic list of members of the <b>Option</b> and <b>Flag</b>
  825. structures. A comprehensive description of all elements of these two
  826. structures and their possible values can be found in
  827. \ref Full_Structure_Members_Description.
  828. \subsection Option_structure Option structure
  829. These are the basic members of the <em>Option</em> structure.
  830. \code
  831. struct Option *opt; /* to declare a command line option */
  832. \endcode
  833. Structure Member Description of Member:
  834. - <i>opt->key</i> - Option name that user will use
  835. - <i>opt->description</i> - Option description that is shown to the user
  836. - <i>opt->type</i> - Variable type of the user's answer to the option
  837. - <i>opt->required</i> - Is this option required on the command line? (Boolean)
  838. \subsection Flag_structure Flag structure
  839. These are the basic members of the Flag structure.
  840. \code
  841. struct Flag *flag; /* to declare a command line flag */
  842. \endcode
  843. Structure Member Description of Member:
  844. - <i>flag->key</i> - Single letter used for flag name
  845. - <i>flag->description</i> - Flag description that is shown to the user
  846. \subsection Parser_Routines Parser Routines
  847. Associated with the parser are five routines that are automatically
  848. included in the GRASS Makefile process. The Makefile process is
  849. documented in \ref Compiling_and_Installing_GRASS_Modules.
  850. - G_define_option()
  851. Returns <i>Option</i> structure. Allocates memory for the Option structure
  852. and returns a pointer to this memory.
  853. - G_define_flag()
  854. Allocates memory for the <i>Flag</i> structure and returns a pointer
  855. to this memory.
  856. - G_parser()
  857. The command line parameters <i>argv</i> and the number of parameters
  858. <i>argc</i> from the main() routine are passed directly to
  859. G_parser(). G_parser() accepts the command line input
  860. entered by the user, and parses this input according to the input
  861. options and/or flags that were defined by the programmer.
  862. G_parser() returns 0 if successful. If not successful, a usage
  863. statement is displayed that describes the expected and/or required
  864. options and flags and a non-zero value is returned.
  865. - G_usage()
  866. Calls to G_usage() allow the programmer to print the usage message at
  867. any time. This will explain the allowed and required command line
  868. input to the user. This description is given according to the
  869. programmer's definitions for options and flags. This function becomes
  870. useful when the user enters options and/or flags on the command line
  871. that are syntactically valid to the parser, but functionally invalid
  872. for the command (e.g. an invalid file name).
  873. For example, the parser logic doesn't directly support grouping
  874. options. If two options be specified together or not at all, the
  875. parser must be told that these options are not required and the
  876. programmer must check that if one is specified the other must be as
  877. well. If this additional check fails, then G_parser() will
  878. succeed, but the programmer can then call G_usage() to print
  879. the standard usage message and print additional information about how
  880. the two options work together.
  881. - G_disable_interactive()
  882. When a user calls a command with no arguments on the command line, the
  883. parser will enter its own standardized interactive session in which
  884. all flags and options are presented to the user for input. A call to
  885. G_disable_interactive() disables the parser's interactive prompting.
  886. <b>Note:</b> Displaying multiple answers default values (new in GRASS
  887. 5, see d.zoom for example).
  888. \code
  889. char *def[] = {"One", "Two", "Last", NULL};
  890. opt->multiple = YES;
  891. opt->answers = def;
  892. if (G_parser(argc, argv))
  893. exit(EXIT_FAILURE);
  894. \endcode
  895. The programmer may not forget last NULL value.
  896. \subsection Parser_Programming_Examples Parser Programming Examples
  897. The use of the parser in the programming process is demonstrated
  898. here. Both a basic step by step example and full code example are
  899. presented.
  900. \subsection Step_by_Step_Use_of_the_Parser Step by Step Use of the Parser
  901. These are the four basic steps to follow to implement the use of the
  902. GRASS parser in a GRASS command:
  903. <b>(1) Allocate memory for Flags and Options:</b>
  904. Flags and Options are pointers to structures allocated through the
  905. parser routines G_define_option() and G_define_flag() as
  906. defined in \ref Parser_Routines.
  907. \code
  908. #include <grass/gis.h>; /* The standard GRASS include file */
  909. struct Option *opt; /* Establish an Option pointer for each option */
  910. struct Flag *flag; /* Establish a Flag pointer for each option */
  911. opt = G_define_option(); /* Request a pointer to memory for each option */
  912. flag = G_define_flag(); /* Request a pointer to memory for each flag */
  913. \endcode
  914. <b>(2) Define members of Flag and Option structures:</b>
  915. The programmer should define the characteristics of each option and
  916. flag desired as outlined by the following example:
  917. \code
  918. opt->key = "option"; /* The name of this option is "option". */
  919. opt->description = _("Option test"); /* The option description is "Option test" */
  920. opt->type = TYPE_STRING; /* The data type of the answer to the option */
  921. opt->required = YES; /* This option *is* required from the user */
  922. flag->key = "t"; /* Single letter name for flag */
  923. flag->description = _("Flag test"); /* The flag description is "Flag test" */
  924. \endcode
  925. <b>Note:</b> There are more options defined later in \ref
  926. Complete_Structure_Members_Table.
  927. <b>(3) Call the parser:</b>
  928. \code
  929. int main(int argc, char *argv[]); /* command line args passed into main() */
  930. if (G_parser(argc, argv)) /* Returns 0 if successful, non-zero otherwise */
  931. exit(EXIT_FAILURE);
  932. \endcode
  933. <b>(4) Extracting information from the parser structures:</b>
  934. \code
  935. fprintf(stdout, "For the option "%s" you chose: <%s>\n", opt->description, opt->answer);
  936. fprintf(stdout, "The flag "-%s" is %s set.\n", flag->key, flag->answer ? "" : "not");
  937. \endcode
  938. <b>(5) Running the example program</b>
  939. Once such a module has been compiled (for example to the default
  940. executable file <tt>a.out</tt> , execution will result in the following
  941. user interface scenarios. Lines that begin with '$' imply user entered
  942. commands on the command line.
  943. \verbatim
  944. $ a.out help
  945. \endverbatim
  946. This is a standard user call for basic help information on the
  947. module. The command line options (in this case, "help") are sent to
  948. the parser via G_parser(). The parser recognizes the "help"
  949. command line option and returns a list of options and/or flags that
  950. are applicable for the specific command. Note how the programmer
  951. provided option and flag information is captured in the output.
  952. \verbatim
  953. a.out [-t] option=name
  954. Flags:
  955. -t Flag test
  956. Parameters:
  957. option Option test
  958. \endverbatim
  959. Now the following command is executed:
  960. \verbatim
  961. # a.out -t
  962. \endverbatim
  963. This command line does not contain the required option. Note that the
  964. output provides this information along with the standard usage message
  965. (as already shown above):
  966. \verbatim
  967. Required parameter <option> not set (Option test).
  968. Usage:
  969. a.out[-t] option=name
  970. Flags:
  971. -t Flag test
  972. Parameters:
  973. option Option test
  974. \endverbatim
  975. The following commands are correct and equivalent. The parser provides no
  976. error messages and the module executes normally:
  977. \verbatim
  978. # a.out option=Hello -t
  979. # a.out -t option=Hello
  980. For the option "Option test" you chose: Hello
  981. The flag "-t" is set.
  982. \endverbatim
  983. \subsection Full_Module_Example Full Module Example
  984. The following code demonstrates some of the basic capabilities of the
  985. parser. To compile this code, create this Makefile and run the
  986. <tt>make</tt> command (see \ref Compiling_and_Installing_GRASS_Modules).
  987. \code
  988. MODULE_TOPDIR = ../..
  989. PGM = r.mysample
  990. LIBES = $(GISLIB)
  991. DEPENDENCIES = $(GISDEP)
  992. include $(MODULE_TOPDIR)/include/Make/Module.make
  993. default: cmd
  994. \endcode
  995. The <tt>sample.c</tt> code follows. You might experiment with this code to
  996. familiarize yourself with the parser.
  997. <b>Note:</b> This example includes some of the advanced structure
  998. members described in \ref Complete_Structure_Members_Table.
  999. \code
  1000. #include <stdlib.h>
  1001. #include <string.h>
  1002. #include <grass/gis.h>
  1003. #include <grass/glocale.h>
  1004. int main(int argc, char *argv[])
  1005. {
  1006. struct Option *opt, *coor;
  1007. struct Flag *flag;
  1008. double X, Y;
  1009. int n;
  1010. opt = G_define_option();
  1011. opt->key = "debug";
  1012. opt->type = TYPE_STRING;
  1013. opt->required = NO;
  1014. opt->answer = "0";
  1015. opt->description = _("Debug level");
  1016. coor = G_define_option();
  1017. coor->key = "coordinate";
  1018. coor->key_desc = "x,y";
  1019. coor->type = TYPE_STRING;
  1020. coor->required = YES;
  1021. coor->multiple = YES;
  1022. coor->description = _("One or more coordinate(s)");
  1023. /* Note that coor->answer is not given a default value. */
  1024. flag = G_define_flag();
  1025. flag->key = 'v';
  1026. flag->description = _("Verbose execution");
  1027. /* Note that flag->answer is not given a default value. */
  1028. if (G_parser(argc, argv))
  1029. exit (EXIT_FAILURE);
  1030. G_message("For the option <%s> you chose: <%s>",
  1031. opt->description, opt->answer);
  1032. G_message("The flag <%s> is: %s set", flag->key,
  1033. flag->answer ? "" : "not");
  1034. G_message("You specified the following coordinates:");
  1035. for (n=0; coor->answers[n] != NULL; n+=2) {
  1036. G_scan_easting(coor->answers[n], &X , G_projection());
  1037. G_scan_northing(coor->answers[n+1], &Y , G_projection());
  1038. fprintf(stdout, "%.15g,%.15g", X, Y);
  1039. }
  1040. }
  1041. \endcode
  1042. \subsection Complete_Structure_Members_Table Complete Structure Members Table
  1043. <v>struct Flag</v>
  1044. <table border=1>
  1045. <tr>
  1046. <td>structure member</td>
  1047. <td>C type</td>
  1048. <td>required</td>
  1049. <td>default</td>
  1050. <td> description and example</td>
  1051. </tr><tr>
  1052. <td>key</td>
  1053. <td> char</td>
  1054. <td> YES</td>
  1055. <td> none</td>
  1056. <td> Key char used on command line<br>
  1057. flag->key = 'f' ;</td>
  1058. </tr><tr>
  1059. <td>Description</td>
  1060. <td> char *</td>
  1061. <td> YES</td>
  1062. <td> none</td>
  1063. <td> String describing flag meaning<br>
  1064. flag->description = _("run in fast mode") ;</td>
  1065. </tr><tr>
  1066. <td>answer</td>
  1067. <td> char</td>
  1068. <td> NO</td>
  1069. <td> NULL</td>
  1070. <td> Default and parser-returned
  1071. flag states.</td>
  1072. </tr>
  1073. </table>
  1074. <b>struct Option</b>
  1075. <table border=1>
  1076. <tr>
  1077. <td>structure member</td>
  1078. <td>C type </td>
  1079. <td>required </td>
  1080. <td>default </td>
  1081. <td>description and example</td>
  1082. </tr>
  1083. <tr>
  1084. <td>key </td>
  1085. <td>char * </td>
  1086. <td>YES </td>
  1087. <td>none </td>
  1088. <td>Key word used on command line.<br>
  1089. opt->key = "map" ;</td>
  1090. </tr>
  1091. <tr>
  1092. <td>type </td>
  1093. <td>int </td>
  1094. <td>YES </td>
  1095. <td>none </td>
  1096. <td>Option type: <br>
  1097. TYPE_STRING <br>
  1098. TYPE_INTEGER <br>
  1099. TYPE_DOUBLE <br>
  1100. opt->type = TYPE_STRING ;</td>
  1101. </tr>
  1102. <tr>
  1103. <td>Description </td>
  1104. <td>char * </td>
  1105. <td>YES </td>
  1106. <td>none </td>
  1107. <td>String describing option along with gettext macro for internationalization
  1108. opt->description = _("Map name") ;</td>
  1109. </tr>
  1110. <tr>
  1111. <td>answer </td>
  1112. <td>char * </td>
  1113. <td>NO </td>
  1114. <td>NULL </td>
  1115. <td>Default and parser-returned answer to an option.<br>
  1116. opt->answer = "defaultmap" ;</td>
  1117. </tr>
  1118. <tr>
  1119. <td>key_desc </td>
  1120. <td>char * </td>
  1121. <td>NO </td>
  1122. <td>NULL </td>
  1123. <td>Single word describing the key. Commas in this string denote
  1124. to the parser that several comma-separated arguments are expected
  1125. from the user as one answer. For example, if a pair of coordinates
  1126. is desired, this element might be defined as follows.<br>
  1127. opt->key_desc = "x,y" ; </td>
  1128. </tr>
  1129. <tr>
  1130. <td>structure member</td>
  1131. <td>C type </td>
  1132. <td>required </td>
  1133. <td>default </td>
  1134. <td>description and example</td>
  1135. </tr>
  1136. <tr>
  1137. <td>multiple </td>
  1138. <td>int </td>
  1139. <td>NO </td>
  1140. <td>NO </td>
  1141. <td>Indicates whether the user can provide multiple answers or not.
  1142. YES and NO are defined in "gis.h" and should be used (NO is
  1143. the default.) Multiple is used in conjunction with the answers
  1144. structure member below. opt->multiple = NO ;</td>
  1145. </tr>
  1146. <tr>
  1147. <td>answers </td>
  1148. <td> </td>
  1149. <td>NO </td>
  1150. <td>NULL </td>
  1151. <td>Multiple parser-returned answers to an option. N/A</td>
  1152. </tr>
  1153. <tr>
  1154. <td>required </td>
  1155. <td>int </td>
  1156. <td>NO </td>
  1157. <td>NO </td>
  1158. <td>Indicates whether user MUST provide the option on the command
  1159. line. YES and NO are defined in "gis.h" and should be used (NO
  1160. is the default.) opt->required = YES ;</td>
  1161. </tr>
  1162. <tr>
  1163. <td>options </td>
  1164. <td>char * </td>
  1165. <td>NO </td>
  1166. <td>NULL </td>
  1167. <td>Approved values or range of values. <br>
  1168. opt->options = "red,blue,white" ;<br>
  1169. For integers and doubles, the following format is available: <br>
  1170. opt->options = "0-1000" ;</td>
  1171. </tr>
  1172. <tr>
  1173. <td>gisprompt</td>
  1174. <td>char *</td>
  1175. <td>NO</td>
  1176. <td>NULL</td>
  1177. <td>Interactive prompt guidance. There are three comma separated
  1178. parts to this argument which guide the use of the standard GRASS
  1179. file name prompting routines.<br>
  1180. opt->gisprompt = "old,cell,raster" ;</td>
  1181. </tr>
  1182. <tr>
  1183. <td>checker</td>
  1184. <td>char *()</td>
  1185. <td>NO</td>
  1186. <td>NULL</td>
  1187. <td>Routine to check the answer to an option<br>
  1188. m opt->checker = my_routine() ;</td>
  1189. </tr>
  1190. </table>
  1191. \subsection Description_of_Complex_Structure_Members Description of Complex Structure Members
  1192. What follows are explanations of possibly confusing structure
  1193. members. It is intended to clarify and supplement the structures table
  1194. above.
  1195. \subsection Answer_member_of_the_Flag_and_Option_structures Answer member of the Flag and Option structures
  1196. The answer structure member serves two functions for GRASS commands
  1197. that use the parser.
  1198. <b>(1) To set the default answer to an option:</b>
  1199. If a default state is desired for a programmer-defined option, the
  1200. programmer may define the Option structure member "answer" before
  1201. calling G_parser() in his module. After the G_parser() call, the
  1202. answer member will hold this preset default value if the user did
  1203. <i>not</i> enter an option that has the default answer member value.
  1204. <b>(2) To obtain the command-line answer to an option or flag:</b>
  1205. After a call to G_parser(), the answer member will contain one of two
  1206. values:
  1207. - (a) If the user provided an option, and answered this option on the
  1208. command line, the default value of the answer member (as described
  1209. above) is replaced by the user's input.
  1210. - (b) If the user provided an option, but did <i>not</i> answer this
  1211. option on the command line, the default is not used. The user may use
  1212. the default answer to an option by withholding mention of the option
  1213. on the command line. But if the user enters an option without an
  1214. answer, the default answer member value will be replaced and set to a
  1215. NULL value by G_parser().
  1216. As an example, please review the use of answer members in the structures
  1217. implemented in \ref Full_Module_Example.
  1218. \subsection Multiple_and_Answers_Members Multiple and Answers Members
  1219. The functionality of the answers structure member is reliant on the
  1220. programmer's definition of the multiple structure member. If the multiple
  1221. member is set to NO, the answer member is used to obtain the answer to an
  1222. option as described above.
  1223. If the multiple structure member is set to YES, the programmer has
  1224. told G_parser() to capture multiple answers. Multiple answers are
  1225. separated by <em>commas</em> on the command line after an option.
  1226. <b>Note:</b> G_parser() does not recognize any character other than a
  1227. comma to delimit multiple answers.
  1228. After the programmer has set up an option to receive multiple answers,
  1229. these the answers are stored in the answers member of the Option
  1230. structure. The answers member is an array that contains each
  1231. individual user-entered answer. The elements of this array are the
  1232. type specified by the programmer using the type member. The answers
  1233. array contains however many comma-delimited answers the user entered,
  1234. followed (terminated) by a NULL array element.
  1235. For example, here is a sample definition of an Option using multiple
  1236. and answers structure members:
  1237. \code
  1238. opt->key ="option";
  1239. opt->description = _("option example");
  1240. opt->type = TYPE_INTEGER;
  1241. opt->required = NO;
  1242. opt->multiple = YES;
  1243. \endcode
  1244. The above definition would ask the user for multiple integer answers
  1245. to the option. If in response to a routine that contained the above
  1246. code, the user entered "option=1,3,8,15" on the command line, the
  1247. answers array would contain the following values:
  1248. \code
  1249. answers[0] == 1
  1250. answers[1] == 3
  1251. answers[2] == 8
  1252. answers[3] == 15
  1253. answers[4] == NULL
  1254. \endcode
  1255. \subsection key_desc_Member key_desc Member
  1256. The <b>key_desc</b> structure member is used to define the format of a single
  1257. command line answer to an option. A programmer may wish to ask for one
  1258. answer to an option, but this answer may not be a single argument of a type
  1259. set by the type structure member. If the programmer wants the user to enter
  1260. a coordinate, for example, the programmer might define an Option as follows:
  1261. \code
  1262. opt->key ="coordinate";
  1263. opt->description = _("Specified Coordinate");
  1264. opt->type = TYPE_INTEGER;
  1265. opt->required = NO;
  1266. opt->key_desc = "x,y"
  1267. opt->multiple = NO;
  1268. \endcode
  1269. The answer to this option would <i>not</i> be stored in the answer
  1270. member, but in the answers member. If the user entered
  1271. "coordinate=112,225" on the command line in response to a routine that
  1272. contains the above option definition, the answers array would have the
  1273. following values after the call to G_parser():
  1274. \code
  1275. answers[0] == 112
  1276. answers[1] == 225
  1277. answers[2] == NULL
  1278. \endcode
  1279. Note that "coordinate=112" would not be valid, as it does not contain both
  1280. components of an answer as defined by the key_desc structure member.
  1281. If the multiple structure member were set to YES instead of NO in the
  1282. example above, the answers are stored sequentially in the answers
  1283. member. For example, if the user wanted to enter the coordinates
  1284. (112,225), (142,155), and (43,201), his response on the command line
  1285. would be "coordinate=112,225,142,155,43,201". Note that G_parser()
  1286. recognizes only a comma for both the key_desc member, and for multiple
  1287. answers.
  1288. The answers array would have the following values after a call to
  1289. G_parser():
  1290. \code
  1291. answers[0] == 112 answers[1] == 225
  1292. answers[2] == 142 answers[3] == 155
  1293. answers[4] == 43 answers[5] == 201
  1294. answers[6] == NULL
  1295. \endcode
  1296. <B>Note.</B> In this case as well, neither "coordinate=112" nor
  1297. "coordinate=112,225,142" would be valid command line arguments, as
  1298. they do not contain even pairs of coordinates. Each answer's format
  1299. (as described by the key_desc member) must be fulfilled completely.
  1300. The overall function of the key_desc and multiple structure members is
  1301. very similar. The key_desc member is used to specify the number of
  1302. <i>required</i> components of a single option answer (e.g. a
  1303. multi-valued coordinate.) The multiple member tells G_parser() to ask
  1304. the user for multiple instances of the compound answer as defined by
  1305. the format in the key_desc structure member.
  1306. Another function of the key_desc structure member is to explain to the
  1307. user the type of information expected as an answer. The coordinate
  1308. example is explained above.
  1309. The usage message that is displayed by G_parser() in case of an error,
  1310. or by G_usage() on programmer demand, is shown below. The Option
  1311. "option" for the command <tt>a.out</tt> does not have its key_desc
  1312. structure member defined.
  1313. \verbatim
  1314. Usage:
  1315. a.out option=name
  1316. \endverbatim
  1317. The use of "name" is a G_parser() standard. If the programmer defines
  1318. the key_desc structure member before a call to G_parser(), the
  1319. value of the key_desc member replaces "name". Thus, if the key_desc
  1320. member is set to "x,y" as was used in an example above, the following
  1321. usage message would be displayed:
  1322. \verbatim
  1323. Usage:
  1324. a.out option=x,y
  1325. \endverbatim
  1326. The key_desc structure member can be used by the programmer to clarify
  1327. the usage message as well as specify single or multiple required
  1328. components of a single option answer.
  1329. \subsection gisprompt_Member gisprompt Member
  1330. The <em>gisprompt</em> Option structure item requires a bit more
  1331. description. The three comma-separated (no spaces allowed)
  1332. sub-arguments are defined as follows:
  1333. - First argument: "old" results in a call to the GRASS library
  1334. subroutine G_open_old(), "new" to G_open_new(), otherwise "any" or
  1335. "mapset".
  1336. - If any option has "new" as the first component, the <tt>--o</tt>
  1337. (overwrite) flag will be listed in the module's interface
  1338. (<tt>--help</tt> output, manual page, GUI dialog, etc).
  1339. - If an option which has "new" as the first component is given, the
  1340. parser checks whether the entity (map, etc.) already exists.
  1341. - Second argument: This is identical to the "element" argument in the
  1342. above subroutine calls. It specifies a directory inside the mapset
  1343. that may contain the user's response. In other words the second field
  1344. is used to determine where to look for the file (i.e. if the option
  1345. has "new,cell,...", it will look in the "cell" directory). The second
  1346. field should be the name of one of the standard subdirectories of the
  1347. mapset, as listed in $GISBASE/etc/element_list.
  1348. - Third argument: Identical to the "prompt" argument in the above
  1349. subroutine calls. This is a string presented to the user that
  1350. describes the type of data element being requested.
  1351. Here are two examples:
  1352. \verbatim
  1353. "new,cell,raster" G_open_new("cell", "map")
  1354. "old,vector,vector" G_open_old("vector", "map")
  1355. \endverbatim
  1356. The gisprompt values are passed to any GUI code, both self-contained
  1357. dialogs generated by the parser for the <tt>--ui</tt> option, and
  1358. stand-alone GUIs (wxGUI) which use the <tt>--xml-description</tt>
  1359. flags to obtain a machine-readable description of the module's
  1360. interface. How the GUI interprets this is up to the GUI.
  1361. \subsection Common_Questions Common Questions
  1362. - "How is automatic prompting turned off?"
  1363. GRASS 4.0 introduced a new method for driving GRASS interactive and
  1364. non-interactive modules as described in \ref
  1365. Compiling_and_Installing_GRASS_Programs. Here is a short overview.
  1366. For most modules a user runs a front-end module out of the GRASS bin
  1367. directory which in turn looks for the existence of interactive and
  1368. non-interactive versions of the module. If an interactive version
  1369. exists and the user provided no command line arguments, then that
  1370. version is executed.
  1371. In such a situation, the parser's default interaction will never be
  1372. seen by the user. A programmer using the parser is able to avoid the
  1373. front-end's default search for a fully interactive version of the
  1374. command by placing a call to G_disable_interactive() before
  1375. calling G_parser() (see \ref Parser_Routines for details).
  1376. - "Can the user mix options and flags?"
  1377. Yes. Options and flags can be given in any order.
  1378. - "In what order does the parser present options and flags?"
  1379. Flags and options are presented by the usage message in the order that
  1380. the programmer defines them using calls to G_define_option()
  1381. and G_define_flag().
  1382. - "How does a programmer query for coordinates?"
  1383. For any user input that requires a set of arguments (like a pair of
  1384. map coordinates,) the programmer specifies the number of arguments in
  1385. the key_desc member of the Option structure. For example, if
  1386. opt-&gt;key_desc was set to "x,y", the parser will require that the
  1387. user enter a pair of arguments separated only by a comma. See the
  1388. source code for the GRASS commands <tt>r.drain</tt> or <tt>r.cost</tt>
  1389. for examples.
  1390. - "Is a user required to use full option names?"
  1391. No. Users are required to type in only as many characters of an option name
  1392. as is necessary to make the option choice unambiguous. If, for example,
  1393. there are two options, "input=" and "output=", the following would be
  1394. valid command line arguments:
  1395. \verbatim
  1396. # command i=map1 o=map2
  1397. # command in=map1 out=map2
  1398. \endverbatim
  1399. - "Are options standardized at all?"
  1400. Yes. There are a few conventions. Options which identify a single input map
  1401. are usually "map=", not "raster=" or "vector=". In the case of an
  1402. input and output map the convention is: "input=xx output=yy". By passing
  1403. the 'help' option to existing GRASS commands, it is likely that you will
  1404. find other conventions. The desire is to make it as easy as possible for the
  1405. user to remember (or guess correctly) what the command line syntax is for a
  1406. given command.
  1407. \section String_Manipulation_Functions String Manipulation Functions
  1408. This section describes some routines which perform string manipulation.
  1409. Strings have the usual C meaning: a NULL terminated array of characters.
  1410. These next 3 routines remove unwanted white space from a single string.
  1411. - G_squeeze()
  1412. Leading and trailing white space is removed from the string and
  1413. internal white space which is more than one character is reduced to a
  1414. single space character. White space here means spaces, tabs,
  1415. linefeeds, newlines, and formfeeds.
  1416. - G_strip()
  1417. Leading and trailing white space is removed from the string. White
  1418. space here means only spaces and tabs. There is no return value.
  1419. - G_chop()
  1420. Chop leading and trailing white spaces: space, &#92;f, &#92;n, &#92;r,
  1421. &#92;t, &#92;v.
  1422. The next routines replaces character(s) from string.
  1423. - G_strchg()
  1424. Replace all occurencies of character in string with new.
  1425. This next routine copies a string to allocated memory.
  1426. - G_store()
  1427. This routine allocates enough memory to hold the string, and returns a
  1428. pointer to the allocated memory.
  1429. The next 2 routines convert between upper and lower case.
  1430. - G_tolcase()
  1431. Upper case letters in the string are converted to their lower case
  1432. equivalent.
  1433. - G_toucase()
  1434. Lower case letters in the string are converted to their upper case
  1435. equivalent.
  1436. - G_trim_decimal()
  1437. This routine remove trailing zeros from decimal number for example:
  1438. 23.45000 would come back as 23.45.
  1439. - G_index()
  1440. - G_rindex()
  1441. Get position of delimiter.
  1442. - G_strcasecmp()
  1443. String compare ignoring case (upper or lower).
  1444. - G_strstr()
  1445. Return a pointer to the first occurrence of subString in mainString,
  1446. or NULL if no occurrences are found.
  1447. - G_strdup()
  1448. Returns a pointer to a string that is a duplicate of the string given
  1449. to G_strdup. The duplicate is created using malloc. If unable to
  1450. allocate the required space, NULL is returned.
  1451. \section Enhanced_UNIX_Routines Enhanced UNIX Routines
  1452. A number of useful UNIX library routines have side effects which are
  1453. sometimes undesirable. The routines here provide the same functions as
  1454. their corresponding UNIX routine, but with different side effects.
  1455. \subsection Running_in_the_Background Running in the Background
  1456. The standard UNIX fork() routine creates a child process which is a
  1457. copy of the parent process. The fork() routine is useful for placing a
  1458. module into the background. For example, a module that gathers input
  1459. from the user interactively, but knows that the processing will take a
  1460. long time, might want to run in the background after gathering all the
  1461. input. It would fork() to create a child process, the parent would
  1462. exit() allowing the child to continue in the background, and the user
  1463. could then do other processing.
  1464. However, there is a subtle problem with this logic. The fork() routine does not
  1465. protect child processes from keyboard interrupts even if the parent is no longer
  1466. running. Keyboard interrupts will also kill background processes that do not
  1467. protect themselves.
  1468. Note: Programmers who use /bin/sh know that programs run in the
  1469. background (using & on the command line) are not automatically
  1470. protected from keyboard interrupts. To protect a command that is run
  1471. in the background, /bin/sh users must do nohup command &. Programmers
  1472. who use the /bin/csh (or other variants) do not know, or forget that
  1473. the C-shell automatically protects background processes from keyboard
  1474. interrupts.
  1475. Thus a module which puts itself in the background may never finish if
  1476. the user interrupts another module which is running at the keyboard.
  1477. \subsection Partially_Interruptible_System_Call Partially Interruptible System Call
  1478. The UNIX system() call allows one program, the parent, to execute
  1479. another UNIX command or module as a child process, wait for that
  1480. process to complete, and then continue. The problem addressed here
  1481. concerns interrupts. During the standard system() call, the child
  1482. process inherits its responses to interrupts from the parent. This
  1483. means that if the parent is ignoring interrupts, the child will ignore
  1484. them as well. If the parent is terminated by an interrupt, the child
  1485. will be also.
  1486. However, in some cases, this may not be the desired effect. In a menu
  1487. environment where the parent activates menu choices by running
  1488. commands using the system() call, it would be nice if the user could
  1489. interrupt the command, but not terminate the menu module itself. The
  1490. G_system() call allows this.
  1491. - G_system()
  1492. The shell level <em>command</em> is executed. Interrupt signals for
  1493. the parent module are ignored during the call. Interrupt signals for
  1494. the command are enabled. The interrupt signals for the parent are
  1495. restored to their previous settings upon return.
  1496. G_system() returns the same value as system(), which is essentially
  1497. the exit status of the <B>command.</B> See UNIX manual system(1) for
  1498. details.
  1499. \subsection ENDIAN_test ENDIAN test
  1500. To test if the user's machine is little or big ENDIAN, the following
  1501. function is provided:
  1502. - G_is_little_endian()
  1503. Test if machine is little or big endian.
  1504. \subsection Miscellaneous Miscellaneous
  1505. A number of general purpose routines have been provided.
  1506. - G_date()
  1507. Returns a pointer to a string which is the current date and time. The
  1508. format is the same as that produced by the UNIX <tt>date</tt> command.
  1509. - G_home()
  1510. Returns a pointer to a string which is the full path name of the
  1511. user's home directory.
  1512. - G_percent()
  1513. This routine prints a percentage complete message to stderr. Example:
  1514. \code
  1515. #include<grass/gis.h>
  1516. #include<grass/glocale.h>
  1517. int row;
  1518. int nrows;
  1519. nrows = 1352; /* 1352 is not a special value - example only */
  1520. G_message(_("Percent complete:"));
  1521. for (row = 0; row < nrows; row++)
  1522. G_percent(row, nrows, 10);
  1523. \endcode
  1524. This will print completion messages at 10% increments; i.e., 10%, 20%,
  1525. 30%, etc., up to 100%. Each message does not appear on a new line, but
  1526. rather erases the previous message. After 100%, a new line is printed.
  1527. - G_program_name()
  1528. Routine returns the name of the module as set by the call to
  1529. G_gisinit().
  1530. - G_whoami()
  1531. Returns a pointer to a string which is the user's login name.
  1532. \section GIS_Library_Data_Structures GIS Library Data Structures
  1533. Some of the data structures, defined in the "gis.h" header file and used
  1534. by routines in this library, are described in the sections below.
  1535. \subsection struct_Cell_head struct Cell_head
  1536. The raster header data structure is used for two purposes. It is used
  1537. for raster header information for map layers. It also used to hold
  1538. region values. The structure is:
  1539. \code
  1540. struct Cell_head
  1541. {
  1542. int format; /* max number of bytes per cell minus 1 */
  1543. int compressed; /* 0 = uncompressed, 1 = compressed, -1 pre 3.0 */
  1544. int rows; /* number of rows in the data 2D */
  1545. int rows3; /* number of rows in the data 3D */
  1546. int cols; /* number of columns in the data 2D */
  1547. int cols3; /* number of columns in the data 3D */
  1548. int depths; /* number of depths in data */
  1549. int proj; /* projection (see #defines above) */
  1550. int zone; /* projection zone */
  1551. double ew_res; /* east to west cell size 2D */
  1552. double ew_res3; /* east to west cell size 3D */
  1553. double ns_res; /* north to south cell size 2D */
  1554. double ns_res3; /* north to south cell size 3D */
  1555. double tb_res; /* top to bottom cell size */
  1556. double north; /* coordinates of map layer */
  1557. double south;
  1558. double east;
  1559. double west;
  1560. double top;
  1561. double bottom;
  1562. };
  1563. \endcode
  1564. The <i>format</i> and <i>compressed</i> fields apply only to raster
  1565. headers. The <i>format</i> field describes the number of bytes per
  1566. raster data value and the <i>compressed</i> field indicates if the
  1567. raster file is compressed or not. The other fields apply both to
  1568. raster headers and regions. The geographic boundaries are described by
  1569. <i>north, south, east</i> and <i>west</i>. The grid resolution is
  1570. described by <i>ew_res</i> and <i>ns_res</i>. The cartographic
  1571. projection is described by <i>proj</i> and the related zone for the
  1572. projection by <i>zone</i>. The <i>rows</i> and <i>cols</i> indicate
  1573. the number of rows and columns in the raster file, or in the
  1574. region. See \ref Raster_Header_File for more information about
  1575. raster headers, and \ref Region for more information about regions.
  1576. The routines described in \ref Raster_Header_File use this structure.
  1577. \subsection struct_Categories struct Categories
  1578. The <i>Categories</i> structure contains a title for the map layer,
  1579. the largest category in the map layer, an automatic label generation
  1580. rule for missing labels, and a list of category labels.
  1581. \code
  1582. struct Categories
  1583. {
  1584. CELL ncats; /* total number of categories */
  1585. CELL num; /* the highest cell values. Only exists
  1586. for backwards compatibility = (CELL)
  1587. max_fp_values in quant rules */
  1588. char *title; /* name of data layer */
  1589. char *fmt; /* printf-like format to generate labels */
  1590. float m1; /* multiplication coefficient 1 */
  1591. float a1; /* addition coefficient 1 */
  1592. float m2; /* multiplication coefficient 2 */
  1593. float a2; /* addition coefficient 2 */
  1594. struct Quant q; /* rules mapping cell values to index in
  1595. list of labels */
  1596. char **labels; /* array of labels of size num */
  1597. int *marks; /* was the value with this label was used? */
  1598. int nalloc;
  1599. int last_marked_rule;
  1600. };
  1601. \endcode
  1602. This structure should be accessed using the routines described in
  1603. \ref Raster_Category_File.
  1604. \subsection struct_Colors struct Colors
  1605. The color data structure holds red, green, and blue color intensities
  1606. for raster categories. The structure has become so complicated that it
  1607. will not be described in this manual.
  1608. \code
  1609. struct Colors
  1610. {
  1611. int version; /* set by read_colors: -1=old, 1=new */
  1612. DCELL shift;
  1613. int invert;
  1614. int is_float; /* defined on floating point raster data? */
  1615. int null_set; /* the colors for null are set? */
  1616. unsigned char null_red;
  1617. unsigned char null_grn;
  1618. unsigned char null_blu;
  1619. int undef_set; /* the colors for cells not in range are set? */
  1620. unsigned char undef_red;
  1621. unsigned char undef_grn;
  1622. unsigned char undef_blu;
  1623. struct _Color_Info_ fixed;
  1624. struct _Color_Info_ modular;
  1625. DCELL cmin;
  1626. DCELL cmax;
  1627. int organizing;
  1628. };
  1629. \endcode
  1630. The routines described in \ref Raster_Color_Table must be used
  1631. to store and retrieve color information using this structure.
  1632. \subsection struct_History struct History
  1633. The <i>History</i> structure is used to document raster files. The
  1634. information contained here is for the user. It is not used in any
  1635. operational way by GRASS. The structure is:
  1636. \code
  1637. # define MAXEDLINES 50
  1638. # define RECORD_LEN 80
  1639. struct History
  1640. {
  1641. char mapid[RECORD_LEN];
  1642. char title[RECORD_LEN];
  1643. char mapset[RECORD_LEN];
  1644. char creator[RECORD_LEN];
  1645. char maptype[RECORD_LEN];
  1646. char datsrc_1[RECORD_LEN];
  1647. char datsrc_2[RECORD_LEN];
  1648. char keywrd[RECORD_LEN];
  1649. int edlinecnt;
  1650. char edhist[MAXEDLINES][RECORD_LEN];
  1651. };
  1652. \endcode
  1653. The <i>mapid</i> and <i>mapset</i> are the raster file name and
  1654. mapset, <i>title</i> is the raster file title, <i>creator</i> is the
  1655. user who created the file, <i>maptype</i> is the map type (which
  1656. should always be "raster"), <i>datasrc_1</i> and <i>datasrc_2</i>
  1657. describe the original data source, <i>keywrd</i> is a one-line data
  1658. description and <i>edhist</i> contains <i>edlinecnt</i> lines of user
  1659. comments.
  1660. The routines described in \ref Raster_History_File use this structure.
  1661. However, there is very little support for manipulating the contents of
  1662. this structure. The programmer must manipulate the contents directly.
  1663. <b>Note:</b> Some of the information in this structure is not
  1664. meaningful. For example, if the raster file is renamed, or copied
  1665. into another mapset, the <i>mapid</i> and <i>mapset</i> will no longer
  1666. be correct. Also the <i>title</i> does not reflect the true raster
  1667. file title. The true title is maintained in the category file.
  1668. <b>Warning:</b> This structure has remained unchanged since the inception of
  1669. GRASS. There is a good possibility that it will be changed or eliminated in
  1670. future releases.
  1671. \subsection struct_Range struct Range
  1672. The <i>Range</i> (<i>FPRange</i> for floating point raster maps)
  1673. structure contains the minimum and maximum values which occur in a
  1674. raster file.
  1675. \code
  1676. struct Range
  1677. {
  1678. CELL min;
  1679. CELL max;
  1680. int first_time; /* whether or not range was updated */
  1681. };
  1682. struct FPRange
  1683. {
  1684. DCELL min;
  1685. DCELL max;
  1686. int first_time; /* whether or not range was updated */
  1687. };
  1688. \endcode
  1689. The routines described in \ref Raster_Range_File should be used to access
  1690. this structure.
  1691. \section Loading_the_GIS_Library Loading the GIS Library
  1692. The library is loaded by specifying $(GISLIB) in the Makefile. The
  1693. following example is a complete Makefile which compiles code that uses
  1694. this library:
  1695. <b>Makefile for $(GISLIB)</b>
  1696. \verbatim
  1697. MODULE_TOPDIR = ../..
  1698. PGM = r.info
  1699. LIBES = $(GISLIB)
  1700. DEPENDENCIES = $(GISDEP)
  1701. include $(MODULE_TOPDIR)/include/Make/Module.make
  1702. default: cmd
  1703. \endverbatim
  1704. See \ref Compiling_and_Installing_GRASS_Modules for a complete
  1705. discussion of Makefiles.
  1706. \section TimeStamp_functions Timestamp functions
  1707. This structure is defined in "gis.h", but there should be no reason to access its
  1708. elements directly:
  1709. \code
  1710. struct TimeStamp {
  1711. DateTime dt[2]; /* two datetimes */
  1712. int count;
  1713. };
  1714. \endcode
  1715. Using the G_*_timestamp() routines reads/writes a timestamp file in
  1716. the cell_misc/rastername mapset element.
  1717. A TimeStamp can be one DateTime, or two DateTimes representing a
  1718. range. When preparing to write a TimeStamp, the programmer should
  1719. use one of:
  1720. - G_set_timestamp() to set a single DateTime
  1721. - G_set_timestamp_range() to set two DateTimes
  1722. - G_read_raster_timestamp to read raster timestamp
  1723. - G_read_vector_timestamp to read vector timestamp
  1724. - G_get_timestamps() to copy TimeStamp into Datetimes
  1725. Use to copy the TimeStamp information into Datetimes, so the members
  1726. of struct TimeStamp shouldn't be accessed directly.
  1727. - count=0 means no datetimes were copied
  1728. - count=1 means 1 datetime was copied into dt1
  1729. - count=2 means 2 datetimes were copied
  1730. - G_init_timestamp()
  1731. Sets ts-&gt;count = 0, to indicate no valid DateTimes are in TimeStamp.
  1732. - G_set_timestamp()
  1733. Copies a single DateTime to a TimeStamp in preparation for writing
  1734. (overwrites any existing information in TimeStamp).
  1735. - G_set_timestamp_range()
  1736. Copies two DateTimes (a range) to a TimeStamp in preparation for
  1737. writing (overwrites any existing information in TimeStamp).
  1738. - G_write_raster_timestamp()
  1739. - G_write_vector_timestamp()
  1740. - G_write_grid3_timestamp()
  1741. - G_format_timestamp()
  1742. - G_scan_timestamp()
  1743. - G_remove_raster_timestamp()
  1744. - G_remove_vector_timestamp()
  1745. - G_remove_grid3_timestamp()
  1746. Only timestamp files in current mapset can be removed.
  1747. - G_read_grid3_timestamp()
  1748. See \ref DateTime_Library for a complete discussion of GRASS datetime
  1749. routines.
  1750. \section Memory_Allocation Memory Allocation
  1751. The following routines provide memory allocation capability. They are
  1752. simply calls to the UNIX suite of memory allocation routines malloc(),
  1753. realloc() and calloc(), except that if there is not enough memory,
  1754. they print a diagnostic message to that effect and then call exit().
  1755. - G_malloc ()
  1756. Allocates a block of memory at least <i>size</i> bytes which is
  1757. aligned properly for all data types. A pointer to the aligned block is
  1758. returned.
  1759. - G_realloc()
  1760. Changes the <i>size</i> of a previously allocated block of memory at
  1761. <i>ptr</i> and returns a pointer to the new block of memory. The
  1762. <i>size</i> may be larger or smaller than the original size. If the
  1763. original block cannot be extended "in place", then a new block is
  1764. allocated and the original block copied to the new block.
  1765. <b>Note:</b> If <i>ptr</i> is NULL, then this routine simply allocates
  1766. a block of <i>size</i> bytes. This routine works around broken
  1767. realloc() routines, which do not handle a NULL <i>ptr</i>.
  1768. - G_calloc()
  1769. Allocates a properly aligned block of memory <i>n</i>*<i>size</i>
  1770. bytes in length, initializes the allocated memory to zero, and returns
  1771. a pointer to the allocated block of memory.
  1772. - G_free()
  1773. Use the G_free() routine to release memory allocated by
  1774. these routines.
  1775. */