gislib.dox 112 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691770177117721773177417751776177717781779178017811782178317841785178617871788178917901791179217931794179517961797179817991800180118021803180418051806180718081809181018111812181318141815181618171818181918201821182218231824182518261827182818291830183118321833183418351836183718381839184018411842184318441845184618471848184918501851185218531854185518561857185818591860186118621863186418651866186718681869187018711872187318741875187618771878187918801881188218831884188518861887188818891890189118921893189418951896189718981899190019011902190319041905190619071908190919101911191219131914191519161917191819191920192119221923192419251926192719281929193019311932193319341935193619371938193919401941194219431944194519461947194819491950195119521953195419551956195719581959196019611962196319641965196619671968196919701971197219731974197519761977197819791980198119821983198419851986198719881989199019911992199319941995199619971998199920002001200220032004200520062007200820092010201120122013201420152016201720182019202020212022202320242025202620272028202920302031203220332034203520362037203820392040204120422043204420452046204720482049205020512052205320542055205620572058205920602061206220632064206520662067206820692070207120722073207420752076207720782079208020812082208320842085208620872088208920902091209220932094209520962097209820992100210121022103210421052106210721082109211021112112211321142115211621172118211921202121212221232124212521262127212821292130213121322133213421352136213721382139214021412142214321442145214621472148214921502151215221532154215521562157215821592160216121622163216421652166216721682169217021712172217321742175217621772178217921802181218221832184218521862187218821892190219121922193219421952196219721982199220022012202220322042205220622072208220922102211221222132214221522162217221822192220222122222223222422252226222722282229223022312232223322342235223622372238223922402241224222432244224522462247224822492250225122522253225422552256225722582259226022612262226322642265226622672268226922702271227222732274227522762277227822792280228122822283228422852286228722882289229022912292229322942295229622972298229923002301230223032304230523062307230823092310231123122313231423152316231723182319232023212322232323242325232623272328232923302331233223332334233523362337233823392340234123422343234423452346234723482349235023512352235323542355235623572358235923602361236223632364236523662367236823692370237123722373237423752376237723782379238023812382238323842385238623872388238923902391239223932394239523962397239823992400240124022403240424052406240724082409241024112412241324142415241624172418241924202421242224232424242524262427242824292430243124322433243424352436243724382439244024412442244324442445244624472448244924502451245224532454245524562457245824592460246124622463246424652466246724682469247024712472247324742475247624772478247924802481248224832484248524862487248824892490249124922493249424952496249724982499250025012502250325042505250625072508250925102511251225132514251525162517251825192520252125222523252425252526252725282529253025312532253325342535253625372538253925402541254225432544254525462547254825492550255125522553255425552556255725582559256025612562256325642565256625672568256925702571257225732574257525762577257825792580258125822583258425852586258725882589259025912592259325942595259625972598259926002601260226032604260526062607260826092610261126122613261426152616261726182619262026212622262326242625262626272628262926302631263226332634263526362637263826392640264126422643264426452646264726482649265026512652265326542655265626572658265926602661266226632664266526662667266826692670267126722673267426752676267726782679268026812682268326842685268626872688268926902691269226932694269526962697269826992700270127022703270427052706270727082709271027112712271327142715271627172718271927202721272227232724272527262727272827292730273127322733273427352736273727382739274027412742274327442745274627472748274927502751275227532754275527562757275827592760276127622763276427652766276727682769277027712772277327742775277627772778277927802781278227832784278527862787278827892790279127922793279427952796279727982799280028012802280328042805280628072808280928102811281228132814281528162817281828192820282128222823282428252826282728282829283028312832283328342835283628372838283928402841284228432844284528462847284828492850285128522853285428552856285728582859286028612862286328642865286628672868286928702871287228732874287528762877287828792880288128822883288428852886288728882889289028912892289328942895289628972898289929002901290229032904290529062907290829092910291129122913291429152916291729182919292029212922292329242925292629272928292929302931293229332934293529362937293829392940294129422943294429452946294729482949295029512952295329542955295629572958295929602961296229632964296529662967296829692970297129722973297429752976297729782979298029812982298329842985298629872988298929902991299229932994299529962997299829993000300130023003300430053006300730083009301030113012301330143015301630173018301930203021302230233024302530263027302830293030303130323033303430353036303730383039304030413042304330443045304630473048304930503051305230533054305530563057305830593060306130623063306430653066306730683069307030713072307330743075307630773078307930803081308230833084308530863087308830893090309130923093309430953096309730983099310031013102310331043105310631073108310931103111311231133114311531163117311831193120312131223123312431253126312731283129313031313132313331343135313631373138313931403141314231433144314531463147314831493150315131523153315431553156315731583159316031613162316331643165316631673168316931703171317231733174317531763177317831793180318131823183318431853186318731883189319031913192319331943195319631973198319932003201320232033204320532063207320832093210321132123213321432153216321732183219322032213222322332243225322632273228322932303231323232333234323532363237323832393240324132423243324432453246324732483249325032513252325332543255325632573258325932603261326232633264326532663267326832693270327132723273327432753276327732783279328032813282328332843285328632873288328932903291329232933294329532963297329832993300330133023303330433053306330733083309331033113312331333143315331633173318331933203321332233233324332533263327332833293330333133323333333433353336333733383339334033413342334333443345334633473348334933503351335233533354335533563357335833593360336133623363336433653366336733683369337033713372337333743375337633773378337933803381338233833384338533863387338833893390339133923393339433953396339733983399340034013402340334043405340634073408340934103411341234133414341534163417341834193420342134223423342434253426342734283429343034313432343334343435343634373438343934403441344234433444344534463447344834493450345134523453345434553456345734583459346034613462346334643465346634673468346934703471347234733474347534763477347834793480348134823483348434853486348734883489349034913492349334943495349634973498349935003501350235033504350535063507350835093510351135123513351435153516351735183519352035213522352335243525352635273528352935303531353235333534353535363537353835393540354135423543354435453546354735483549355035513552355335543555355635573558355935603561356235633564356535663567356835693570357135723573357435753576357735783579358035813582358335843585358635873588358935903591359235933594359535963597359835993600360136023603360436053606360736083609361036113612361336143615361636173618361936203621362236233624362536263627362836293630363136323633363436353636363736383639364036413642364336443645364636473648364936503651365236533654365536563657365836593660366136623663366436653666366736683669367036713672367336743675367636773678367936803681368236833684368536863687368836893690369136923693369436953696369736983699
  1. /*! \page gislib GRASS GIS Library
  2. <!-- doxygenized from "GRASS 5 Programmer's Manual"
  3. by M. Neteler 2/2004, 2005, 2006
  4. -->
  5. This chapter is divided in the following pages:
  6. - \subpage gislibintro
  7. - \subpage gisrasterlib
  8. - \subpage gisvectorlib
  9. \section gislibintro Introduction
  10. This sections is divided as follows:
  11. - \subpage gislibintro
  12. - \subpage Raster_File_Processing
  13. - \subpage Vector_File_Processing
  14. - \subpage General_Plotting_Routines
  15. - \subpage Temporary_Files
  16. - \subpage Command_Line_Parsing
  17. - \subpage Full_Module_Example
  18. - \subpage Compiling_and_Installing_GRASS_Modules
  19. - \subpage Multiple_Architecture_Conventions
  20. - \subpage Full_Structure_Members_Description
  21. - \subpage Complete_Structure_Members_Table
  22. - \subpage Description_of_Complex_Structure_Members
  23. - \subpage String_Manipulation_Functions
  24. - \subpage Enhanced_UNIX_Routines
  25. - \subpage GIS_Library_Data_Structures
  26. - \subpage Loading_the_GIS_Library
  27. - \subpage TimeStamp_functions
  28. - \subpage GRASS_GIS_Library_Overview
  29. The <i>GIS Library</i> is the primary programming library provided
  30. with the GRASS system. <b>Programs must use this libary to access the
  31. database.</b> It contains the routines which locate, create, open,
  32. rename, and remove GRASS database files. It contains the routines
  33. which read and write raster files. It contains routines which
  34. interface the user to the database, including prompting the user,
  35. listing available files, validating user access, etc. It also has
  36. some general purpose routines (string manipulation, user information,
  37. etc.) which are not tied directly to database processing.
  38. It is assumed that the reader has read Database_Structure for a
  39. general description of GRASS databases, Raster_Maps for details about
  40. raster map layers in GRASS, and Region_and_Mask which discusses
  41. regions and masks. The routines in the <i>GIS Library</i> are
  42. presented in functional groupings, rather than in alphabetical
  43. order. The order of presentation will, it is hoped, provide a better
  44. understanding of how the library is to be used, as well as show the
  45. interrelationships among the various routines. Note that a good way to
  46. understand how to use these routines is to look at the source code for
  47. GRASS modules which use them. Most routines in this library require
  48. that the header file "gis.h" be included in any code using these
  49. routines. Therefore, programmers should always include this file when
  50. writing code using routines from this library:
  51. \verbatim
  52. #include <grass/gis.h>
  53. \endverbatim
  54. <b>Note</b>. All routines and global variables in this library,
  55. documented or undocumented, start with the prefix <b>G_</b>. To avoid
  56. name conflicts, programmers should not create variables or routines in
  57. their own modules which use this prefix.
  58. \subsection init Library Initialization
  59. <P>
  60. It is <B>mandatory</B> that the system be initialized before any other
  61. library routines are called.
  62. <P>
  63. int G_gisinit() initialize gis library
  64. This routine reads the user's GRASS environment file into memory and
  65. makes sure that the user has selected a valid database and mapset. It
  66. also initializes hidden variables used by other routines. If the
  67. user's database information is invalid, an error message is printed
  68. and the module exits. The <B>program_name</B> is stored for later
  69. recall by <I>G_program_name.</I> It is recommended that argv[0] be
  70. used for the <B>program_name:</B>
  71. \verbatim
  72. int main (int argc, char **argv)
  73. {
  74. G_gisinit(argv[0]);
  75. }
  76. \endverbatim
  77. \subsection diag Diagnostic Messages
  78. The following routines are used by other routines in the library to
  79. report warning and error messages. They may also be used directly by
  80. GRASS programs.
  81. <P> int G_fatal_error() print error message and exit
  82. <P> int G_debug() print debug message
  83. <P> int G_warning() print warning message and continue
  84. These routines report errors to the user. The normal mode is to write
  85. the <B>message</B> to the screen (on the standard error output) and
  86. wait a few seconds. G_warning() will return and
  87. G_fatal_error() will exit.
  88. <P>
  89. If the standard error output is not a tty device, then the message is
  90. mailed to the user instead.
  91. <P> If the file GIS_ERROR_LOG exists (with write permission), in
  92. either the user's home directory or in the $GISBASE directory, the
  93. messages will also be logged to this file.
  94. <P> While most applications will find the normal error reporting quite
  95. adequate, there will be times when different handling is needed. For
  96. example, graphics modules may want the messages displayed graphically
  97. instead of on the standard error output. If the programmer wants to
  98. handle the error messages differently, the following routines can be
  99. used to modify the error handling:
  100. <P>
  101. int G_set_error_routine() change error handling
  102. This routine provides a different error handler for G_fatal_error()
  103. and G_warning(). The <B>handler</B> routine must be defined as follows:
  104. \verbatim
  105. int handler (char *message, int fatal)
  106. \endverbatim
  107. <P>
  108. where <B>message</B> is the message to be handled and <B>fatal</B>
  109. indicates the type of error: 1 (fatal error) or 0 (warning).
  110. <P> <B>Note.</B> The handler only provides a way to send the message
  111. somewhere other than to the error output. If the error is fatal, the
  112. module will exit after the handler returns.
  113. <P>
  114. int G_unset_error_routine() reset normal error handling
  115. This routine resets the error handling for <I>G_fatal_error()</I> and
  116. <I>G_warning()</I> back to the default action.
  117. <P>
  118. int G_sleep_on_error() sleep on error?
  119. If <B>flag</B> is 0, then no pause will occur after printing an error
  120. or warning message. Otherwise the pause will occur.
  121. <P>
  122. int G_suppress_warnings() suppress warnings?
  123. If <B>flag</B> is 0, then <I>G_warning()</I> will no longer print
  124. warning messages. If <B>flag</B> is 1, then G_warning() will print
  125. warning messages.
  126. <P>
  127. <B>Note.</B> This routine has no effect on <I>G_fatal_error().</I>
  128. \subsection envir Environment and Database Information
  129. <P> The following routines return information about the current
  130. database selected by the user. Some of this information is retrieved
  131. from the user's GRASS environment file. Some of it comes from files in
  132. the database itself. See \ref Environment_Variables for a discussion of
  133. the GRASS environment.
  134. <P>
  135. The following four routines can be used freely by the programmer:
  136. <P> char * G_location() current location name
  137. Returns the name of the current database location. This routine should
  138. be used by modules that need to display the current location to the
  139. user. See \ref Locations for an explanation of locations.
  140. <P>
  141. char * G_mapset() current mapset name
  142. Returns the name of the current mapset in the current location. This
  143. routine is often used when accessing files in the current mapset. See
  144. Mapsets for an explanation of mapsets.
  145. <P>
  146. char * G_myname() location title
  147. Returns a one line title for the database location. This title is read
  148. from the file MYNAME in the PERMANENT mapset. See also
  149. \ref Permanent_Mapset for a discussion of the PERMANENT mapset.
  150. <P>
  151. char * G_gisbase() top level module directory
  152. Returns the full path name of the top level directory for GRASS
  153. programs. This directory will have subdirectories which will contain
  154. modules and files required for the running of the system. Some of
  155. these directories are:
  156. \verbatim
  157. bin commands run by the user
  158. etc modules and data files used by GRASS commands
  159. html help files
  160. \endverbatim
  161. <P>
  162. The use of G_gisbase() to find these subdirectories enables GRASS modules
  163. to be written independently of where the GRASS system is actually installed
  164. on the machine. For example, to run the module <I>sroff</I> in the GRASS
  165. <I>etc</I> directory:
  166. \verbatim
  167. char command[200];
  168. sprintf (command, "%s/etc/sroff", G_gisbase() );
  169. system (command);
  170. \endverbatim
  171. <P>
  172. The following two routines return full path UNIX directory names. They
  173. should be used only in special cases. They are used by other routines
  174. in the library to build full UNIX file names for database
  175. files. <B>The programmer should not use the next two routines to
  176. bypass the normal database access routines.</B>
  177. <P>
  178. char * G_gisdbase() top level database directory
  179. Returns the full UNIX path name of the directory which holds the database
  180. locations. See \ref GISDBASE for a full explanation of this directory.
  181. <P>
  182. char * G_location_path() current location directory
  183. Returns the full UNIX path name of the current database location. For
  184. example, if the user is working in location <I>spearfish</I> in the
  185. <I>/home/user/grassdata</I> database directory, this routine will
  186. return a string which looks like
  187. <P>
  188. <I>/home/user/grassdata/spearfish</I>.
  189. <BR>
  190. <P>
  191. These next routines provide the low-level management of the
  192. information in the user's GRASS environment file. <B>They should not
  193. be used in place of the higher level interface routines described
  194. above.</B>
  195. <P>
  196. int G_getenv() query GRASS environment variable
  197. <P>
  198. int G__getenv() query GRASS environment variable
  199. These routines look up the variable <B>name</B> in the GRASS
  200. environment and return its value (which is a character string). If
  201. <B>name</B> is not set, G_getenv() issues an error message and calls
  202. exit(). G__setenv() just returns the NULL pointer.
  203. <P>
  204. int G_setenv () set GRASS environment
  205. variable
  206. <P>
  207. int G__setenv() set GRASS environment variable
  208. These routines set the the GRASS environment variable <B>name</B>
  209. to <B>value.</B> If <B>value</B> is NULL, the <B>name</B> is unset.
  210. <P>
  211. Both routines set the value in module memory, but only G_setenv() writes the
  212. new value to the user's GRASS environment file.
  213. \subsection dbaseaccess Fundamental Database Access Routines
  214. <P>
  215. The routines described in this section provide the low-level interface
  216. to the GRASS database. They search the database for files, prompt the
  217. user for file names, open files for reading or writing, etc. The
  218. programmer should never bypass this level of database interface. These
  219. routines must be used to access the GRASS database unless there <B>are
  220. other higher level library routines which perform the same
  221. function.</B> For example, routines to process raster files
  222. (Raster_File_Processing), vector files (Vector_File_Processing),
  223. etc., should be used instead.
  224. <P>
  225. In the descriptions below, the term database <I>element</I> is used.
  226. Elements are subdirectories within a mapset and are associated with a
  227. specific GRASS data type. For example, raster files live in the "cell"
  228. and "fcell" element. See \ref Elements for more details.
  229. \subsection prompt Prompting for Database Files
  230. <P>
  231. The following routines interactively prompt the user for a file name
  232. from a specific database <B>element.</B> (See \ref Elements for an
  233. explanation of elements.) In each, the <B>prompt</B> string will be
  234. printed as the first line of the full prompt which asks the user to
  235. enter a file name. If <B>prompt</B> is the empty string "" then an
  236. appropriate prompt will be substituted. The name that the user enters
  237. is copied into the <B>name</B> buffer. The size of name should be
  238. large enough to hold any GRASS file name. Most systems allow file
  239. names to be quite long. It is recommended that name be declared char
  240. name[GNAME_MAX].
  241. The short (one or two word) <B>label</B> describing the <B>element</B>
  242. is used as part of a title when listing the files <B>in element.</B>
  243. <P>
  244. The user is required to enter a valid file name, or else hit the
  245. RETURN key to cancel the request. If the user enters an invalid
  246. response, a message is printed, and the user is prompted again. If the
  247. user cancels the request, the NULL pointer is returned. Otherwise the
  248. mapset where the file lives or is to be created is returned. Both the
  249. name and the mapset are used in other routines to refer to the file.
  250. <P>
  251. An example will be given here. The G_ask_old() routine used in the
  252. example is described a bit later. The user is asked to enter a file
  253. from the "paint/labels" element:
  254. \verbatim
  255. char name[GNAME_MAX];
  256. char *mapset;
  257. mapset = G_ask_old("", name, "paint/labels", "labels");
  258. if (mapset == NULL)
  259. exit(EXIT_SUCCESS); /* user canceled the request */
  260. \endverbatim
  261. <P>
  262. The user will see the following:
  263. \verbatim
  264. Enter the name of an existing labels file
  265. Enter 'list' for a list of existing labels files
  266. Hit RETURN to cancel request
  267. \endverbatim
  268. The last line of the prompt can be modified using G_set_ask_return_msg().
  269. <P>
  270. char * G_ask_old() prompt for existing database file
  271. The user is asked to enter the name of an existing database file.
  272. <P>
  273. <B>Note.</B> This routine looks for the file in the current mapset as
  274. well as other mapsets. The mapsets that are searched are determined
  275. from the user's mapset search path. See \ref Mapset_Search_Path for some
  276. more details about the search path.
  277. <P>
  278. char * G_ask_new() prompt for new database file
  279. The user is asked to enter the name of a new file which does not exist
  280. in the current mapset.
  281. <P>
  282. <B>Note.</B> The file chosen by the user may exist in other
  283. mapsets. This routine does not look in other mapsets, since the
  284. assumption is that <B>name</B> will be used to create a new file. New
  285. files are always created in the current mapset.
  286. <P>
  287. char * G_ask_in_mapset() prompt for existing database file
  288. The user is asked to enter the name of an file which exists in the
  289. current mapset.
  290. <P>
  291. <B>Note.</B> The file chosen by the user may or may not exist in other
  292. mapsets. This routine does not look in other mapsets, since the
  293. assumption is that <B>name</B> will be used to modify a file. GRASS
  294. only permits users to modify files in the current mapset.
  295. <P>
  296. char * G_ask_any() prompt for any valid file name
  297. The user is asked to enter any legal file name. If <B>warn</B> is 1
  298. and the file chosen exists in the current mapset, then the user is
  299. asked if it is ok to overwrite the file. If <B>warn</B> is 0, then any
  300. leg al name is accepted and no warning is issued to the user if the
  301. file exists.
  302. <P>
  303. int G_set_ask_return_msg() set Hit RETURN msg
  304. The "Hit RETURN to cancel request" part of the prompt in the prompting
  305. routines described above, is modified to "Hit RETURN <B>msg.</B>"
  306. <P>
  307. char * G_get_ask_return_msg() get Hit RETURN msg
  308. The current <I>msg</I> (as set by <I>G_set_ask_return_msg()</I>) is
  309. returned.
  310. \subsection Fully_Qualified_File_Names Fully Qualified File Names
  311. All GRASS routines which access database files must be given both the
  312. file name and the mapset where the file resides. Often the name and
  313. the mapset are 2 distinct character strings. However, there is a need
  314. for a single character string which contains both the name and the
  315. mapset (e.g., for interactive interfacing to command-line
  316. programs). This form of the name is known as the <I>fully qualified
  317. file name</I> and is built by the following routine:
  318. <P>
  319. char * G_fully_qualified_name() fully
  320. qualified file name
  321. Returns a fully qualified name for the file <B>name</B> in
  322. <B>mapset.</B> Currently this string is in the form
  323. <I>name@mapset</I>, but the programmer should pretend not to know this
  324. and always call this routine to get the fully qualified name.
  325. <P>
  326. The following example shows how an interactive version of <I>d.rast</I>
  327. interfaces with the command-line version of <I>d.rast</I>:
  328. \verbatim
  329. #include <grass/gis.h>
  330. int main(char *argc, char **argv)
  331. {
  332. char name[GNAME_MAX], *mapset, *fqn;
  333. char command[1024];
  334. G_gisinit(argv[0]);
  335. mapset = G_ask_cell_old("", name, "");
  336. if (mapset == NULL) exit(EXIT_SUCCESS);
  337. fqn = G_fully_qualified_name(name, mapset);
  338. sprintf(command, "d.rast map='%s'", fqn);
  339. system(command);
  340. }
  341. \endverbatim
  342. \subsection finding Finding Files in the Database
  343. Noninteractive modules cannot make use of the interactive prompting
  344. routines described above. For example, a command line driven module
  345. may require a database file name as one of the command arguments. In
  346. this case, the programmer must search the database to find the mapset
  347. where the file resides.
  348. <P>
  349. The following routines search the database for files:
  350. <P>
  351. char * G_find_file() find a
  352. database file
  353. Look for the file <B>name</B> under the specified <B>element</B> in
  354. the database. The <B>mapset</B> parameter can either be the empty
  355. string "", which means search all the mapsets in the user's current
  356. mapset search path, or it can be a specific mapset, which means
  357. <I>.</I> look for the file only in this one mapset (for example, in
  358. the current mapset).
  359. <P>
  360. If found, the mapset where the file lives is returned. If not found,
  361. the NULL pointer is returned.
  362. <P>
  363. If the user specifies a fully qualified file name, (i.e, a name that also
  364. contains the mapset; see \ref Fully_Qualified_File_Names) then
  365. <I>G_find_file()</I> modifies <B>name</B> by eliminating the mapset
  366. from the <B>name</B>
  367. <P>
  368. For example, to find a "paint/labels" file anywhere in the database:
  369. \verbatim
  370. char name[GNAME_MAX];
  371. char *mapset;
  372. if ((mapset = G_find_file("paint/labels",name,"")) == NULL)
  373. /* not found */
  374. \endverbatim
  375. <P>
  376. To check that the file exists in the current mapset:
  377. \verbatim
  378. char name[GNAME_MAX];
  379. if (G_find_file("paint/labels",name,G_mapset()) == NULL)
  380. /* not found */
  381. \endverbatim
  382. <P>
  383. \subsection Legal_File_Names Legal File Names
  384. <P>
  385. Not all names that a user may enter will be legal files for the GRASS
  386. databases. The routines which create new files require that the new
  387. file have a legal name. The routines which prompt the user for file
  388. names (e.g., <I>G_ask_new()</I>) guarantee that the name entered by
  389. the user will be legal. If the name is obtained from the command
  390. line, for example, the programmer must check that the name is
  391. legal. The following routine checks for legal file names:
  392. <P>
  393. int G_legal_filename() check for legal database file
  394. names
  395. Returns 1 if <B>name</B> is ok, -1 otherwise.
  396. <P>
  397. \subsection Opening_an_Existing_Database_File_for_Reading Opening an Existing Database File for Reading
  398. <P>
  399. The following routines open the file <B>name</B> in <B>mapset</B> from
  400. the specified database <B>element</B> for reading (but not for
  401. writing). The file <B>name</B> and <B>mapset</B> can be obtained
  402. interactively <I>using G_ask_old(), and noninteractively using
  403. G_find_file().</I>
  404. <P>
  405. int G_open_old() open a database file for reading
  406. The database file <B>name</B> under the
  407. <B>element</B> in the specified <B>mapset</B> is opened for reading (but
  408. not for writing).
  409. <P>
  410. The UNIX open() routine is used to open the file. If the file does not exist,
  411. -1 is returned. Otherwise the file descriptor from the open() is returned.
  412. <P>
  413. FILE * G_fopen_old() open a database file for reading
  414. The database file <B>name</B> under the
  415. <B>element</B> in the specified <B>mapset</B> is opened for reading (but
  416. not for writing).
  417. <P>
  418. The UNIX fopen() routine, with "r" read mode, is used to open the
  419. file. If the file does not exist, the NULL pointer is
  420. returned. Otherwise the file descriptor from the fopen() is returned.
  421. <P>
  422. \subsection Opening_an_Existing_Database_File_for_Update Opening an Existing Database File for Update
  423. <P>
  424. The following routines open the file <B>name</B> in the current mapset
  425. from the specified database <B>element</B> for writing. The file must
  426. exist. Its <B>name</B> can be obtained interactively <I>using
  427. G_ask_in_mapset(), and noninteractively using G_find_file().</I>
  428. <P>
  429. int G_open_update() open a database file for update
  430. The database file <B>name</B> under the <B>element</B> in the
  431. current mapset is opened for reading and writing.
  432. <P>
  433. The UNIX open() routine is used to open the file. If the file does not exist,
  434. -1 is returned. Otherwise the file is positioned at the end of the file and
  435. the file descriptor from the open() is returned.
  436. <P>
  437. int G_fopen_append() open a database file for update
  438. The database file <B>name</B> under the <B>element</B> in the current
  439. mapset is opened for appending (but not for reading).
  440. <P>
  441. The UNIX fopen() routine, with "a" append mode, is used to open the
  442. file. If the file does not exist, the NULL pointer is
  443. returned. Otherwise the file is positioned at the end of the file and
  444. the file descriptor from the fopen() is returned.
  445. \subsection Creating_and_Opening_a_New_Database_File Creating and Opening a New Database File
  446. <P>
  447. The following routines create the new file <B>name</B> in the current
  448. mapset (GRASS does not allow files to be created outside the current
  449. mapset; see \ref Database_Access_Rules) under the specified database
  450. <B>element</B> and open it for writing. The database <B>element</B> is
  451. created, if it does not already exist.
  452. <P>
  453. The file <B>name</B> should be obtained interactively using
  454. <I>G_ask_new()</I>. If obtained noninteractively (e.g., from the
  455. command line), <I>G_legal_filename()</I> should be called first to
  456. make sure that <B>name</B> is a valid GRASS file name. <B>Warning.</B>
  457. It is not an error for <B>name</B> to already exist. However, the file
  458. will be removed and recreated empty. The interactive routine
  459. <I>G_ask_new()</I> guarantees that <B>name</B> will not exist, but if
  460. <B>name</B> is obtained from the command line, <B>name</B> may
  461. exist. In this case <I>G_find_file()</I> could be used to see if
  462. <B>name</B> exists.
  463. <P>
  464. int G_open_new() open a new database file
  465. The database file <B>name</B> under the <B>element</B> in the current
  466. mapset is created and opened for writing (but not reading).
  467. <P>
  468. The UNIX open() routine is used to open the file. If the file does not
  469. exist, -1 is returned. Otherwise the file is positioned at the end of
  470. the file and the file descriptor from the open() is returned.
  471. <P>
  472. FILE * G_fopen_new() open a new database file
  473. The database file <B>name</B> under the <B>element</B> in the current
  474. mapset is created and opened for writing (but not reading).
  475. <P>
  476. The UNIX fopen() routine, with "w" write mode, is used to open the
  477. file. If the file does not exist, the NULL pointer is
  478. returned. Otherwise the file is positioned at the end of the file and
  479. the file descriptor from the fopen() is returned.
  480. <P>
  481. \subsection Database_File_Management Database File Management
  482. <P>
  483. The following routines allow the renaming and removal of database
  484. files in the current mapset (These functions only apply to the current
  485. mapset since GRASS does permit users to modify things in mapsets other
  486. than the current mapset; see \ref Database_Access_Rules).
  487. int G_rename() rename a database
  488. file
  489. The file or directory <B>old</B> under the database <B>element</B>
  490. directory in the current mapset is renamed to <B>new.</B>
  491. <P>
  492. Returns 1 if successful, 0 if <B>old</B> does not exist, and -1 if
  493. there was an error.
  494. <P>
  495. <B>Bug.</B> This routine does not check to see if the <B>new</B> name is a
  496. valid database file name.
  497. <P>
  498. int G_remove() remove a database file
  499. The file or directory <B>name</B> under the database <B>element</B> directory
  500. in the current mapset is removed.
  501. <P>
  502. Returns 1 if successful, 0 if <B>name</B> does not exist, and -1 if
  503. there was an error.
  504. <P>
  505. <B>Note.</B> If <B>name</B> is a directory, everything within the
  506. directory is removed as well.
  507. <P>
  508. <B>Note.</B> These functions only apply to the specific <B>element</B>
  509. and not to other "related" elements. For example, if <B>element</B> is
  510. "cell", then the specified raster file will be removed (or renamed),
  511. but the other support files, such as "cellhd" or "cats", will not. To
  512. remove these other files as well, specific calls must be made for each
  513. related <B>element.</B>
  514. \subsection Memory_Allocation Memory Allocation
  515. <P>
  516. The following routines provide memory allocation capability. They are
  517. simply calls to the UNIX suite of memory allocation routines malloc(),
  518. realloc() and calloc(), except that if there is not enough memory,
  519. they print a diagnostic message to that effect and then call exit().
  520. <P>
  521. <B>Note.</B> Use the G_free() routine to release memory allocated by these
  522. routines.
  523. <P>
  524. int G_free() free the memory allocated
  525. Free the memory allocated by the GRASS malloc routines.
  526. <P>
  527. void * G_malloc () memory allocation
  528. Allocates a block of memory at least <B>size</B> bytes which is
  529. aligned properly for all data types. A pointer to the aligned block is
  530. returned.
  531. <P>
  532. void * G_realloc() memory allocation
  533. Changes the <B>size</B> of a previously allocated block of memory at
  534. <B>ptr</B> and returns a pointer to the new block of memory. The
  535. <B>size</B> may be larger or smaller than the original size. If the
  536. original block cannot be extended "in place", then a new block is
  537. allocated and the original block copied to the new block.
  538. <P>
  539. <B>Note.</B> If <B>ptr</B> is NULL, then this routine simply allocates
  540. a block of <B>size</B> bytes. This routine works around broken
  541. realloc() routines, which do not handle a NULL <B>ptr.</B>
  542. <P>
  543. void * G_calloc() memory allocation
  544. Allocates a properly aligned block of memory <B>n</B>*<B>size</B>
  545. bytes in length, initializes the allocated memory to zero, and returns
  546. a pointer to the allocated block of memory.
  547. <P>
  548. <B>Note.</B> Allocating memory for reading and writing raster files is
  549. discussed in Allocating_Raster_I_O_Buffers.
  550. <P>
  551. double * G_alloc_vector() memory allocation
  552. Allocate a vector (array) of <B>n</B> doubles initialized to zero.
  553. <P>
  554. float * G_alloc_fvector() memory allocation
  555. Allocate a vector (array) of <B>n</B> floats initialized to zero.
  556. <P>
  557. double ** G_alloc_matrix() memory allocation
  558. Allocate a matrix of <B>rows</B> by <B>cols</B> doubles initialized to
  559. zero.
  560. <P>
  561. float ** G_alloc_fmatrix() memory allocation
  562. Allocate a matrix of <B>rows</B> by <B>cols</B> floats initialized to
  563. zero.
  564. <P>
  565. int G_free_vector() memory deallocation
  566. Deallocate a vector (array) of doubles or floats.
  567. <P>
  568. int G_free_matrix() memory deallocation
  569. Deallocate a matrix of doubles.
  570. <P>
  571. int G_free_fmatrix() memory deallocation
  572. Deallocate a matrix of floats.
  573. \subsection The_Region The Region
  574. The region concept is explained in Region. It can be thought of as a
  575. two-dimensional matrix with known boundaries and rectangular cells.
  576. <P>
  577. There are logically two different regions. The first is the database
  578. region that the user has set in the current mapset. The other is the
  579. region that is active in the module. This active module region is what
  580. controls reading and writing of raster file data. The
  581. vector map export does not take care for the active region settings.
  582. <P>
  583. The routines described below use a GRASS data structure
  584. <I>Cell_head</I> to hold region information. This structure is defined
  585. in the "gis.h" header file. It is discussed in detail under
  586. GIS_Library_Data_Structures .
  587. \subsection The_Database_Region The Database Region
  588. Reading and writing the user's database region are done by the
  589. following routines:
  590. [Note: Previous versions of GRASS called this the "window". Due to
  591. overuse of this term (database window, graphics window, etc.), the
  592. term was changed to "region". However, to maintain compatibility with
  593. existing programs, library routine names were not changed - hence the
  594. term "window" is used in the routine name (where "region" should
  595. probably be used instead.)]
  596. <P>
  597. int G_get_window() read the database region
  598. Reads the database region as stored in the WIND file in the user's
  599. current mapset <B>into region.</B>
  600. <P>
  601. An error message is printed and exit() is called if there is a problem
  602. reading the region.
  603. <P>
  604. <B>Note.</B> GRASS applications that read or write raster files should
  605. not use this routine since its use implies that the active module
  606. region will not be used. Programs that read or write raster file data
  607. (or vector data) can query the active module region <I>using
  608. G_window_rows() and G_window_cols().</I>
  609. <P>
  610. int G_put_window() write the database region
  611. Writes the database region file (WIND) in the user's current mapset
  612. from <B>region.</B> Returns 1 if the region is written ok. Returns -1
  613. if not (no diagnostic message is printed).
  614. <P>
  615. <B>Warning.</B> Since this routine actually changes the database
  616. region, it should only be called by modules which the user knows will
  617. change the region. It is probably fair to say that under GRASS 3.0
  618. only the <I>g.region</I>, and <I>d.zoom</I> modules should call this
  619. routine.
  620. <P>
  621. There is another database region. This region is the default region
  622. for the location. The default region provides the user with a
  623. "starting" region, i.e., a region to begin with and return to as a
  624. reference point. The GRASS modules <I>g.region</I> allow the user to
  625. set their database region from the default region. (See
  626. Permanent_Mapset for a discussion of the default region.) The
  627. following routine reads this region:
  628. <P>
  629. int G_get_default_window() read the default
  630. region
  631. Reads the default region for the location into <B>region.</B>
  632. <P>
  633. An error message is printed and exit() is called if there is a problem
  634. reading the default region.
  635. \subsection The_Active_Module_Region The Active Module Region
  636. <P>
  637. The active module region is the one that is used when reading and
  638. writing raster file data. This region determines the resampling when
  639. reading raster data. It also determines the extent and resolution of
  640. new raster files.
  641. <P>
  642. Initially the active module region and the user's database region are
  643. the same, but the programmer can make them different. The following
  644. routines manage the active module region.
  645. <P>
  646. int G_window_rows() number of rows in active region
  647. <P>
  648. int G_window_cols() number of columns in active region
  649. These routines return the number of rows and columns (respectively) in
  650. the active module region. Before raster files can be read or written,
  651. it is necessary to known how many rows and columns are in the active
  652. region. For example:
  653. \verbatim
  654. int nrows, cols;
  655. int row, col;
  656. nrows = G_window_rows();
  657. ncols = G_window_cols();
  658. for (row = 0; row < nrows; row++){
  659. read row ...
  660. for (col = 0; col < ncols; col++){
  661. process col ...
  662. }
  663. }
  664. \endverbatim
  665. <P>
  666. int G_set_window() set the active region
  667. This routine sets the active region from <B>region.</B> Setting the
  668. active region does not change the WIND file in the database. It simply
  669. changes the region for the duration of the module.
  670. However, the new region setting is not retained across the UNIX exec()
  671. call. This implies that G_set_window() cannot be used to set the
  672. region for a module to be executed using the system() or popen()
  673. routines.
  674. A warning message is printed and -1 returned if <B>region</B> is not
  675. valid. Otherwise 1 is returned.
  676. <P>
  677. <B>Note.</B> This routine overrides the region as set by the user. Its
  678. use should be very limited since it changes what the user normally
  679. expects to happen. If this routine is not called, then the active
  680. region will be the same as what is in the user's WIND file.
  681. <P>
  682. <B>Warning.</B> Calling this routine with already opened raster files
  683. has some side effects. If there are raster files which are open for
  684. reading, they will be read into the newly set region, not the region
  685. that was active when they were opened. However, CELL buffers allocated
  686. for reading the raster files are not automatically reallocated. The
  687. module must reallocate them explicitly. Also, this routine does not
  688. change the region for raster files which are open for writing. The
  689. region that was active when the open occurred still applies to these
  690. files.
  691. <P>
  692. int G_get_set_window() get the active region
  693. Gets the values of the currently active region into <B>region.</B> If
  694. <I>G_set_window()</I> has been called, then the values set by that call
  695. are retrieved. Otherwise the user's database region is retrieved.
  696. <P>
  697. <B>Note.</B> For modules that read or write raster data, and really
  698. need the full region information, this routine is preferred over
  699. <I>G_get_window().</I> However, since <I>G_window_rows() and G_window_cols()
  700. return the number of rows and</I> columns in the active region, the
  701. programmer should consider whether or not the full region information
  702. is really needed before using this routine.
  703. <P>
  704. char * G_align_window() align two regions
  705. Modifies the input <B>region</B> to align to the <B>ref</B>
  706. region. The resolutions in <B>region</B> are set to match those in
  707. <B>ref</B> and the <B>region</B> edges (north, south, east, west) are
  708. modified to align with the grid of the <B>ref</B> region.
  709. <P>
  710. The <B>region</B> may be enlarged if necessary to achieve the
  711. alignment. The north is rounded northward, the south southward, the
  712. east eastward and the west westward.
  713. <P>
  714. This routine returns NULL if ok, otherwise it returns an error message.
  715. <P>
  716. double G_col_to_easting() column to easting
  717. Converts a <B>col</B>umn relative to a <B>region</B> to an easting;
  718. <P>
  719. <B>Note.</B> col is a double: col+0.5 will return the easting for the
  720. center of the column; col+0.0 will return the easting for the western
  721. edge of the column; and col+1.0 will return the easting for the
  722. eastern edge of the column.
  723. <P>
  724. double G_row_to_northing() row to northing
  725. Converts a <B>row</B> relative to a <B>region</B> to a northing;
  726. <P>
  727. <B>Note.</B> row is a double: row+0.5 will return the northing for the
  728. center of the row; row+0.0 will return the northing for the northern
  729. edge of the row; and row+1.0 will return the northing for the southern
  730. edge of the row.
  731. double G_easting_to_col() easting to column
  732. <P>
  733. Converts an <B>east</B>ing relative to a <B>region</B> to a column.
  734. <P>
  735. <B>Note.</B> The result is a double. Casting it to an integer will
  736. give the column number.
  737. <P>
  738. double G_northing_to_row() northing to row
  739. Converts a <B>north</B>ing relative to a <B>region</B> to a row.
  740. <P>
  741. <B>Note.</B> the result is a double. Casting it to an integer will
  742. give the row number.
  743. \subsection Projection_Information Projection Information
  744. <P>
  745. The following routines return information about the cartographic
  746. projection and zone. See \ref Region for more information about these
  747. values.
  748. <P>
  749. int G_projection() query cartographic projection
  750. This routine returns a code indicating the projection for the active
  751. region. The current values are:
  752. <P>
  753. 0 unreferenced x,y (imagery data)
  754. <br>
  755. 1 UTM
  756. <br>
  757. 2 State Plane
  758. <br>
  759. 3 Latitude-Longitude
  760. <br>
  761. 99 Other (more than 121 projections are supported)
  762. <P>
  763. char * G_database_projection_name() query cartographic projection
  764. Returns a pointer to a string which is a printable name for projection
  765. code <B>proj</B> (as returned by <I>G_projection()</I>). Returns NULL if
  766. <B>proj</B> is not a valid projection.
  767. <P>
  768. char * G_database_unit_name() database units
  769. Returns a string describing the database grid units. It returns a
  770. plural form (eg. feet) if <B>plural</B> is true. Otherwise it returns
  771. a singular form (eg. foot).
  772. <P>
  773. double G_database_units_to_meters_factor() conversion to
  774. meters
  775. Returns a factor which converts the grid unit to meters (by
  776. multiplication). If the database is not metric (eg. imagery) then 0.0
  777. is returned.
  778. <P>
  779. int G_zone() query cartographic zone
  780. This routine returns the zone for the active region. The meaning for
  781. the zone depends on the projection. For example zone 18 for projection
  782. type 1 would be UTM zone 18.
  783. \subsection Latitude_Longitude_Databases Latitude-Longitude Databases
  784. <P>
  785. GRASS supports databases in a longitude-latitude grid using a
  786. projection where the x coordinate is the longitude and the y
  787. coordinate is the latitude. This projection is called the Equidistant
  788. Cylindrical Projection (also known as Plate Carree). ECP has the
  789. property that <I>where am I</I> and <I>row-column</I> calculations are
  790. identical to those in planimetric grids (like UTM, Universal
  791. Transverse Mercator Projection). This implies that normal GRASS
  792. registration and overlay functions will work without any special
  793. considerations or modifications to existing code. However, the
  794. projection is not planimetric. This means that distance and area
  795. calculations are no longed Euclidean.
  796. <P>
  797. Also, since the world is round, maps may not have edges in the
  798. east-west direction, especially for global databases. Maps may have
  799. the same longitude at both the east and west edges of the
  800. display. This feature, called global wraparound, must be accounted for
  801. by GRASS modules (particularly vector based functions, like plotting.)
  802. What follows is a description of the GISLIB library routines that are
  803. available to support latitude-longitude databases.
  804. \subsection Coordinates Coordinates
  805. <P>
  806. Latitudes and longitudes are specified in degrees. Northern latitudes
  807. range from 0 to 90 degrees, and southern latitudes from 0 to
  808. -90. Longitudes have no limits since longitudes ±360 degrees are
  809. equivalent.
  810. <P>
  811. Coordinates are represented in ASCII using the format <B>dd:mm:ssN</B>
  812. or <B>dd:mm:ssS</B> for latitudes, <B>ddd:mm:ssE</B> or
  813. <B>ddd.mm.ssW</B> for longitudes, and <B>dd.mm.ss</B> for grid
  814. resolution. For example, 80:30:24N represents a northern latitude of
  815. 80 degrees, 30 minutes, and 24 seconds. 120:15W represents a
  816. longitude 120 degrees and 15 minutes west of the prime meridian. 30:15
  817. represents a resolution of 30 degrees and 15 minutes. These next
  818. routines convert between ASCII representations and the machine
  819. representation for a coordinate. They work both with
  820. latitude-longitude projections and planimetric projections.
  821. <P>
  822. <B>Note.</B> In each subroutine, the programmer must specify the
  823. projection number. If the projection number is PROJECTION_LL (defined
  824. in "gis.h"), then latitude-longitude ASCII format is invoked.
  825. Otherwise, a standard floating-point to ASCII conversion is made.
  826. <P>
  827. int G_format_easting() easting to ASCII
  828. Converts the double representation of the <B>east</B> coordinate to
  829. its ASCII representation (into <B>buf</B>).
  830. <P>
  831. int G_format_northing() northing to ASCII
  832. Converts the double representation of the <B>north</B> coordinate to
  833. its ASCII representation (into <B>buf</B>).
  834. <P>
  835. int G_format_resolution() resolution to ASCII
  836. Converts the double representation of the <B>resolution</B> to its
  837. ASCII representation (into <B>buf</B>).
  838. <P>
  839. int G_scan_easting() ASCII easting to double
  840. Converts the ASCII "easting" coordinate string in <B>buf</B> to its
  841. double representation (into <B>easting</B>).
  842. <P>
  843. int G_scan_northing() ASCII northing to double
  844. Converts the ASCII "northing" coordinate string in <B>buf</B> to its
  845. double representation (into <B>northing</B>).
  846. <P>
  847. int G_scan_resolution() ASCII resolution to double
  848. Converts the ASCII "resolution" string in <B>buf</B> to its double
  849. representation (into resolution).
  850. <P>
  851. The following are examples of how these routines are used.
  852. \verbatim
  853. double north;
  854. char buf[50];
  855. G_scan_northing(buf, north, G_projection()); /* ASCII to double */
  856. G_format_northing(north, buf, G_projection()); /* double to ASCII */
  857. G_format_northing(north, buf, -1); /* double to ASCII */
  858. /* This last example forces floating-point ASCII format */
  859. \endverbatim
  860. \subsection Raster_Area_Calculations Raster Area Calculations
  861. The following routines perform area calculations for raster maps.,
  862. They are based on the fact that while the latitude-longitude grid is
  863. not planimetric, the size of the grid cell at a given latitude is
  864. constant. The first routines work in any projection.
  865. <P>
  866. int G_begin_cell_area_calculations() begin cell area calculations
  867. This routine must be called once before any call to
  868. <I>G_area_of_cell_at_row().</I> It can be used in either planimetric
  869. projections or the latitude-longitude projection. It returns 2 if the
  870. projection is latitude-longitude, 1 if the projection is planimetric,
  871. and 0 of the projection doesn't hav e a metric (e.g. imagery.) If the
  872. return value is 1 or 0, all the grid cells in the map have the same
  873. area. Otherwise the area of a grid cell varies with the row.
  874. <P>
  875. double G_area_of_cell_at_row() cell area in specified row
  876. This routine returns the area in square meters of a cell in the
  877. specified <B>row.</B> This value is constant for planimetric grids and
  878. varies with the row if the projection is latitude-longitude.
  879. <P>
  880. int G_begin_zone_area_on_ellipsoid() begin area calculations for ellipsoid
  881. Initializes raster area calculations for an ellipsoid, where <B>a</B>
  882. is the semi-major axis of the ellipse (in meters), <B>e2</B> is the
  883. ellipsoid eccentricity squared, and <B>s</B> is a scale factor to
  884. allow for calculations of part of the zone (<B>s</B>=1.0 is full zone,
  885. <B>s</B>=0.5 is half the zone, and <B>s</B>=360/ew_res is for a single
  886. grid cell).
  887. <P>
  888. <B>Note.</B> e2 must be positive. A negative value makes no sense, and
  889. zero implies a sphere.
  890. <P>
  891. double G_area_for_zone_on_ellipsoid() area between latitudes
  892. Returns the area between latitudes <B>north</B> and <B>south</B>
  893. scaled by the factor <B>s</B> passed to
  894. <I>G_begin_zone_area_on_ellipsoid().</I>
  895. <P>
  896. int G_begin_zone_area_on_sphere() initialize
  897. calculations for sphere
  898. Initializes raster area calculations for a sphere. The radius of the
  899. sphere is <B>r</B> and <B>s</B> is a scale factor to allow for
  900. calculations of a part of the zone (see
  901. <I>G_begin_zone_area_on_ellipsoid()</I>).
  902. <P>
  903. double G_area_for_zone_on_sphere() area between latitudes
  904. Returns the area between latitudes <B>north</B> and <B>south</B>
  905. scaled by the factor <B>s</B> passed to
  906. <I>G_begin_zone_area_on_sphere()</I>
  907. \subsection Polygonal_Area_Calculations Polygonal Area Calculations
  908. <P>
  909. These next routines provide area calculations for polygons. Some of
  910. the routines are specifically for latitude-longitude, while others
  911. will function for all projections.
  912. <P>
  913. However, there is an issue for latitude-longitude that does not occur
  914. with planimetric grids. Vector/polygon data is described as a series
  915. of x,y coordinates. The lines connecting the points are not stored but
  916. are inferred. This is a simple, straight-forward process for
  917. planimetric grids, but it is not simple for latitude-longitude. What
  918. is the shape of the line that connects two points on the surface of a
  919. globe?
  920. <P>
  921. One choice (among many) is the shortest path from <B>x1,y1</B> to
  922. <B>x2,y2</B>, known as the geodesic. Another is a straight line on the
  923. grid. The area routines described below assume the latter. Routines
  924. to work with the former have not yet been developed.
  925. <P>
  926. int G_begin_polygon_area_calculations() begin polygon area
  927. calculations
  928. This initializes the polygon area calculation routines. It is used
  929. both for planimetric and latitude-longitude projections.
  930. <P>
  931. It returns 2 if the projection is latitude-longitude, 1 if the
  932. projection is planimetric, and 0 if the projection doesn't have a
  933. metric (e.g. imagery.)
  934. <P>
  935. double G_area_of_polygon() area in square meters of polygon
  936. Returns the area in square meters of the polygon described by the
  937. <B>n</B> pairs of <B>x,y</B> coordinate vertices. It is used both for
  938. planimetric and latitude-longitude projections.
  939. <P>
  940. <B>Note.</B> If the database is planimetric with the non-meter grid,
  941. this routine performs the required unit conversion to produce square
  942. meters. double <B>G_planimetric_polygon_area()</B> (x, y, n) <I>area in
  943. coordinate units</I> double *x, *y ; int n ;
  944. <P>
  945. Returns the area in coordinate units of the polygon described by the
  946. <B>n</B> pairs of <B>x,y</B> coordinate vertices for planimetric
  947. grids. If the units for <B>x,y</B> are meters, then the area is in
  948. square meters. If the units are feet, then the area is in square
  949. feet, and so on.
  950. <P>
  951. int G_begin_ellipsoid_polygon_area () begin area calculations
  952. This initializes the polygon area calculations for the ellipsoid with
  953. semi-major axis <B>a</B> (in meters) and ellipsoid eccentricity
  954. squared <B>e2.</B>
  955. <P>
  956. double G_ellipsoid_polygon_area() area of lat-long polygon
  957. Returns the area in square meters of the polygon described by the
  958. <B>n</B> pairs of <B>lat,long</B> vertices for latitude-longitude
  959. grids.
  960. <P>
  961. <B>Note.</B> This routine assumes grid lines on the connecting the
  962. vertices (as opposed to geodesics).
  963. \subsection Distance Calculations Distance Calculations
  964. Two routines perform distance calculations for any projection.
  965. <P>
  966. int G_begin_distance_calculations() begin distance calculations
  967. Initializes the distance calculations. It is used both for the
  968. planimetric and latitude-longitude projections.
  969. <P>
  970. It returns 2 if the projection is latitude-longitude, 1 if the
  971. projection is planimetric, and 0 if the projection doesn't hav e a
  972. metric (e.g. imagery).
  973. <P>
  974. double G_distance() distance in meters
  975. This routine computes the distance, in meters, from <B>x1,y1</B> to
  976. <B>x2,y2.</B> If the projection is latitude-longitude, this distance
  977. is measured along the geodesic. Two routines perform geodesic distance
  978. calculations.
  979. <P>
  980. int G_begin_geodesic_distance() begin geodesic distance
  981. Initializes the distance calculations for the ellipsoid with
  982. semi-major axis <B>a</B> (in meters) and ellipsoid eccentricity
  983. squared <B>e2.</B> It is used only for the latitude-longitude
  984. projection.
  985. <P>
  986. double G_geodesic_distance() geodesic distance
  987. Calculates the geodesic distance from <B>lon1,lat1</B> to
  988. <B>lon2,lat2</B> in meters.
  989. <P>
  990. The calculation of the geodesic distance is fairly costly. These next
  991. three routines provide a mechanism for calculating distance with two
  992. fixed latitudes and varying longitude separation.
  993. <P>
  994. int G_set_geodesic_distance_lat1() set geodesic distance lat1
  995. Set the first latitude.
  996. <P>
  997. int G_set_geodesic_distance_lat2() set geodesic distance lat2
  998. Set the second latitude.
  999. <P>
  1000. double G_geodesic_distance_lon_to_lon() geodesic distance
  1001. Calculates the geodesic distance from <B>lon1,lat1</B> to
  1002. <B>lon2,lat2</B> in meters, where <B>lat1</B> was the latitude passed
  1003. to <I>G_set_geodesic_distance_latl()</I> and <B>lat2</B> was the
  1004. <I>latitude passed to G_set_geodesic_distance_lat2().</I>
  1005. \subsection Global_Wraparound Global Wraparound
  1006. <P>
  1007. These next routines provide a mechanism for determining the relative
  1008. position of a pair of longitudes. Since longitudes of ±360 are
  1009. equivalent, but GRASS requires the east to be bigger than the west,
  1010. some adjustment of coordinates is necessary.
  1011. <P>
  1012. double G_adjust_easting() returns east larger than west
  1013. If the region projection is PROJECTION_LL, then this routine returns
  1014. an equivalent <B>east</B> that is larger, but no more than 360 degrees
  1015. larger, than the coordinate for the western edge of the
  1016. region. Otherwise no adjustment is made and the original <B>east</B>
  1017. is returned.
  1018. <P>
  1019. double G_adjust_east_longitude() adjust east
  1020. longitude
  1021. This routine returns an equivalent <B>east</B> that is larger, but no
  1022. more than 360 larger than the <B>west</B> coordinate.
  1023. <P>
  1024. This routine should be used only with latitude-longitude coordinates.
  1025. <P>
  1026. int G_shortest_way() shortest way between eastings
  1027. If the database projection is PROJECTION_LL, then <B>east1,east2</B>
  1028. are changed so that they are no more than 180 degrees apart. Their
  1029. true locations are not changed. If the database projection is not
  1030. PROJECTION_LL, then <B>east1,east2</B> are not changed.
  1031. \subsection Miscellaneous Miscellaneous
  1032. char * G_ellipsoid_name() return ellipsoid name
  1033. This routine returns a pointer to a string containing the name for the
  1034. <B>n</B><I>th</I> ellipsoid in the GRASS ellipsoid table; NULL when
  1035. <B>n</B> is too large. It can be used as follows:
  1036. \verbatim
  1037. int n;
  1038. char *name;
  1039. for (n=0; name=G_ellipsoid_name(n); n++)
  1040. fprintf(stdout, "%s\n", name);
  1041. \endverbatim
  1042. <P>
  1043. int G_get_ellipsoid_by_name() get ellipsoid by name
  1044. This routine returns the semi-major axis <B>a</B> (in meters) and
  1045. eccentricity squared <B>e2</B> for the named ellipsoid. Returns 1 if
  1046. <B>name</B> is a known ellipsoid, 0 otherwise.
  1047. <P>
  1048. int G_get_ellipsoid_parameters() get ellipsoid parameters
  1049. This routine returns the semi-major axis <B>a</B> (in meters) and the
  1050. eccentricity squared <B>e2</B> for the ellipsoid associated with the
  1051. database. If there is no ellipsoid explicitly associated with the
  1052. database, it returns the values for the WGS 84 ellipsoid.
  1053. <P>
  1054. double G_meridional_radius_of_curvature() meridional radius of curvature
  1055. Returns the meridional radius of curvature at a given longitude:
  1056. \f$
  1057. \rho = \frac{a (1-e^2)}{(1-e^2\sin^2 lon)^{3/2}}
  1058. \f$
  1059. <P>
  1060. double G_transverse_radius_of_curvature() transverse radius of curvature
  1061. Returns the transverse radius of curvature at a given longitude:
  1062. \f$
  1063. \nu = \frac{a}{(1-e^2\sin^2 lon)^{1/2}}
  1064. \f$
  1065. <P>
  1066. double G_radius_of_conformal_tangent_sphere() radius of conformal tangent sphere
  1067. Returns the radius of the conformal sphere tangent to ellipsoid at a
  1068. given longitude:
  1069. \f$
  1070. r = \frac{a (1-e^2)^{1/2}}{(1-e^2\sin^2 lon)}
  1071. \f$
  1072. <P>
  1073. int G_pole_in_polygon() pole in polygon
  1074. For latitude-longitude coordinates, this routine determines if the
  1075. polygon defined by the <B>n</B> coordinate vertices <B>x,y</B>
  1076. contains one of the poles.
  1077. <P>
  1078. Returns -1 if it contains the south pole; 1 if it contains the north
  1079. pole; 0 if it contains neither pole.
  1080. <P>
  1081. <B>Note.</B> Use this routine only if the projection is PROJECTION_LL.
  1082. <P>
  1083. \section Raster_File_Processing GRASS Raster File Processing
  1084. Please refer to GRASS Raster File Processing in Chapter \ref gisrastintro .
  1085. \section Vector_File_Processing GRASS Vector File Processing
  1086. Please refer to GRASS Vector File Processing in Chapter \ref gisvectintro .
  1087. Note, that the old "sites" are stored as vector points since GRASS 6.
  1088. \section General_Plotting_Routines General Plotting Routines
  1089. The following routines form the foundation of a general purpose line
  1090. and polygon plotting capability.
  1091. <P>
  1092. int G_bresenham_line() Bresenham line algorithm
  1093. Draws a line from <B>x1,y1</B> to <B>x2,y2</B> using Bresenham's
  1094. algorithm. A routine to plot points must be provided, as is defined
  1095. as:
  1096. <P>
  1097. point(x, y) plot a point at x,y
  1098. <P>
  1099. This routine does not require a previous call to <I>G_setup_plot()</I> to
  1100. function correctly, and is independent of all following routines.
  1101. <P>
  1102. int G_setup_plot() initialize plotting routines
  1103. Initializes the plotting capability. This routine must be called once
  1104. before calling the <B>G_plot_*()</B> routines described below.
  1105. <P>
  1106. The parameters <B>t, b, l, r</B> are the top, bottom, left, and right
  1107. of the output x,y coordinate space. They are not integers, but doubles
  1108. to allow for subpixel registration of the input and output coordinate
  1109. spaces. The input coordinate space is assumed to be the current GRASS
  1110. region, and the routines supports both planimetric and
  1111. latitude-longitude coordinate systems.
  1112. <P>
  1113. <B>Move</B> and <B>Cont</B> are subroutines that will draw lines in
  1114. x,y space. They will be called as follows:
  1115. <P>
  1116. Move(x, y) move to x,y (no draw)
  1117. <P>
  1118. Cont(x, y) draw from previous position
  1119. <P>
  1120. to x,y. Cont() is responsible for clipping
  1121. <P>
  1122. int G_plot_line() plot line between latlon coordinates
  1123. A line from <B>east1,north1</B> to <B>east2,north2</B> is plotted in
  1124. output x,y coordinates (e.g. pixels for graphics.) This routine
  1125. handles global wrap-around for latitude-longitude databases.
  1126. <P>
  1127. See <B>G_setup_plot()</B> for the required coordinate initialization
  1128. procedure.
  1129. <P>
  1130. int G_plot_polygon() plot filled
  1131. polygon with n vertices
  1132. The polygon, described by the <B>n</B> vertices <B>east,north</B>, is
  1133. plotted in the output x,y space as a filled polygon.
  1134. <P>
  1135. See <B>G_setup_plot()</B> for the required coordinate initialization
  1136. procedure.
  1137. <P>
  1138. int G_plot_area() plot multiple polygons
  1139. Like G_plot_polygon(), except it takes a set of polygons, each with
  1140. <B>npts[<I>i</I>]</B> vertices, where the number of polygons is
  1141. specified with the <B>rings</B> argument. It is especially useful for
  1142. plotting vector areas with interior islands.
  1143. <P>
  1144. int G_plot_where_en() x,y to east,north
  1145. The pixel coordinates <B>x,y</B> are converted to map coordinates
  1146. <B>east,north.</B>
  1147. <P>
  1148. See <B>G_setup_plot()</B> for the required coordinate initialization
  1149. procedure.
  1150. <P>
  1151. int G_plot_where_xy() east,north to x,y
  1152. The map coordinates <B>east,north</B> are converted to pixel
  1153. coordinates <B>x,y.</B>
  1154. <P>
  1155. See <B>G_setup_plot()</B> for the required coordinate initialization
  1156. procedure.
  1157. <P>
  1158. int G_plot_fx() plot f(east1) to f(east2)
  1159. The function <B>f(east)</B> is plotted from <B>east1</B> to
  1160. <B>east2.</B> The function <B>f(east)</B> must return the map northing
  1161. coordinate associated with east.
  1162. <P>
  1163. See <B>G_setup_plot()</B> for the required coordinate initialization
  1164. procedure.
  1165. \section Temporary_Files Temporary Files
  1166. Often it is necessary for modules to use temporary files to store
  1167. information that is only useful during the module run. After the
  1168. module finishes, the information in the temporary file is no longer
  1169. needed and the file is removed. Commonly it is required that
  1170. temporary file names be unique from invocation to invocation of the
  1171. module. It would not be good for a fixed name like "/tmp/mytempfile"
  1172. to be used. If the module were run by two users at the same time, they
  1173. would use the same temporary file. In addition systematic use of the
  1174. /tmp directory could leave the system vulnerable to symlink attacks.
  1175. The following routine generates temporary file names which are unique
  1176. within the module and across all GRASS programs.
  1177. <P>
  1178. char * G_tempfile() returns a temporary file name
  1179. This routine returns a pointer to a string containing a unique file
  1180. name that can be used as a temporary file within the
  1181. module. Successive calls to G_tempfile() will generate new names.
  1182. <P>
  1183. Only the file name is generated. The file itself is not created. To
  1184. create the file, the module must use standard UNIX functions which
  1185. create and open files, e.g., creat() or fopen().
  1186. <P>
  1187. The programmer should take reasonable care to remove (unlink) the file
  1188. before the module exits. However, GRASS database management will
  1189. eventually remove all temporary files created by G_tempfile() that
  1190. have been left behind by the modules which created them.
  1191. <P>
  1192. <B>Note.</B> The temporary files are created in the GRASS database
  1193. rather than under /tmp. This is done for two reasons. The first is to
  1194. increase the likelihood that enough disk is available for large
  1195. temporary files since /tmp may be a very small file system. The second
  1196. is so that abandoned temporary files can be automatically removed (but
  1197. see the warning below).
  1198. <P>
  1199. <B>Warning.</B> The temporary files are named, in part, using the
  1200. process id of the module. GRASS database management will remove these
  1201. files only if the module which created them is no longer
  1202. running. However, this feature has a subtle trap. Programs which
  1203. create child processes (using the UNIX fork(), see also G_fork()
  1204. routine) should let the child call G_tempfile(). If the parent does it
  1205. and then exits, the child may find that GRASS has removed the
  1206. temporary file since the process which created it is no longer
  1207. running.
  1208. \section Command_Line_Parsing Command Line Parsing
  1209. The following routines provide a standard mechanism for command line
  1210. parsing. Use of the provided set of routines will standardize GRASS
  1211. commands that expect command line arguments, creating a family of
  1212. GRASS modules that is easy for users to learn. As soon as a GRASS user
  1213. familiarizes himself with the general form of command line input as
  1214. defined by the parser, it will greatly simplify the necessity of
  1215. remembering or at least guessing the required command line arguments
  1216. for any GRASS command. It is strongly recommended that GRASS
  1217. programmers use this set of routines for all command line
  1218. parsing. With their use, the programmer is freed from the burden of
  1219. generating user interface code for every command. The parser will
  1220. limit the programmer to a pre-defined look and feel, but limiting the
  1221. interface is well worth the shortened user learning curve.
  1222. \subsection Description Description
  1223. The GRASS parser is a collection of five subroutines which use two
  1224. structures that are defined in the GRASS "gis.h" header file. These
  1225. structures allow the programmer to define the options and flags that
  1226. make up the valid command line input of a GRASS command.
  1227. <P>
  1228. The parser routines behave in one of three ways:
  1229. <P>
  1230. (1) If no command line arguments are entered by the user, the parser
  1231. searches for a completely interactive version of the command. If the
  1232. interactive version is found, control is passed over to this
  1233. version. If not, the parser will prompt the user for all
  1234. programmer-defined options and flags. This prompting conforms to the
  1235. same standard for every GRASS command that uses the parser routines.
  1236. <P>
  1237. (2) If command line arguments are entered but they are a subset of the
  1238. options and flags that the programmer has defined as required
  1239. arguments, three things happen. The parser will pass an error message
  1240. to the user indicating which required options and/or flags were
  1241. missing from the command line, the parser will then display a complete
  1242. usage message for that command, and finally the parser cancels
  1243. execution of the command.
  1244. <P>
  1245. (3) If all necessary options and flags are entered on the command line
  1246. by the user, the parser executes the command with the given options
  1247. and flags.
  1248. \subsection Structures Structures
  1249. <P>
  1250. The parser routines described below use two structures as defined in
  1251. the GRASS "gis.h" header file.
  1252. <P>
  1253. This is a basic list of members of the Option and Flag structures. A
  1254. comprehensive description of all elements of these two structures and
  1255. their possible values can be found in
  1256. Full_Structure_Members_Description.
  1257. \subsection Option_structure Option structure
  1258. These are the basic members of the Option structure.
  1259. \verbatim
  1260. struct Option *opt; /* to declare a command line option */
  1261. \endverbatim
  1262. <P>
  1263. <B>Structure Member Description of Member</B>
  1264. \verbatim
  1265. opt->key Option name that user will use
  1266. opt->description Option description that is shown to the user
  1267. opt->type Variable type of the user's answer to the option
  1268. opt->required Is this option required on the command line? (Boolean)
  1269. \endverbatim
  1270. \subsection Flag_structure Flag structure
  1271. <P>
  1272. These are the basic members of the Flag structure.
  1273. \verbatim
  1274. struct Flag *flag; /* to declare a command line flag */
  1275. \endverbatim
  1276. <B>Structure Member Description of Member</B>
  1277. \verbatim
  1278. flag->key Single letter used for flag name
  1279. flag->description Flag description that is shown to the user
  1280. \endverbatim
  1281. \subsection Parser_Routines Parser Routines
  1282. <P>
  1283. Associated with the parser are five routines that are automatically
  1284. included in the GRASS Makefile process. The Makefile process is
  1285. documented in \ref Compiling_and_Installing_GRASS_Modules.
  1286. <P>
  1287. struct Option * G_define_option() returns Option structure
  1288. Allocates memory for the Option structure and returns a pointer to
  1289. this memory (of <I>type struct Option *).</I>
  1290. <P>
  1291. struct Flag * G_define_flag() return Flag structure
  1292. Allocates memory for the Flag structure and returns a pointer to this
  1293. memory (of <I>type struct Flag *).</I>
  1294. <P>
  1295. int G_parser() parse command line
  1296. The command line parameters <B>argv</B> and the number of parameters
  1297. <B>argc</B> from the main() routine are passed directly to
  1298. <I>G_parser()</I>. <I>G_parser()</I> accepts the command line input
  1299. entered by the user, and parses this input according to the input
  1300. options and/or flags that were defined by the programmer.
  1301. <P>
  1302. <I>G_parser()</I> returns 0 if successful. If not successful, a usage
  1303. statement is displayed that describes the expected and/or required
  1304. options and flags and a non-zero value is returned.
  1305. <P>
  1306. int G_usage() command line help/usage message
  1307. Calls to <I>G_usage()</I> allow the programmer to print the usage
  1308. message at any time. This will explain the allowed and required
  1309. command line input to the user. This description is given according to
  1310. the programmer's definitions for options and flags. This function
  1311. becomes useful when the user enters options and/or flags on the
  1312. command line that are syntactically valid to the parser, but
  1313. functionally invalid for the command (e.g. an invalid file name.)
  1314. <P>
  1315. For example, the parser logic doesn't directly support grouping
  1316. options. If two options be specified together or not at all, the
  1317. parser must be told that these options are not required and the
  1318. programmer must check that if one is specified the other must be as
  1319. well. If this additional check fails, then <I>G_parser()</I> will
  1320. succeed, but the programmer can then call <I>G_usage()</I> to print
  1321. the standard usage message and print additional information about how
  1322. the two options work together.
  1323. <P>
  1324. int G_disable_interactive() turns off interactive capability
  1325. When a user calls a command with no arguments on the command line, the
  1326. parser will enter its own standardized interactive session in which
  1327. all flags and options are presented to the user for input. A call to
  1328. <I>G_disable_interactive()</I> disables the parser's interactive
  1329. prompting.
  1330. <P>
  1331. <B>Note:</B> Displaying multiple answers default values (new in GRASS
  1332. 5, see d.zoom for example).
  1333. \verbatim
  1334. char *def[] = {"One", "Two", "Last", NULL};
  1335. opt->multiple = YES;
  1336. opt->answers = def;
  1337. if (G_parser(argc, argv))
  1338. exit(EXIT_FAILURE);
  1339. \endverbatim
  1340. <P>
  1341. The programmer may not forget last NULL value.
  1342. \subsection Parser_Programming_Examples Parser Programming Examples
  1343. <P>
  1344. The use of the parser in the programming process is demonstrated
  1345. here. Both a basic step by step example and full code example are
  1346. presented.
  1347. \subsection Step_by_Step_Use_of_the_Parser Step by Step Use of the Parser
  1348. These are the four basic steps to follow to implement the use of the
  1349. GRASS parser in a GRASS command:
  1350. <P>
  1351. (1) Allocate memory for Flags and Options:
  1352. <P>
  1353. Flags and Options are pointers to structures allocated through the
  1354. parser routines <I>G_define_option()</I> and <I>G_define_flag()</I> as
  1355. defined in Parser_Routines.
  1356. \verbatim
  1357. #include <grass/gis.h> ; /* The standard GRASS include file */
  1358. struct Option *opt ; /* Establish an Option pointer for each option */
  1359. struct Flag *flag ; /* Establish a Flag pointer for each option */
  1360. opt = G_define_option() ; /* Request a pointer to memory for each option */
  1361. flag = G_define_flag() ; /* Request a pointer to memory for each flag */
  1362. \endverbatim
  1363. <P>
  1364. (2) Define members of Flag and Option structures:
  1365. <P>
  1366. The programmer should define the characteristics of each option and
  1367. flag desired as outlined by the following example:
  1368. \verbatim
  1369. opt->key = "option"; /* The name of this option is "option". */
  1370. opt->description = _("Option test"); /* The option description is "Option test" */
  1371. opt->type = TYPE_STRING; /* The data type of the answer to the option */
  1372. opt->required = YES; /* This option *is* required from the user */
  1373. flag->key = "t"; /* Single letter name for flag */
  1374. flag->description = _("Flag test"); /* The flag description is "Flag test" */
  1375. \endverbatim
  1376. <B>Note.</B>There are more options defined later in
  1377. Complete_Structure_Members_Table.
  1378. <P>
  1379. (3) Call the parser:
  1380. \verbatim
  1381. int
  1382. main(int argc, char *argv[]); /* command line args passed into main() */
  1383. if (G_parser(argc, argv)) /* Returns 0 if successful, non-zero otherwise */
  1384. exit(EXIT_FAILURE);
  1385. \endverbatim
  1386. (4) Extracting information from the parser structures:
  1387. \verbatim
  1388. fprintf(stdout, "For the option "%s" you chose: <%s>\n", opt->description, opt->answer );
  1389. fprintf(stdout, "The flag "-%s" is %s set.\n", flag->key, flag->answer ? "" : "not" );
  1390. \endverbatim
  1391. <P>
  1392. (5) Running the example program
  1393. <P>
  1394. Once such a module has been compiled (for example to the default
  1395. executable file <I>a.out</I> , execution will result in the following
  1396. user interface scenarios. Lines that begin with # imply user entered
  1397. commands on the command line.
  1398. \verbatim
  1399. # a.out help
  1400. \endverbatim
  1401. <P>
  1402. This is a standard user call for basic help information on the
  1403. module. The command line options (in this case, "help") are sent to
  1404. the parser via <I>G_parser().</I> The parser recognizes the "help"
  1405. command line option and returns a list of options and/or flags that
  1406. are applicable for the specific command. Note how the programmer
  1407. provided option and flag information is captured in the output.
  1408. \verbatim
  1409. a.out [-t] option=name
  1410. Flags:
  1411. -t Flag test
  1412. Parameters:
  1413. option Option test
  1414. \endverbatim
  1415. Now the following command is executed:
  1416. \verbatim
  1417. # a.out -t
  1418. \endverbatim
  1419. <P>
  1420. This command line does not contain the required option. Note that the
  1421. output provides this information along with the standard usage message
  1422. (as already shown above):
  1423. \verbatim
  1424. Required parameter <option> not set (Option test).
  1425. Usage:
  1426. a.out[-t] option=name
  1427. Flags:
  1428. -t Flag test
  1429. Parameters:
  1430. option Option test
  1431. \endverbatim
  1432. <P>
  1433. The following commands are correct and equivalent. The parser provides no
  1434. error messages and the module executes normally:
  1435. \verbatim
  1436. # a.out option=Hello -t
  1437. # a.out -t option=Hello
  1438. For the option "Option test" you chose: Hello
  1439. The flag "-t" is set.
  1440. \endverbatim
  1441. <P>
  1442. If this specific command has no fully interactive version (a user
  1443. interface that does not use the parser), the parser will prompt for
  1444. all programmer-defined options and/or flags.
  1445. <P>
  1446. User input is in <I>italics</I>, default answers are displayed in
  1447. square brackets [ ].
  1448. \verbatim
  1449. # a.out
  1450. OPTION: Option test
  1451. key: option
  1452. required: YES
  1453. enter option> <I>Hello</I>
  1454. You have chosen:
  1455. option=Hello
  1456. Is this correct? (y/n) [y] <I>y</I>
  1457. FLAG: Set the following flag?
  1458. Flag test? (y/n) [n] <I>n</I>
  1459. You chose: <Hello>
  1460. The flag is not set
  1461. \endverbatim
  1462. \section Full_Module_Example Full Module Example
  1463. <P>
  1464. The following code demonstrates some of the basic capabilities of the
  1465. parser. To compile this code, create this Makefile and run the
  1466. <I>make</I> command (see \ref Compiling_and_Installing_GRASS_Modules.
  1467. \verbatim
  1468. MODULE_TOPDIR = ../..
  1469. PGM = r.mysample
  1470. LIBES = $(GISLIB)
  1471. DEPENDENCIES = $(GISDEP)
  1472. include $(MODULE_TOPDIR)/include/Make/Module.make
  1473. default: cmd
  1474. \endverbatim
  1475. <P>
  1476. The sample.c code follows. You might experiment with this code to
  1477. familiarize yourself with the parser.
  1478. <P>
  1479. <B>Note.</B> This example includes some of the advanced structure members
  1480. described in Complete_Structure_Members_Table.
  1481. \verbatim
  1482. #include <stdlib.h>
  1483. #include <string.h>
  1484. #include <grass/gis.h>
  1485. #include "glocale.h"
  1486. int
  1487. main(int argc , char *argv[] )
  1488. {
  1489. struct Option *opt ;
  1490. struct Option *coor ;
  1491. struct Flag *flag ;
  1492. double X , Y ;
  1493. int n ;
  1494. opt = G_define_option() ;
  1495. opt->key = "debug" ;
  1496. opt->type = TYPE_STRING ;
  1497. opt->required = NO ;
  1498. opt->answer = "0" ;
  1499. opt->description = _("Debug level") ;
  1500. coor = G_define_option() ;
  1501. coor->key = "coordinate" ;
  1502. coor->key_desc = "x,y" ;
  1503. coor->type = TYPE_STRING ;
  1504. coor->required = YES ;
  1505. coor->multiple = YES ;
  1506. coor->description = _("One or more coordinates") ;
  1507. /* Note that coor->answer is not given a default value. */
  1508. flag = G_define_flag() ;
  1509. flag->key = 'v' ;
  1510. flag->description = _("Verbose execution") ;
  1511. /* Note that flag->answer is not given a default value. */
  1512. if (G_parser( argc , argv ))
  1513. exit (EXIT_FAILURE);
  1514. G_message("For the option <%s> you chose: <%s>\n", opt->description, opt->answer );
  1515. G_message("The flag <%s> is: %s set \n", flag->key, flag->answer ? "" : "not");
  1516. G_message("You specified the following coordinates:\n");
  1517. for ( n=0 ; coor->answers[n] != NULL ; n+=2 ) {
  1518. G_scan_easting ( coor->answers[n ] , &X , G_projection() );
  1519. G_scan_northing ( coor->answers[n+1] , &Y , G_projection() );
  1520. G_message( "%.31f,%.21f\n", X , Y );
  1521. }
  1522. }
  1523. \endverbatim
  1524. \section Compiling_and_Installing_GRASS_Modules Compiling and Installing GRASS Modules
  1525. GRASS modules are compiled and installed using the UNIX <i>make</i> command:
  1526. <i>make</i> reads a file named <i>Makefile</i> (see
  1527. Multiple_Architecture_Conventions for more information,) and then runs
  1528. the compiler. The GRASS compilation process allows for multiple-architecture
  1529. compilation from a single copy of the source code (for instance, if the source
  1530. code is NFS mounted to various machines with differing architectures.) This
  1531. chapter assumes that the programmer is familiar with <i>make</i> and its
  1532. accompanying <i>Makefile</i>.
  1533. <b>TODO: Explain ''auto-conf''....</b>
  1534. <b>TODO: Include contents of SUBMITTING and INSTALL files from source code</b>
  1535. To compile enter following:
  1536. \verbatim
  1537. ./configure
  1538. make
  1539. make install
  1540. \endverbatim
  1541. Then the code will be compiled into "/usr/local/grass-7.x.y" directory. The start
  1542. script "grass7x" will be placed into "/usr/local/bin/".\\
  1543. Optionally other target directories can be specified while "configuring":
  1544. \verbatim
  1545. ./configure --prefix=/opt/grass-7.x.y --with-bindir=/usr/bin
  1546. make
  1547. make install
  1548. \endverbatim
  1549. This will store the GRASS binaries into the directory "/opt/grass-7.x.y" and
  1550. the script mentioned above into "/usr/bin".
  1551. The script "make" is required to compile single modules. The
  1552. compilation process and requirements are discussed in more detail now.
  1553. \subsection Makefile_Variables Makefile Variables
  1554. <b>GRASS Libraries</b>. The following variables name the various GRASS
  1555. libraries:
  1556. <i>GISLIB</i> This names the <i>GIS Library</i>, which is the principal GRASS
  1557. library. See \ref GIS_Library for details about this library, and
  1558. \ref Loading_the_GIS_Library for a sample Makefile which
  1559. loads this library.
  1560. <i>VASKLIB</i> This names the <i>Vask Library</i>, which does full screen user
  1561. input.
  1562. <i>VASK</i> This specifies the <i>Vask Library</i> plus the UNIX [n]curses and
  1563. termcap libraries needed to use the <i>Vask Library</i> routines. See
  1564. \ref Vask_Library for details about this library, and \ref Loading_the_Vask_Library
  1565. for a sample Makefile which loads this library.
  1566. <i>SEGMENTLIB</i> This names the <i>Segment Library</i>, which manages large
  1567. matrix data. See \ref Segment_Library for details about this library,
  1568. and \ref Loading_the_Vask_Library for a sample <i>Makefile</i> which loads
  1569. this library.
  1570. <i>RASTERLIB</i> This names the <i>Raster Graphics Library</i>, which
  1571. communicates with GRASS graphics drivers. See \ref Raster_Graphics_Library
  1572. for details about this library, and \ref Loading_the_Raster_Graphics_Library
  1573. for a sample <i>Makefile</i> which loads this library.
  1574. <i>DISPLAYLIB</i> This names the <i>Display Graphics Library</i>, which provides a
  1575. higher level graphics interface to <i>RASTERLIB</i>. See
  1576. Display_Graphics_Library for details about this library, and
  1577. Loading_the_Display_Graphics_Library for a sample <i>Makefile</i>
  1578. which loads this library.\\
  1579. <b>UNIX Libraries:</b> The following variables name some useful UNIX
  1580. system libraries:
  1581. <i>MATHLIB</i> This names the math library. It should be used instead of the -lm
  1582. loader option.
  1583. <i>CURSES</i> This names both the curses and termcap libraries. It should be used
  1584. instead of the \newline
  1585. -lcurses/-lncurses and -ltermcap loader options. Do not use <tt>$CURSES</tt> if
  1586. you use <tt>$VASK</tt>.
  1587. <i>TERMLIB</i> This names the termcap library. It should be used instead of the
  1588. -ltermcap or -ltermlib loader options. Do not use <tt>$TERMLIB</tt> if you use
  1589. <tt>$VASK</tt> or <tt>$CURSES</tt>.\\
  1590. <b>Compiler and loader variables.</b> The following variables are related
  1591. to compiling and loading C programs:
  1592. <i>EXTRA\_CFLAGS</i> This variable can be used to add additional options
  1593. to <tt>$CFLAGS</tt>. It has no predefined values. It is usually used to specify
  1594. additional -I include directories, or -D preprocessor defines.
  1595. \subsection Constructing_a_Makefile Constructing a Makefile
  1596. The complete syntax for a <i>Makefile</i> is discussed in the UNIX
  1597. documentation for <i>make</i> and will not be repeated here. The essential
  1598. idea is that a target (e.g. a GRASS module) is to be built from a list of
  1599. dependencies (e.g. object files, libraries, etc.). The relationship between
  1600. the target, its dependencies, and the rules for constructing the target is
  1601. expressed according to the following syntax:
  1602. \verbatim
  1603. target: dependencies
  1604. actions
  1605. more actions
  1606. \endverbatim
  1607. If the target does not exist, or if any of the dependencies have a newer
  1608. date than the target (i.e., have changed), the actions will be executed to
  1609. build the target. The actions must be indented using a TAB. <i>make</i> is
  1610. picky about this. It does not like spaces in place of the TAB.
  1611. \section Multiple_Architecture_Conventions Multiple-Architecture Conventions
  1612. The following conventions allow for multiple architecture compilation on a
  1613. machine that uses a common or networked GRASS source code directory tree.
  1614. <P>
  1615. Object files and library archives are compiled into subdirectories that
  1616. represent the architecture that they were compiled on. These subdirectories
  1617. are created in the $SRC directory as OBJ.<I>arch</I> and
  1618. LIB.<I>arch</I>, where <I>arch</I> represents the architecture of the
  1619. compiling machine. Thus, for example, $SRC/OBJ.sun4 would contain the
  1620. object files for Sun/4 and SPARC architectures, and $SRC/LIB.686-pc-linux-gnu would
  1621. contain library archives for Linux architectures. Likewise,
  1622. $SRC/OBJ.686-pc-linux-gnu would contain the object files for Linux architectures,
  1623. and $SRC/LIB.686-pc-linux-gnu would contain library archives for Linux
  1624. architectures.
  1625. <P>
  1626. Note that 'arch' is defined for a specific architecture during setup and
  1627. compilation of GRASS, it is not limited to sun4 or any specific string.
  1628. \section Full_Structure_Members_Description Full Structure Members Description
  1629. <P>
  1630. There are many members to the Option and Flag structures. The following
  1631. tables and descriptions summarize all defined members of both the Option and
  1632. Flag structures.
  1633. <P>
  1634. An in-depth summary of the more complex structure members is presented in
  1635. Description_of_Complex_Structure_Members.
  1636. \section Complete_Structure_Members_Table Complete Structure Members Table
  1637. <B>struct Flag</B>
  1638. <table border=1>
  1639. <tr>
  1640. <td>structure member</td>
  1641. <td>C type</td>
  1642. <td>required</td>
  1643. <td>default</td>
  1644. <td> description and example</td>
  1645. </tr><tr>
  1646. <td>key</td>
  1647. <td> char</td>
  1648. <td> YES</td>
  1649. <td> none</td>
  1650. <td> Key char used on command line<br>
  1651. flag->key = 'f' ;</td>
  1652. </tr><tr>
  1653. <td>Description</td>
  1654. <td> char *</td>
  1655. <td> YES</td>
  1656. <td> none</td>
  1657. <td> String describing flag meaning<br>
  1658. flag->description = _("run in fast mode") ;</td>
  1659. </tr><tr>
  1660. <td>answer</td>
  1661. <td> char</td>
  1662. <td> NO</td>
  1663. <td> NULL</td>
  1664. <td> Default and parser-returned
  1665. flag states.</td>
  1666. </tr>
  1667. </table>
  1668. <P>
  1669. <B>struct Option</B>
  1670. <table border=1>
  1671. <tr>
  1672. <td>structure member</td>
  1673. <td>C type </td>
  1674. <td>required </td>
  1675. <td>default </td>
  1676. <td>description and example</td>
  1677. </tr>
  1678. <tr>
  1679. <td>key </td>
  1680. <td>char * </td>
  1681. <td>YES </td>
  1682. <td>none </td>
  1683. <td>Key word used on command line.<br>
  1684. opt->key = "map" ;</td>
  1685. </tr>
  1686. <tr>
  1687. <td>type </td>
  1688. <td>int </td>
  1689. <td>YES </td>
  1690. <td>none </td>
  1691. <td>Option type: <br>
  1692. TYPE_STRING <br>
  1693. TYPE_INTEGER <br>
  1694. TYPE_DOUBLE <br>
  1695. opt->type = TYPE_STRING ;</td>
  1696. </tr>
  1697. <tr>
  1698. <td>Description </td>
  1699. <td>char * </td>
  1700. <td>YES </td>
  1701. <td>none </td>
  1702. <td>String describing option along with gettext macro for internationalization
  1703. opt->description = _("Map name") ;</td>
  1704. </tr>
  1705. <tr>
  1706. <td>answer </td>
  1707. <td>char * </td>
  1708. <td>NO </td>
  1709. <td>NULL </td>
  1710. <td>Default and parser-returned answer to an option.<br>
  1711. opt->answer = "defaultmap" ;</td>
  1712. </tr>
  1713. <tr>
  1714. <td>key_desc </td>
  1715. <td>char * </td>
  1716. <td>NO </td>
  1717. <td>NULL </td>
  1718. <td>Single word describing the key. Commas in this string denote
  1719. to the parser that several comma-separated arguments are expected
  1720. from the user as one answer. For example, if a pair of coordinates
  1721. is desired, this element might be defined as follows.<br>
  1722. opt->key_desc = "x,y" ; </td>
  1723. </tr>
  1724. <tr>
  1725. <td>structure member</td>
  1726. <td>C type </td>
  1727. <td>required </td>
  1728. <td>default </td>
  1729. <td>description and example</td>
  1730. </tr>
  1731. <tr>
  1732. <td>multiple </td>
  1733. <td>int </td>
  1734. <td>NO </td>
  1735. <td>NO </td>
  1736. <td>Indicates whether the user can provide multiple answers or not.
  1737. YES and NO are defined in "gis.h" and should be used (NO is
  1738. the default.) Multiple is used in conjunction with the answers
  1739. structure member below. opt->multiple = NO ;</td>
  1740. </tr>
  1741. <tr>
  1742. <td>answers </td>
  1743. <td> </td>
  1744. <td>NO </td>
  1745. <td>NULL </td>
  1746. <td>Multiple parser-returned answers to an option. N/A</td>
  1747. </tr>
  1748. <tr>
  1749. <td>required </td>
  1750. <td>int </td>
  1751. <td>NO </td>
  1752. <td>NO </td>
  1753. <td>Indicates whether user MUST provide the option on the command
  1754. line. YES and NO are defined in "gis.h" and should be used (NO
  1755. is the default.) opt->required = YES ;</td>
  1756. </tr>
  1757. <tr>
  1758. <td>options </td>
  1759. <td>char * </td>
  1760. <td>NO </td>
  1761. <td>NULL </td>
  1762. <td>Approved values or range of values. <br>
  1763. opt->options = "red,blue,white" ;<br>
  1764. For integers and doubles, the following format is available: <br>
  1765. opt->options = "0-1000" ;</td>
  1766. </tr>
  1767. <tr>
  1768. <td>gisprompt</td>
  1769. <td>char *</td>
  1770. <td>NO</td>
  1771. <td>NULL</td>
  1772. <td>Interactive prompt guidance. There are three comma separated
  1773. parts to this argument which guide the use of the standard GRASS
  1774. file name prompting routines.<br>
  1775. opt->gisprompt = "old,cell,raster" ;</td>
  1776. </tr>
  1777. <tr>
  1778. <td>checker</td>
  1779. <td>char *()</td>
  1780. <td>NO</td>
  1781. <td>NULL</td>
  1782. <td>Routine to check the answer to an option<br>
  1783. m opt->checker = my_routine() ;</td>
  1784. </tr>
  1785. </table>
  1786. \section Description_of_Complex_Structure_Members Description of Complex Structure Members
  1787. What follows are explanations of possibly confusing structure
  1788. members. It is intended to clarify and supplement the structures table
  1789. above.
  1790. \subsection Answer_member_of_the_Flag_and_Option_structures Answer member of the Flag and Option structures
  1791. The answer structure member serves two functions for GRASS commands
  1792. that use the parser.
  1793. <P>
  1794. <B><I>(1) To set the default answer to an option:</I></B>
  1795. <P>
  1796. If a default state is desired for a programmer-defined option, the
  1797. programmer may define the Option structure member "answer" before
  1798. calling <I>G_parser()</I> in his module. After the <I>G_parser()</I> call,
  1799. the answer member will hold this preset default value if the user did
  1800. <I>not</I> enter an option that has the default answer member value.
  1801. <P>
  1802. <B><I>(2) To obtain the command-line answer to an option or flag:
  1803. After a call to</I> </B><B>G_parser()</B><B><I>, the answer member will
  1804. contain one of two values:</I></B>
  1805. <P>
  1806. (a) If the user provided an option, and answered this option on the command
  1807. line, the default value of the answer member (as described above) is
  1808. replaced by the user's input.
  1809. <P>
  1810. (b) If the user provided an option, but did <I>not</I> answer this option
  1811. on the command line, the default is not used. The user may use the default
  1812. answer to an option by withholding mention of the option on the command
  1813. line. But if the user enters an option without an answer, the default answer
  1814. member value will be replaced and set to a NULL value by <I>G_parser().</I>
  1815. <P>
  1816. As an example, please review the use of answer members in the structures
  1817. implemented in Full_Module_Example.
  1818. \subsection Multiple_and_Answers_Members Multiple and Answers Members
  1819. <P>
  1820. The functionality of the answers structure member is reliant on the
  1821. programmer's definition of the multiple structure member. If the multiple
  1822. member is set to NO, the answer member is used to obtain the answer to an
  1823. option as described above.
  1824. <P>
  1825. If the multiple structure member is set to YES, the programmer has told
  1826. <I>G_parser()</I> to capture multiple answers. Multiple answers are
  1827. separated by commas on the command line after an option.
  1828. <P>
  1829. <B>Note.</B> <I>G_parser()</I> does not recognize any character other
  1830. than a comma to delimit multiple answers.
  1831. <P>
  1832. After the programmer has set up an option to receive multiple answers,
  1833. these the answers are stored in the answers member of the Option
  1834. structure. The answers member is an array that contains each
  1835. individual user-entered answer. The elements of this array are the
  1836. type specified by the programmer using the type member. The answers
  1837. array contains however many comma-delimited answers the user entered,
  1838. followed (terminated) by a NULL array element.
  1839. <P>
  1840. For example, here is a sample definition of an Option using multiple and
  1841. answers structure members:
  1842. \verbatim
  1843. opt->key ="option" ;
  1844. opt->description = _("option example") ;
  1845. opt->type = TYPE_INTEGER ;
  1846. opt->required = NO ;
  1847. opt->multiple = YES ;
  1848. \endverbatim
  1849. <P>
  1850. The above definition would ask the user for multiple integer answers
  1851. to the option. If in response to a routine that contained the above
  1852. code, the user entered "option=1,3,8,15" on the command line, the
  1853. answers array would contain the following values:
  1854. \verbatim
  1855. answers[0] == 1
  1856. answers[1] == 3
  1857. answers[2] == 8
  1858. answers[3] == 15
  1859. answers[4] == NULL
  1860. \endverbatim
  1861. \subsection key_desc_Member key_desc Member
  1862. <P>
  1863. The key_desc structure member is used to define the format of a single
  1864. command line answer to an option. A programmer may wish to ask for one
  1865. answer to an option, but this answer may not be a single argument of a type
  1866. set by the type structure member. If the programmer wants the user to enter
  1867. a coordinate, for example, the programmer might define an Option as follows:
  1868. \verbatim
  1869. opt->key ="coordinate" ;
  1870. opt->description = _("Specified Coordinate") ;
  1871. opt->type = TYPE_INTEGER ;
  1872. opt->required = NO ;
  1873. opt->key_desc = "x,y"
  1874. opt->multiple = NO ;
  1875. \endverbatim
  1876. <P>
  1877. The answer to this option would <I>not</I> be stored in the answer member,
  1878. but in the answers member. If the user entered "coordinate=112,225" on the
  1879. command line in response to a routine that contains the above option
  1880. definition, the answers array would have the following values after the call
  1881. to <I>G_parser()</I>:
  1882. \verbatim
  1883. answers[0] == 112
  1884. answers[1] == 225
  1885. answers[2] == NULL
  1886. \endverbatim
  1887. <P>
  1888. Note that "coordinate=112" would not be valid, as it does not contain both
  1889. components of an answer as defined by the key_desc structure member.
  1890. <P>
  1891. If the multiple structure member were set to YES instead of NO in the
  1892. example above, the answers are stored sequentially in the answers member.
  1893. For example, if the user wanted to enter the coordinates (112,225),
  1894. (142,155), and (43,201), his response on the command line would be
  1895. "coordinate=112,225,142,155,43,201". Note that <I>G_parser()</I>
  1896. recognizes only a comma for both the key_desc member, and for multiple
  1897. answers.
  1898. <P>
  1899. The answers array would have the following values after a call to
  1900. <I>G_parser()</I>:
  1901. \verbatim
  1902. answers[0] == 112 answers[1] == 225
  1903. answers[2] == 142 answers[3] == 155
  1904. answers[4] == 43 answers[5] == 201
  1905. answers[6] == NULL
  1906. \endverbatim
  1907. <P>
  1908. <B>Note.</B> In this case as well, neither "coordinate=112" nor
  1909. "coordinate=112,225,142" would be valid command line arguments, as
  1910. they do not contain even pairs of coordinates. Each answer's format
  1911. (as described by the key_desc member) must be fulfilled completely.
  1912. <P>
  1913. The overall function of the key_desc and multiple structure members is
  1914. very similar. The key_desc member is used to specify the number of
  1915. <I>required</I> components of a single option answer (e.g. a
  1916. multi-valued coordinate.) The multiple member tells <I>G_parser()</I> to
  1917. ask the user for multiple instances of the compound answer as defined
  1918. by the format in the key_desc structure member.
  1919. <P>
  1920. Another function of the key_desc structure member is to explain to the
  1921. user the type of information expected as an answer. The coordinate
  1922. example is explained above.
  1923. <P>
  1924. The usage message that is displayed by <I>G_parser()</I> in case of an
  1925. error, or by
  1926. <P>
  1927. <I>G_usage()</I> on programmer demand, is shown below. The Option
  1928. "option" for the command <I>a.out</I> does not have its key_desc
  1929. structure member defined.
  1930. \verbatim
  1931. Usage:
  1932. a.out option=name
  1933. \endverbatim
  1934. <P>
  1935. The use of "name" is a <I>G_parser()</I> standard. If the programmer
  1936. defines the key_desc structure member before a call to <I>G_parser()</I>,
  1937. the value of the key_desc member replaces "name". Thus, if the key_desc
  1938. member is set to "x,y" as was used in an example above, the following
  1939. usage message would be displayed:
  1940. \verbatim
  1941. Usage:
  1942. a.out option=x,y
  1943. \endverbatim
  1944. <P>
  1945. The key_desc structure member can be used by the programmer to clarify the
  1946. usage message as well as specify single or multiple required components of a
  1947. single option answer.
  1948. \subsection gisprompt_Member gisprompt Member
  1949. <P>
  1950. The gisprompt Option structure item requires a bit more
  1951. description. The three comma-separated (no spaces allowed)
  1952. sub-arguments are defined as follows:
  1953. <P>
  1954. First argument:
  1955. <P>
  1956. "old" results in a call to the GRASS library subroutine
  1957. <I>G_ask_old()</I>, "new" to <I>G_ask_new(), "any" to
  1958. G_ask_any(), and "mapset" to G_ask_in_mapset().</I>
  1959. <P>
  1960. Second argument:
  1961. <P>
  1962. This is identical to the "element" argument in the above subroutine calls.
  1963. It specifies a directory inside the mapset that may contain the user's
  1964. response.
  1965. <P>
  1966. Third argument:
  1967. <P>
  1968. Identical to the "prompt" argument in the above subroutine calls. This is
  1969. a string presented to the user that describes the type of data element being
  1970. requested.
  1971. <P>
  1972. Here are two examples:
  1973. <P>
  1974. <B>gisprompt arguments Resulting call</B>
  1975. <P>
  1976. "new,cell,raster" G_ask_new("", buffer, "cell", "raster")
  1977. <P>
  1978. "old,dig,vector" G_ask_old("", buffer, "dig", "vector")
  1979. <P>
  1980. \subsection Common_Questions Common Questions
  1981. <P>
  1982. "How is automatic prompting turned off?"
  1983. <P>
  1984. GRASS 4.0 introduced a new method for driving GRASS interactive and
  1985. non-interactive modules as described in \ref Compiling_and_Installing_GRASS_Programs.
  1986. Here is a short overview.
  1987. <P>
  1988. For most modules a user runs a front-end module out of the GRASS bin
  1989. directory which in turn looks for the existence of interactive and
  1990. non-interactive versions of the module. If an interactive version
  1991. exists and the user provided no command line arguments, then that
  1992. version is executed.
  1993. <P>
  1994. In such a situation, the parser's default interaction will never be seen by
  1995. the user. A programmer using the parser is able to avoid the front-end's
  1996. default search for a fully interactive version of the command by placing a
  1997. call to <I>G_disable_interactive()</I> before calling <I>G_parser()</I>
  1998. (see \ref Parser_Routines for details.)
  1999. <P>
  2000. "Can the user mix options and flags?"
  2001. <P>
  2002. Yes. Options and flags can be given in any order.
  2003. <P>
  2004. "In what order does the parser present options and flags?"
  2005. <P>
  2006. Flags and options are presented by the usage message in the order that the
  2007. programmer defines them using calls to <I>G_define_option()</I> and
  2008. <I>G_define_flag().</I>
  2009. <P>
  2010. "How does a programmer query for coordinates?"
  2011. <P>
  2012. For any user input that requires a set of arguments (like a pair of map
  2013. coordinates,) the programmer specifies the number of arguments in the
  2014. key_desc member of the Option structure. For example, if opt-&gt;key_desc was
  2015. set to "x,y", the parser will require that the user enter a pair of
  2016. arguments separated only by a comma. See the source code for the GRASS
  2017. commands r.drain or r.cost for examples.
  2018. <P>
  2019. "Is a user required to use full option names?"
  2020. <P>
  2021. No! Users are required to type in only as many characters of an option name
  2022. as is necessary to make the option choice unambiguous. If, for example,
  2023. there are two options, "input=" and "output=", the following would be
  2024. valid command line arguments:
  2025. <P>
  2026. # command i=map1 o=map2
  2027. <P>
  2028. # command in=map1 out=map2
  2029. <P>
  2030. "Are options standardized at all?"
  2031. <P>
  2032. Yes. There are a few conventions. Options which identify a single input map
  2033. are usually "map=", not "raster=" or "vector=". In the case of an
  2034. input and output map the convention is: "input=xx output=yy". By passing
  2035. the 'help' option to existing GRASS commands, it is likely that you will
  2036. find other conventions. The desire is to make it as easy as possible for the
  2037. user to remember (or guess correctly) what the command line syntax is for a
  2038. given command.
  2039. <P>
  2040. \section String_Manipulation_Functions String Manipulation Functions
  2041. <P>
  2042. This section describes some routines which perform string manipulation.
  2043. Strings have the usual C meaning: a NULL terminated array of characters.
  2044. <P>
  2045. These next 3 routines remove unwanted white space from a single string.
  2046. <P>
  2047. char * G_squeeze() remove unnecessary white space
  2048. Leading and trailing white space is removed from the string <B>s</B>
  2049. and internal white space which is more than one character is reduced
  2050. to a single space character. White space here means spaces, tabs,
  2051. linefeeds, newlines, and formfeeds. Returns <B>s.</B>
  2052. <P>
  2053. void G_strip() remove leading/training white space
  2054. Leading and trailing white space is removed from the string <B>s.</B>
  2055. White space here means only spaces and tabs. There is no return value.
  2056. <P>
  2057. char * G_chop() Chop leading and trailing white spaces:
  2058. space, &#92;f, &#92;n, &#92;r, &#92;t, &#92;v - returns pointer to string
  2059. <P>
  2060. The next routines replaces character(s) from string.
  2061. <P>
  2062. char * G_strchg() replace character(s)
  2063. Replace all occurencies of character in string bug with new. Returns
  2064. changed string
  2065. <P>
  2066. This next routine copies a string to allocated memory.
  2067. <P>
  2068. char * G_store() copy string to allocated memory
  2069. This routine allocates enough memory to hold the string <B>s</B>,
  2070. copies <B>s</B> to the allocated memory, and returns a pointer to the
  2071. allocated memory.
  2072. <P>
  2073. The next 2 routines convert between upper and lower case.
  2074. <P>
  2075. char * G_tolcase() convert string to lower case
  2076. Upper case letters in the string <B>s</B> are converted to their lower
  2077. case equivalent. Returns <B>s.</B>
  2078. <P>
  2079. char * G_toucase() convert string to upper case
  2080. Lower case letters in the string <B>s</B> are converted to their upper
  2081. case equivalent. Returns <B>s.</B>
  2082. <P>
  2083. And finally a routine which gives a printable version of control characters.
  2084. <P>
  2085. char * G_unctrl() printable version of control
  2086. character
  2087. This routine returns a pointer to a string which contains an
  2088. English-like representation for the character <B>c.</B> This is useful
  2089. for nonprinting characters, such as control characters. Control
  2090. characters are represented by ctrl-C, e.g., control A is represented
  2091. by ctrl-A. 0177 is represented by DEL/RUB. Normal characters remain
  2092. unchanged.
  2093. <P>
  2094. This routine is useful in combination with <I>G_intr_char()</I> for printing
  2095. the user's interrupt character:
  2096. \verbatim
  2097. char G_intr_char();
  2098. char * G_unctrl();
  2099. fprintf(stdout, "Your interrupt character is %s\n",
  2100. G_unctrl(G_intr_char()));
  2101. \endverbatim
  2102. <P>
  2103. <B>Note.</B> G_unctrl() uses a hidden static buffer which is
  2104. overwritten from call to call.
  2105. <P>
  2106. <I>FOLLOWING new FUNCTIONS need to be merged into the flow of this text:</I>
  2107. <P>
  2108. int G_trim_decimal() trim
  2109. This routine remove trailing zeros from decimal number for example:
  2110. 23.45000 would come back as 23.45
  2111. <P>
  2112. char * G_index() delimiter
  2113. Position of delimiter
  2114. <P>
  2115. char * G_rindex () ??????
  2116. <P>
  2117. int G_strcasecmp(char *a, char *b) string compare ignoring case (upper or lower)
  2118. returns: -1 if a&lt;b
  2119. <br>
  2120. 0 if a==b
  2121. <br>
  2122. 1 if a&gt;b
  2123. <P>
  2124. char * G_strstr() Return a pointer
  2125. to the first occurrence of subString in mainString, or NULL if no occurrences
  2126. are found
  2127. <P>
  2128. char * G_strdup() Return a pointer to a string that is a
  2129. duplicate of the string given to G_strdup. The duplicate is created
  2130. using malloc. If unable to allocate the required space, NULL is
  2131. returned.
  2132. \section Enhanced_UNIX_Routines Enhanced UNIX Routines
  2133. <P>
  2134. A number of useful UNIX library routines have side effects which are
  2135. sometimes undesirable. The routines here provide the same functions as their
  2136. corresponding UNIX routine, but with different side effects.
  2137. <P>
  2138. \subsection Running in the Background Running in the Background
  2139. <P>
  2140. The standard UNIX fork() routine creates a child process which is a
  2141. copy of the parent process. The fork() routine is useful for placing a
  2142. module into the background. For example, a module that gathers input
  2143. from the user interactively, but knows that the processing will take a
  2144. long time, might want to run in the background after gathering all the
  2145. input. It would fork() to create a child process, the parent would
  2146. exit() allowing the child to continue in the background, and the user
  2147. could then do other processing.
  2148. <P>
  2149. However, there is a subtle problem with this logic. The fork() routine does not
  2150. protect child processes from keyboard interrupts even if the parent is no longer
  2151. running. Keyboard interrupts will also kill background processes that do not
  2152. protect themselves.
  2153. <br>
  2154. Note: Programmers who use /bin/sh know that programs run in the
  2155. background (using & on the command line) are not automatically
  2156. protected from keyboard interrupts. To protect a command that is run
  2157. in the background, /bin/sh users must do nohup command &. Programmers
  2158. who use the /bin/csh (or other variants) do not know, or forget that
  2159. the C-shell automatically protects background processes from keyboard
  2160. interrupts.
  2161. <br>
  2162. Thus a module which puts itself in the background may never finish if
  2163. the user interrupts another module which is running at the keyboard.
  2164. <P>
  2165. The solution is to fork() but also put the child process in a process group
  2166. which is different from the keyboard process group. G_fork() does this.
  2167. <P>
  2168. pid_t G_fork() create a protected child process
  2169. This routine creates a child process by calling the UNIX fork()
  2170. routine. It also changes the process group for the child so that
  2171. interrupts from the keyboard do not reach the child. It does not cause
  2172. the parent to exit().
  2173. <P>
  2174. G_fork() returns what fork() returns: -1 if fork() failed; otherwise 0
  2175. to the child, and the process id of the new child to the parent.
  2176. <P>
  2177. <B>Note.</B> Interrupts are still active for the child. Interrupts
  2178. sent using the <I>kill</I> command, for example, will interrupt the
  2179. child. It is simply that keyboard-generated interrupts are not sent to
  2180. the child.
  2181. <P>
  2182. \subsection Partially_Interruptible_System_Call Partially Interruptible System Call
  2183. The UNIX system() call allows one program, the parent, to execute
  2184. another UNIX command or module as a child process, wait for that
  2185. process to complete, and then continue. The problem addressed here
  2186. concerns interrupts. During the standard system() call, the child
  2187. process inherits its responses to interrupts from the parent. This
  2188. means that if the parent is ignoring interrupts, the child will ignore
  2189. them as well. If the parent is terminated by an interrupt, the child
  2190. will be also.
  2191. <P>
  2192. However, in some cases, this may not be the desired effect. In a menu
  2193. environment where the parent activates menu choices by running commands
  2194. using the system() call, it would be nice if the user could interrupt the
  2195. command, but not terminate the menu module itself. The G_system() call
  2196. allows this.
  2197. <P>
  2198. int G_system() run a shell level command
  2199. The shell level <B>command</B> is executed. Interrupt signals for the
  2200. parent module are ignored during the call. Interrupt signals for the
  2201. <B>command</B> are enabled. The interrupt signals for the parent are
  2202. restored to their previous settings upon return.
  2203. <P>
  2204. G_system() returns the same value as system(), which is essentially
  2205. the exit status of the <B>command.</B> See UNIX manual system(1) for
  2206. details.
  2207. <P>
  2208. \subsection ENDIAN_test ENDIAN test
  2209. <P>
  2210. To test if the user's machine is little or big ENDIAN, the following
  2211. function is provided:
  2212. <P>
  2213. int G_is_little_endian() test little ENDIAN
  2214. Test if machine is little or big endian.
  2215. <P>
  2216. Returns:
  2217. <br>
  2218. 1 little endian
  2219. <br>
  2220. 0 big endian
  2221. <P>
  2222. \subsection Unix_Socket_Functions Unix Socket Functions
  2223. <P>
  2224. The following provide a simplified interface for interprocess
  2225. communication via Unix sockets. The caller need only be concerned
  2226. with the path to the socket file and the various file descriptors for
  2227. socket connections. The caller does not need to worry about handling
  2228. socket structures - which, unlike internet sockets, have little
  2229. utility once a file descriptor has been opened on a connection. All
  2230. socket functions in the GIS library have a <B>G_sock()</B> prefix.
  2231. One should keep in mind that unix sockets connections can both be read
  2232. from and written to. Also, it is possible for calls to <B>read()</B>
  2233. and <B>write()</B> to read or write fewer bytes than specified.
  2234. Hence, looping calls may be required to read or write all of the data.
  2235. The <B>read()</B> will still normally block if there is nothing to
  2236. read, so a zero byte return value typically means the connection has
  2237. been closed. The <B>write()</B> function typically returns
  2238. immediately (see W. Richard Stevens. 1997. UNIX network programming:
  2239. Volume 1, 2nd edition. Prentice Hall).
  2240. <P>
  2241. char * G_sock_get_fname() makes full socket path
  2242. Takes a simple <B>name</B> for a communication channel and builds the
  2243. full path for a sockets file with that <B>name</B>. The path as of
  2244. this writing (2000-02-18) is located in the temporary directory for
  2245. the user's current mapset (although this will likely change). A
  2246. <B>NULL</B> pointer is returned if the function fails for some reason.
  2247. The caller is responsible for freeing the memory of the returned
  2248. string when it is no longer needed.
  2249. <P>
  2250. int G_sock_exists() does the socket exist
  2251. Takes the full path to a unix socket; determines if the file exists;
  2252. and if the file exists whether it is a socket file or not. Returns a
  2253. non-zero value if the file exists and is a socket file. Otherwise it
  2254. returns zero.
  2255. <P>
  2256. int G_sock_bind() binds the socket
  2257. Takes the full path to a unix socket and attempts to bind a file
  2258. descriptor to the path <B>name</B>. If successful it will return the
  2259. file descriptor. Otherwise, it returns -1. The socket file must not
  2260. already exist. If it does, this function will fail and set the global
  2261. <B>errno</B> to <B>EADDRINUSE</B>. Other error numbers may be set if
  2262. the call to <B>bind()</B> fails. Server programs wishing to bind a
  2263. socket should test if the socket file they wish to use already exists.
  2264. And, if so, they may try to connect to the socket to see if it is in
  2265. use. If it is not in use, such programs may then call <B>unlink()</B>
  2266. or <B>remove()</B> to delete the file before calling
  2267. <B>G_sock_bind()</B>. It is important that server processes do not
  2268. just delete existing socket files without testing the connection.
  2269. Doing so may make another server process unreachable (i.e. you will
  2270. have hijacked the other server's communication channel). Server
  2271. processes must call <B>G_sock_bind()</B> prior to calling
  2272. <B>G_sock_listen()</B> and <B>G_sock_accept()</B>.
  2273. <P>
  2274. int G_sock_listen() listen on a socket
  2275. Takes the file descriptor returned by a successful call to
  2276. <B>G_sock_bind()</B> and the length of the the listen queue. A
  2277. successful call will return 0, while a failed call will return -1.
  2278. The global <B>errno</B> will contain the error number corresponding to
  2279. the reason for the failure. The queue length should never be zero.
  2280. Some systems may interpret this to mean that no connections should be
  2281. queued. Other systems may add a fudge factor to the queue length that
  2282. the caller specifies. Servers that don't want additional connections
  2283. queued should <B>close()</B> the listening file descriptor after a
  2284. successful call to <B>G_sock_accept()</B>. This function is a simple
  2285. wrapper around the system <B>listen()</B> function.
  2286. <P>
  2287. int G_sock_accept() accept a connection on the listening
  2288. socket
  2289. Takes the file descriptor returned by a successful call to
  2290. <B>G_sock_bind()</B>, for which a successful call to
  2291. <B>G_sock_listen()</B> has also been made, and waits for an incoming
  2292. connection. When a connection arrives, the file descriptor for the
  2293. connection is returned. This function normally blocks indefinitely.
  2294. However, an interrupt like <B>SIGINT</B> may cause this function to
  2295. return without a valid connection. In this case, the return value
  2296. will be -1 and the global error number will be set to <B>EINTR</B>.
  2297. Servers should handle this possibility by calling
  2298. <B>G_sock_accept()</B> again. A typical server might have a call to
  2299. <B>fork()</B> after a successful return from <B>G_sock_accept()</B>.
  2300. A server might also use <B>select()</B> to see if an a connection is
  2301. ready prior to calling <B>G_sock_accept()</B>. This function is a
  2302. simple wrapper around the system's <B>accept()</B> function, with the
  2303. second and third arguments being <B>NULL</B>.
  2304. <P>
  2305. int G_sock_connect() make a connection to a server process
  2306. Takes the full path to a socket file and attempts to make a connection
  2307. to a server listening for connections. If successful, the file
  2308. descriptor for the socket connection is returned. Otherwise, -1 is
  2309. returned and the global <B>errno</B> may be set. This function and
  2310. <B>G_sock_get_fname()</B> are the only functions a client program
  2311. really needs to worry about. If the caller wants to be sure that the
  2312. global error number was set from an unsuccessful call to this
  2313. function, she should zero <B>errno</B> prior to the call. Failures
  2314. due to a non-existent socket file or a path name that exceeds system
  2315. limits, will not change the global error number.
  2316. \subsection Trivial_Socket_Server_Example Trivial Socket Server
  2317. Example
  2318. \verbatim
  2319. #include <stdio.h>
  2320. #include <stdlib.h>
  2321. #include <errno.h>
  2322. #include <grass/gis.h>
  2323. int main (int argc, char *argv[])
  2324. {
  2325. int listenfd, rwfd;
  2326. char *path;
  2327. pid_t pid;
  2328. /* Path is built using server's name */
  2329. if (NULL == (path = G_sock_get_fname (argv[0])))
  2330. exit (EXIT_FAILURE);
  2331. /* Make sure another instance isn't running */
  2332. if (G_sock_exists (path))
  2333. {
  2334. if ((listenfd = G_sock_connect (path)) != -1)
  2335. {
  2336. close (listenfd);
  2337. exit (EXIT_FAILURE);
  2338. }
  2339. remove (path);
  2340. }
  2341. /* Bind the socket */
  2342. if ((listenfd = G_sock_bind (path)) < 0)
  2343. exit (EXIT_FAILURE);
  2344. /* Begin listening on the socket */
  2345. if (G_sock_listen (listenfd, 1) != 0)
  2346. exit (EXIT_FAILURE);
  2347. /* Loop forever waiting for connections */
  2348. for (;;)
  2349. {
  2350. if ((rwfd = G_sock_accept (listenfd)) < 0)
  2351. {
  2352. if (errno == EINTR)
  2353. continue;
  2354. }
  2355. else
  2356. exit (EXIT_FAILURE);
  2357. /* Fork connection */
  2358. if ((pid = fork()) == 0)
  2359. {
  2360. char c;
  2361. /* child closes listenfd */
  2362. close (listenfd);
  2363. while (read (rwfd, &amp;c, 1) > 0)
  2364. write (rwfd, &amp;c, 1);
  2365. close (rwfd);
  2366. return 0;
  2367. }
  2368. else if (pid > 0)
  2369. {
  2370. /* parent closes rwfd
  2371. * a well behaved server would limit
  2372. * the number of forks.
  2373. */
  2374. close (rwfd);
  2375. }
  2376. else
  2377. exit (EXIT_FAILURE);
  2378. }
  2379. G_free (path);
  2380. return 0;
  2381. }
  2382. \endverbatim
  2383. \subsection Miscellaneous Miscellaneous
  2384. <P>
  2385. A number of general purpose routines have been provided.
  2386. <P>
  2387. char * G_date() current date and time
  2388. Returns a pointer to a string which is the current date and time. The
  2389. format is the same as that produced by the UNIX <I>date</I> command.
  2390. <P>
  2391. char * G_gets() get a line of input (detect ctrl-z)
  2392. This routine does a <I>gets()</I> from stdin into <B>buf.</B> It exits
  2393. if end-of-file is detected. If stdin is a tty (i.e., not a pipe or
  2394. redirected) then ctrl-z is detected. Returns 1 if the read was
  2395. successful, or 0 if ctrl-z was entered.
  2396. <P>
  2397. <B>Note.</B> This is very useful for allowing a module to reprompt when a
  2398. module is restarted after being stopped with a ctrl-z. If this routine returns
  2399. 0, then the calling module should reprint a prompt and call <I>G_gets()</I>
  2400. again. For example:
  2401. \verbatim
  2402. char buf[1024];
  2403. do{
  2404. fprintf(stdout, "Enter some input: ") ;
  2405. } while ( ! G_gets(buf) ) ;
  2406. \endverbatim
  2407. <P>
  2408. char * G_home() user's home directory
  2409. Returns a pointer to a string which is the full path name of the
  2410. user's home directory.
  2411. <P>
  2412. char G_intr_char() return interrupt char
  2413. This routine returns the user's keyboard interrupt character. This is
  2414. the character that generates the SIGINT signal from the keyboard.
  2415. <P>
  2416. See also <I>G_unctr()</I> for converting this character to a printable format.
  2417. <P>
  2418. int G_percent(int n, int total, int incr) print percent complete
  2419. messages
  2420. This routine prints a percentage complete message to stderr. The
  2421. percentage complete is (<B>n</B>/ <B>total</B>)*100, and these are
  2422. printed only for each <B>incr</B> percentage. This is perhaps best
  2423. explained by example:
  2424. \verbatim
  2425. # include <stdio.h>
  2426. int row;
  2427. int nrows;
  2428. nrows = 1352; /* 1352 is not a special value - example only */
  2429. fprintf (stderr, "Percent complete: ");
  2430. for (row = 0; row < nrows; row++)
  2431. G_percent (row, nrows, 10);
  2432. \endverbatim
  2433. <P>
  2434. This will print completion messages at 10% increments; i.e., 10%, 20%, 30%,
  2435. etc., up to 100%. Each message does not appear on a new line, but rather erases
  2436. the previous message. After 100%, a new line is printed.
  2437. <P>
  2438. char * G_program_name() return module name
  2439. Routine returns the name of the module as set by the call to
  2440. <I>G_gisinit().</I>
  2441. <P>
  2442. char * G_whoami() user's name
  2443. Returns a pointer to a string which is the user's login name.
  2444. <P>
  2445. int G_yes() ask a yes/no question
  2446. This routine prints a <B>question</B> to the user, and expects the
  2447. user to respond either yes or no. (Invalid responses are rejected and
  2448. the process is repeated until the user answers yes or no.)
  2449. <P>
  2450. The <B>default</B> indicates what the RETURN key alone should mean. A
  2451. <B>default</B> of 1 indicates that RETURN means yes, 0 indicates that
  2452. RETURN means no, and -1 indicates that RETURN alone is not a valid
  2453. response.
  2454. <P>
  2455. The <B>question</B> will be appended with "(y/n) ", and, if
  2456. <B>default</B> is not -1, with "[y] " or "[n] ", depending on the
  2457. <B>default.</B>
  2458. <P>
  2459. <I>G_yes()</I> returns 1 if the user said yes, and 0 if the user said no.
  2460. <P>
  2461. \section GIS_Library_Data_Structures GIS Library Data Structures
  2462. <P>
  2463. Some of the data structures, defined in the "gis.h" header file and used
  2464. by routines in this library, are described in the sections below.
  2465. <P>
  2466. \subsection struct_Cell_head struct Cell_head
  2467. <P>
  2468. The raster header data structure is used for two purposes. It is used
  2469. for raster header information for map layers. It also used to hold
  2470. region values. The structure is:
  2471. \verbatim
  2472. struct Cell_head
  2473. {
  2474. int format; /* number of bytes per cell */
  2475. int compressed; /* compressed(1) or not compressed(0) */
  2476. int rows, cols; /* number of rows and columns */
  2477. int proj; /* projection */
  2478. int zone; /* zone */
  2479. double ew_res; /* east-west resolution */
  2480. double ns_res; /* north-south resolution */
  2481. double north; /* northern edge */
  2482. double south; /* southern edge */
  2483. double east; /* eastern edge */
  2484. double west; /* western edge */
  2485. };
  2486. \endverbatim
  2487. <P>
  2488. The <I>format</I> and <I>compressed</I> fields apply only to raster
  2489. headers. The <I>format</I> field describes the number of bytes per
  2490. raster data value and the <I>compressed</I> field indicates if the
  2491. raster file is compressed or not. The other fields apply both to
  2492. raster headers and regions. The geographic boundaries are described by
  2493. <I>north, south, east</I> and <I>west.</I> The grid resolution is
  2494. described by <I>ew_res</I> and <I>ns_res.</I> The cartographic
  2495. projection is described by <I>proj</I> and the related zone for the
  2496. projection by <I>zone.</I> The <I>rows</I> and <I>cols</I> indicate
  2497. the number of rows and columns in the raster file, or in the
  2498. region. See \ref Raster_Header_Format for more information about raster
  2499. headers, and \ref Region for more information about regions.
  2500. <P>
  2501. The routines described in \ref Raster_Header_File use this structure.
  2502. <P>
  2503. \subsection struct_Categories struct Categories
  2504. <P>
  2505. The <I>Categories</I> structure contains a title for the map layer,
  2506. the largest category in the map layer, an automatic label generation
  2507. rule for missing labels, and a list of category labels.
  2508. <P>
  2509. The structure is declared: <I>struct Categories</I> .
  2510. <P>
  2511. This structure should be accessed using the routines described in
  2512. Raster_Category_File.
  2513. <P>
  2514. \subsection struct_Colors struct Colors
  2515. <P>
  2516. The color data structure holds red, green, and blue color intensities
  2517. for raster categories. The structure has become so complicated that it
  2518. will not be described in this manual.
  2519. <P>
  2520. The structure is declared: <I>struct Colors</I> .
  2521. <P>
  2522. The routines described in Raster_Color_Table must be used
  2523. to store and retrieve color information using this structure.
  2524. <P>
  2525. \subsection struct_History struct History
  2526. <P>
  2527. The <I>History</I> structure is used to document raster files. The
  2528. information contained here is for the user. It is not used in any
  2529. operational way by GRASS. The structure is:
  2530. \verbatim
  2531. # define MAXEDLINES 50
  2532. # define RECORD_LEN 80
  2533. struct History
  2534. {
  2535. char mapid[RECORD_LEN];
  2536. char title[RECORD_LEN];
  2537. char mapset[RECORD_LEN];
  2538. char creator[RECORD_LEN];
  2539. char maptype[RECORD_LEN];
  2540. char datsrc_1[RECORD_LEN];
  2541. char datsrc_2[RECORD_LEN];
  2542. char keywrd[RECORD_LEN];
  2543. int edlinecnt;
  2544. char edhist[MAXEDLINES][RECORD_LEN];
  2545. };
  2546. \endverbatim
  2547. <P>
  2548. The <I>mapid</I> and <I>mapset</I> are the raster file name and
  2549. mapset, <I>title</I> is the raster file title, <I>creator</I> is the
  2550. user who created the file, <I>maptype</I> is the map type (which
  2551. should always be "raster"), <I>datasrc_1</I> and <I>datasrc_2</I>
  2552. describe the original data source, <I>keywrd</I> is a one-line data
  2553. description and <I>edhist</I> contains <I>edlinecnt</I> lines of user
  2554. comments.
  2555. <P>
  2556. The routines described in Raster_History_File use this structure.
  2557. However, there is very little support for manipulating the contents of this
  2558. structure. The programmer must manipulate the contents directly.
  2559. <P>
  2560. <B>Note.</B> Some of the information in this structure is not meaningful. For
  2561. example, if the raster file is renamed, or copied into another mapset, the
  2562. <I>mapid</I> and <I>mapset</I> will no longer be correct. Also the
  2563. <I>title</I> does not reflect the true raster file title. The true title is
  2564. maintained in the category file.
  2565. <P>
  2566. <B>Warning.</B> This structure has remained unchanged since the inception of
  2567. GRASS. There is a good possibility that it will be changed or eliminated in
  2568. future releases.
  2569. <P>
  2570. \subsection struct_Range struct Range
  2571. <P>
  2572. The <I>Range</I> structure contains the minimum and maximum values which occur
  2573. in a raster file.
  2574. <P>
  2575. The structure is declared: <I>struct Range</I> .
  2576. <P>
  2577. The routines described in Raster_Range_File should be used
  2578. to access this structure.
  2579. <P>
  2580. \section Loading_the_GIS_Library Loading the GIS Library
  2581. <P>
  2582. The library is loaded by specifying $(GISLIB) in the Makefile. The following
  2583. example is a complete Makefile which compiles code that uses this library:
  2584. <br>
  2585. <B>Makefile for $(GISLIB)}</B>
  2586. \verbatim
  2587. MODULE_TOPDIR = ../..
  2588. PGM = r.info
  2589. LIBES = $(GISLIB)
  2590. DEPENDENCIES = $(GISDEP)
  2591. include $(MODULE_TOPDIR)/include/Make/Module.make
  2592. default: cmd
  2593. \endverbatim
  2594. <P>
  2595. See \ref Compiling_and_Installing_GRASS_Modules for a complete discussion
  2596. of Makefiles.
  2597. <P>
  2598. \section TimeStamp_functions Timestamp functions
  2599. \verbatim
  2600. #include <grass/gis.h>
  2601. \endverbatim
  2602. <P>
  2603. This structure is defined in gis.h, but there should be no reason to access its
  2604. elements directly:
  2605. \verbatim
  2606. struct TimeStamp {
  2607. DateTime dt[2]; /* two datetimes */
  2608. int count;
  2609. };
  2610. \endverbatim
  2611. <P>
  2612. Using the G_*_timestamp() routines reads/writes a timestamp file in
  2613. the cell_misc/rastername or dig_misc/vectorname mapset element.
  2614. <P>
  2615. A TimeStamp can be one DateTime, or two DateTimes representing a
  2616. range. When preparing to write a TimeStamp, the programmer should
  2617. use one of:
  2618. <P>
  2619. <B>G_set_timestamp()</B> to set a single DateTime
  2620. <P>
  2621. <B>G_set_timestamp_range()</B> to set two DateTimes.
  2622. <P>
  2623. int G_read_raster_timestamp(char *name, char *mapset, struct TimeStamp
  2624. *ts) Read raster timestamp<br>
  2625. Returns 1 on success. 0 or negative on error.
  2626. <P>
  2627. int G_read_vector_timestamp(char *name, char *mapset, struct TimeStamp
  2628. *ts) Read vector timestamp<br>
  2629. Returns 1 on success. 0 or negative on error.
  2630. <P>
  2631. int G_get_timestamps(struct TimeStamp *ts, DateTime *dt1, DateTime
  2632. *dt2, int *count) copy TimeStamp into Datetimes
  2633. Use to copy the TimeStamp information into Datetimes, so the members
  2634. of struct TimeStamp shouldn't be accessed directly.
  2635. <br>
  2636. count=0 means no datetimes were copied
  2637. <br>
  2638. count=1 means 1 datetime was copied into dt1
  2639. <br>
  2640. count=2 means 2 datetimes were copied
  2641. <P>
  2642. int G_init_timestamp()
  2643. Sets ts-&gt;count = 0, to indicate no valid DateTimes are in TimeStamp.
  2644. <P>
  2645. int G_set_timestamp() Copies a
  2646. single DateTime to a TimeStamp in preparation for writing. (overwrites any
  2647. existing information in TimeStamp)
  2648. <P>
  2649. int G_set_timestamp_range()
  2650. Copies two DateTimes (a range) to a TimeStamp in preparation for writing.
  2651. (overwrites any existing information in TimeStamp)
  2652. <P>
  2653. int G_write_raster_timestamp()
  2654. Returns:
  2655. <br>
  2656. 1 on success.
  2657. <br>
  2658. -1 error - can't create timestamp file
  2659. <br>
  2660. -2 error - invalid datetime in ts
  2661. <P>
  2662. int G_write_vector_timestamp()
  2663. Returns:
  2664. <br>
  2665. 1 on success.
  2666. <br>
  2667. -1 error - can't create timestamp file
  2668. <br>
  2669. -2 error - invalid datetime in ts
  2670. <P>
  2671. int G_format_timestamp()
  2672. Returns:
  2673. <br>
  2674. 1 on success
  2675. <br>
  2676. -1 error
  2677. <P>
  2678. int G_scan_timestamp()
  2679. Returns:
  2680. <br>
  2681. 1 on success
  2682. <br>
  2683. -1 error
  2684. <P>
  2685. int G_remove_raster_timestamp()
  2686. Only timestamp files in current mapset can be removed
  2687. <P>
  2688. Returns:
  2689. <br>
  2690. 0 if no file
  2691. <br>
  2692. 1 if successful
  2693. <br>
  2694. -1 on fail
  2695. <P>
  2696. int G_remove_vector_timestamp()
  2697. Only timestamp files in current mapset can be removed
  2698. <P>
  2699. Returns:
  2700. <br>
  2701. 0 if no file
  2702. <br>
  2703. 1 if successful
  2704. <br>
  2705. -1 on fail
  2706. <P>
  2707. int G_read_grid3_timestamp() read grid3 timestamp
  2708. Returns 1 on success. 0 or negative on error.
  2709. <P>
  2710. int G_remove_grid3_timestamp() remove grid3
  2711. timestamp
  2712. Only timestamp files in current mapset can be removed
  2713. <P>
  2714. Returns:
  2715. <br>
  2716. 0 if no file
  2717. <br>
  2718. 1 if successful
  2719. <br>
  2720. -1 on fail
  2721. <P>
  2722. int G_write_grid3_timestamp() write grid3 timestamp
  2723. Returns:
  2724. <br>
  2725. 1 on success.
  2726. <br>
  2727. -1 error - can't create timestamp file
  2728. <br>
  2729. -2 error - invalid datetime in ts
  2730. <P>
  2731. See \ref DateTime_Library for a complete discussion of GRASS datetime
  2732. routines.
  2733. <P>
  2734. \section GRASS_GIS_Library_Overview GRASS GIS Library Overview
  2735. <P>
  2736. Contents of directory lib/:
  2737. <P>
  2738. <PRE>
  2739. bitmap : bitmap library for X Window Bitmaps
  2740. btree : binary tree library
  2741. bwidget : tcl/tk extra library
  2742. cdhc : library for testing normality and exponentiality
  2743. D : display library
  2744. datetime : DateTime library
  2745. db : database management interface database drivers + SQL parser
  2746. display : library for CELL driver
  2747. dspf : G3D display files library
  2748. edit : edit library
  2749. external : external libraries from other projects (shapelib)
  2750. fonts : Hershey fonts
  2751. form : forms library
  2752. front.end : interface for interactive modules
  2753. g3d : G3D raster volume library
  2754. gis : main GRASS library
  2755. gmath : generic mathematical functions (matrix, fft etc.)
  2756. (later to be extended, BLAS/LAPACK library wrapper)
  2757. gpde : partial differential equations library
  2758. gtcltk : Tcl/Tk stuff
  2759. image : extra imagery library (image3)
  2760. imagery : imagery library
  2761. init : GRASS initialization code + scripts
  2762. linkm : linked list memory manager
  2763. ogsf : openGL library (required for NVIZ)
  2764. proj : PROJ4 projection library wrapper
  2765. raster : GRASS raster display library (raster map functions are in gis/)
  2766. rowio : row in/out library
  2767. rst : library for interpolation with regularized splines with tension
  2768. segment : segment library
  2769. sites : old sites library (deprecated), now interfaced to vect library
  2770. symbol : drawing symbols for point vector data
  2771. vask : Curses management library
  2772. vect : GRASS vector and Direct Graph library
  2773. </PRE>
  2774. */