gislib.dox 116 KB

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