GRASS Programmer's Manual
6.4.2(2012)
|
00001 #include <string.h> 00002 #include <stdlib.h> 00003 #include <stdio.h> 00004 #include <sys/types.h> 00005 #include <sys/stat.h> 00006 #include <grass/gis.h> 00007 #include "local_proto.h" 00008 00009 int make_location(const char *gisdbase, const char *location_name) 00010 { 00011 struct Cell_head window; 00012 char buf[GPATH_MAX]; 00013 int i; 00014 char myname[75]; 00015 char *mapset; 00016 char *name, c; 00017 FILE *fp; 00018 00019 G_clear_screen(); 00020 fprintf(stderr, 00021 "To create a new LOCATION, you will need the following information:\n"); 00022 fprintf(stderr, "\n"); 00023 fprintf(stderr, "1. The coordinate system for the database\n"); 00024 /*for (i = 1; name = G__projection_name(i); i++) */ 00025 fprintf(stderr, " %s (for imagery and other unreferenced data)\n", 00026 G__projection_name(PROJECTION_XY)); 00027 fprintf(stderr, " %s\n", G__projection_name(PROJECTION_LL)); 00028 fprintf(stderr, " %s\n", G__projection_name(PROJECTION_UTM)); 00029 fprintf(stderr, " %s\n", G__projection_name(PROJECTION_OTHER)); 00030 fprintf(stderr, "2. The zone for the %s database\n", 00031 G__projection_name(PROJECTION_UTM)); 00032 fprintf(stderr, 00033 " and all the necessary parameters for projections other than\n"); 00034 fprintf(stderr, " %s, %s, and %s\n", G__projection_name(PROJECTION_LL), 00035 G__projection_name(PROJECTION_XY), 00036 G__projection_name(PROJECTION_UTM)); 00037 fprintf(stderr, 00038 "3. The coordinates of the area to become the default region\n"); 00039 fprintf(stderr, " and the grid resolution of this region\n"); 00040 fprintf(stderr, 00041 "4. A short, one-line description or title for the location\n"); 00042 fprintf(stderr, "\n"); 00043 00044 if (!G_yes("Do you have all this information? ", 1)) 00045 return 0; 00046 00047 G_zero(&window, sizeof(window)); 00048 while (1) { 00049 G_clear_screen(); 00050 fprintf(stderr, 00051 "Please specify the coordinate system for location <%s>\n\n", 00052 location_name); 00053 fprintf(stderr, "A %s\n", G__projection_name(PROJECTION_XY)); 00054 fprintf(stderr, "B %s\n", G__projection_name(PROJECTION_LL)); 00055 fprintf(stderr, "C %s\n", G__projection_name(PROJECTION_UTM)); 00056 fprintf(stderr, "D %s\n", G__projection_name(PROJECTION_OTHER)); 00057 fprintf(stderr, "RETURN to cancel\n"); 00058 fprintf(stderr, "\n"); 00059 fprintf(stderr, "> "); 00060 if (!G_gets(buf)) 00061 continue; 00062 G_strip(buf); 00063 if (*buf == 0) 00064 return 0; 00065 if (sscanf(buf, "%c", &c) != 1) 00066 continue; 00067 switch (c) { 00068 case 'A': 00069 i = PROJECTION_XY; 00070 break; 00071 case 'a': 00072 i = PROJECTION_XY; 00073 break; 00074 case 'B': 00075 i = PROJECTION_LL; 00076 break; 00077 case 'b': 00078 i = PROJECTION_LL; 00079 break; 00080 case 'C': 00081 i = PROJECTION_UTM; 00082 break; 00083 case 'c': 00084 i = PROJECTION_UTM; 00085 break; 00086 case 'D': 00087 i = PROJECTION_OTHER; 00088 break; 00089 case 'd': 00090 i = PROJECTION_OTHER; 00091 break; 00092 default: 00093 continue; 00094 } 00095 name = G__projection_name(i); 00096 if (name == NULL) 00097 continue; 00098 fprintf(stderr, "\n"); 00099 sprintf(buf, "\n%s coordinate system? ", name); 00100 if (G_yes(buf, 1)) 00101 break; 00102 } 00103 window.proj = i; 00104 /* 00105 while (window.proj == PROJECTION_UTM) 00106 { 00107 fprintf (stderr, " ("Please specify the %s zone for location <%s>\n", 00108 G__projection_name(window.proj), location_name); 00109 fprintf (stderr, " ("or RETURN to cancel\n"); 00110 fprintf (stderr, " ("\n"); 00111 fprintf (stderr, " ("> "); 00112 if (!G_gets(buf)) 00113 continue; 00114 G_strip (buf); 00115 if (*buf == 0) return 0; 00116 if (sscanf (buf, "%d", &i) != 1) 00117 continue; 00118 if (i > 0 && i <= 60) 00119 { 00120 fprintf(stderr, "zone %d is illegal"\n); 00121 fprintf (stderr, " ("\n"); 00122 sprintf (buf, "zone %d? ", i); 00123 if (G_yes (buf, 1)) 00124 { 00125 window.zone = i; 00126 break; 00127 } 00128 } 00129 */ 00130 00131 while (1) { 00132 G_clear_screen(); 00133 fprintf(stderr, 00134 "Please enter a one line description for location <%s>\n\n", 00135 location_name); 00136 fprintf(stderr, "> "); 00137 if (!G_gets(buf)) 00138 continue; 00139 G_squeeze(buf); 00140 buf[sizeof(myname)] = 0; 00141 G_squeeze(buf); 00142 fprintf(stderr, 00143 "=====================================================\n"); 00144 fprintf(stderr, "%s\n", buf); 00145 fprintf(stderr, 00146 "=====================================================\n"); 00147 if (G_yes("ok? ", *buf != 0)) 00148 break; 00149 } 00150 strcpy(myname, buf); 00151 /* 00152 if(G_edit_cellhd(&window, -1) < 0) 00153 return 0; 00154 */ 00155 00156 mapset = "PERMANENT"; 00157 G__setenv("MAPSET", mapset); 00158 G__setenv("LOCATION_NAME", location_name); 00159 00160 sprintf(buf, "%s/%s", gisdbase, location_name); 00161 if (G_mkdir(buf) < 0) 00162 return 0; 00163 sprintf(buf, "%s/%s/%s", gisdbase, location_name, mapset); 00164 if (G_mkdir(buf) < 0) 00165 return 0; 00166 /* set the dummy window */ 00167 window.north = 1.; 00168 window.south = 0.; 00169 window.top = 1.; 00170 window.bottom = 0.; 00171 window.rows = 1; 00172 window.rows3 = 1; 00173 window.cols = 1; 00174 window.cols3 = 1; 00175 window.depths = 1; 00176 window.ew_res = 1.; 00177 window.ew_res3 = 1.; 00178 window.ns_res = 1.; 00179 window.ns_res3 = 1.; 00180 window.tb_res = 1.; 00181 window.east = 1.; 00182 window.west = 0.; 00183 window.zone = 0.; 00184 /* make a dummy default window for location */ 00185 /* later after calling g.setrpj we will let user create a real default window */ 00186 G__put_window(&window, "", "DEFAULT_WIND"); 00187 G__put_window(&window, "", "WIND"); 00188 00189 sprintf(buf, "%s/%s/%s/MYNAME", gisdbase, location_name, mapset); 00190 fp = fopen(buf, "w"); 00191 fputs(myname, fp); 00192 fclose(fp); 00193 00194 return 1; 00195 }