gislib.dox 79 KB

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