~ubuntu-branches/ubuntu/vivid/grass/vivid-proposed

« back to all changes in this revision

Viewing changes to lib/gis/make_loc.c

  • Committer: Package Import Robot
  • Author(s): Bas Couwenberg
  • Date: 2015-02-20 23:12:08 UTC
  • mfrom: (8.2.6 experimental)
  • Revision ID: package-import@ubuntu.com-20150220231208-1u6qvqm84v430b10
Tags: 7.0.0-1~exp1
* New upstream release.
* Update python-ctypes-ternary.patch to use if/else instead of and/or.
* Drop check4dev patch, rely on upstream check.
* Add build dependency on libpq-dev to grass-dev for libpq-fe.h.
* Drop patches applied upstream, refresh remaining patches.
* Update symlinks for images switched from jpg to png.

Show diffs side-by-side

added added

removed removed

Lines of Context:
1
 
 
2
 
/******************************************************************************
3
 
 *
4
 
 * Project:  libgrass
5
 
 * Purpose:  Function to create a new location automatically given a 
6
 
 *           "Cell_head", PROJ_INFO and PROJ_UNITS information.
7
 
 * Author:   Frank Warmerdam, warmerda@pobox.com
8
 
 *
9
 
 ******************************************************************************
10
 
 * Copyright (c) 2000, Frank Warmerdam
11
 
 *
12
 
 * This library is free software; you can redistribute it and/or
13
 
 * modify it under the terms of the GNU Library General Public
14
 
 * License as published by the Free Software Foundation; either
15
 
 * version 2 of the License, or (at your option) any later version.
16
 
 *
17
 
 * This library is distributed in the hope that it will be useful,
18
 
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
19
 
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
20
 
 * Library General Public License for more details.
21
 
 *
22
 
 * You should have received a copy of the GNU Library General Public
23
 
 * License along with this library; if not, write to the
24
 
 * Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
25
 
 ******************************************************************************
26
 
 *
 
1
/*!
 
2
 * \file lib/gis/make_loc.c
 
3
 *
 
4
 * \brief GIS Library - Functions to create a new location
 
5
 *
 
6
 * Creates a new location automatically given a "Cell_head", PROJ_INFO
 
7
 * and PROJ_UNITS information.
 
8
 *
 
9
 * (C) 2000-2013 by the GRASS Development Team
 
10
 *
 
11
 * This program is free software under the GNU General Public License
 
12
 * (>=v2). Read the file COPYING that comes with GRASS for details.
 
13
 *
 
14
 * \author Frank Warmerdam
27
15
 */
28
16
 
29
17
#include <grass/gis.h>
34
22
#include <sys/stat.h>
35
23
#include <math.h>
36
24
 
37
 
/*
38
 
 * Returns 0 on success.
39
 
 * Returns -1 to indicate a system error (check errno).
 
25
/*!
 
26
 * \brief Create a new location
 
27
 * 
 
28
 * This function creates a new location in the current database,
 
29
 * initializes the projection, default window and current window.
 
30
 *
 
31
 * \param location_name Name of the new location. Should not include
 
32
 *                      the full path, the location will be created within
 
33
 *                      the current database.
 
34
 * \param wind          default window setting for the new location.
 
35
 *                      All fields should be set in this
 
36
 *                      structure, and care should be taken to ensure that
 
37
 *                      the proj/zone fields match the definition in the
 
38
 *                      proj_info parameter(see G_set_cellhd_from_projinfo()).
 
39
 *
 
40
 * \param proj_info     projection definition suitable to write to the
 
41
 *                      PROJ_INFO file, or NULL for PROJECTION_XY.
 
42
 *
 
43
 * \param proj_units    projection units suitable to write to the PROJ_UNITS
 
44
 *                      file, or NULL.
 
45
 *
 
46
 * \return 0 on success
 
47
 * \return -1 to indicate a system error (check errno).
 
48
 * \return -2 failed to create projection file (currently not used)
 
49
 * \return -3 illegal name 
40
50
 */
41
 
 
42
 
 
43
 
int G__make_location(const char *location_name,
44
 
                     struct Cell_head *wind,
45
 
                     struct Key_Value *proj_info,
46
 
                     struct Key_Value *proj_units, FILE * report_file)
 
51
int G_make_location(const char *location_name,
 
52
                    struct Cell_head *wind,
 
53
                    const struct Key_Value *proj_info,
 
54
                    const struct Key_Value *proj_units)
47
55
{
48
56
    char path[GPATH_MAX];
49
 
    int out_stat;
 
57
 
 
58
    /* check if location name is legal */
 
59
    if (G_legal_filename(location_name) != 1)
 
60
        return -3;
50
61
 
51
62
    /* Try to create the location directory, under the gisdbase. */
52
63
    sprintf(path, "%s/%s", G_gisdbase(), location_name);
55
66
 
56
67
    /* Make the PERMANENT mapset. */
57
68
    sprintf(path, "%s/%s/%s", G_gisdbase(), location_name, "PERMANENT");
58
 
    if (G_mkdir(path) != 0)
 
69
    if (G_mkdir(path) != 0) {
59
70
        return -1;
 
71
    }
60
72
 
61
73
    /* make these the new current location and mapset */
62
 
    G__setenv("LOCATION_NAME", location_name);
63
 
    G__setenv("MAPSET", "PERMANENT");
 
74
    G_setenv_nogisrc("LOCATION_NAME", location_name);
 
75
    G_setenv_nogisrc("MAPSET", "PERMANENT");
64
76
 
65
77
    /* Create the default, and current window files */
66
 
    G__put_window(wind, "", "DEFAULT_WIND");
67
 
    G__put_window(wind, "", "WIND");
 
78
    G_put_element_window(wind, "", "DEFAULT_WIND");
 
79
    G_put_element_window(wind, "", "WIND");
68
80
 
69
81
    /* Write out the PROJ_INFO, and PROJ_UNITS if available. */
70
82
    if (proj_info != NULL) {
71
 
        G__file_name(path, "", "PROJ_INFO", "PERMANENT");
72
 
        G_write_key_value_file(path, proj_info, &out_stat);
73
 
        if (out_stat != 0)
74
 
            return -2;
 
83
        G_file_name(path, "", "PROJ_INFO", "PERMANENT");
 
84
        G_write_key_value_file(path, proj_info);
75
85
    }
76
86
 
77
87
    if (proj_units != NULL) {
78
 
        G__file_name(path, "", "PROJ_UNITS", "PERMANENT");
79
 
        G_write_key_value_file(path, proj_units, &out_stat);
80
 
        if (out_stat != 0)
81
 
            return -2;
 
88
        G_file_name(path, "", "PROJ_UNITS", "PERMANENT");
 
89
        G_write_key_value_file(path, proj_units);
82
90
    }
83
91
 
84
92
    return 0;
85
93
}
86
94
 
87
 
 
88
 
/*!
89
 
 * \brief  create a new location
90
 
 * 
91
 
 * This function creates a new location in the current database,
92
 
 * initializes the projection, default window and current window.  
93
 
 *
94
 
 * \param location_name
95
 
 *                      The name of the new location.  Should not include
96
 
 *                      the full path, the location will be created within
97
 
 *                      the current database.
98
 
 * \param wind
99
 
 *                      Contains the default window setting for the
100
 
 *                      new location.  All fields should be set in this
101
 
 *                      structure, and care should be taken to ensure that
102
 
 *                      the proj/zone fields match the definition in the
103
 
 *                      proj_info parameter (see G_set_cellhd_from_projinfo()).
104
 
 *
105
 
 * \param proj_info
106
 
 *                      Projection definition suitable to write to the
107
 
 *                      PROJ_INFO file, or NULL for PROJECTION_XY.
108
 
 *
109
 
 * \param proj_units
110
 
 *                      Projection units suitable to write to the PROJ_UNITS
111
 
 *                      file, or NULL.
112
 
 *
113
 
 * \param report_file 
114
 
 *                      File to which creation information should be written
115
 
 *                      (can be stdout).  Currently not used.
116
 
 *
117
 
 * \return Returns 0 on success, or generates a fatal error on failure.  
118
 
 *         The G__make_location() function operates the same, but returns a
119
 
 *         non-zero error code on failure, instead of terminating. 
120
 
 */
121
 
 
122
 
int G_make_location(const char *location_name,
123
 
                    struct Cell_head *wind,
124
 
                    struct Key_Value *proj_info,
125
 
                    struct Key_Value *proj_units, FILE * report_file)
126
 
{
127
 
    int err;
128
 
 
129
 
    err = G__make_location(location_name, wind, proj_info, proj_units,
130
 
                           report_file);
131
 
 
132
 
    if (err == 0)
133
 
        return 0;
134
 
 
135
 
    if (err == -1) {
136
 
        perror("G_make_location");
137
 
    }
138
 
 
139
 
    G_fatal_error("G_make_location failed.");
140
 
 
141
 
    return 1;
142
 
}
143
 
 
144
 
 
145
 
/************************************************************************/
146
 
/*                       G_compare_projections()                        */
147
 
 
148
 
/************************************************************************/
149
 
 
150
 
/*!
151
 
 * \brief compare projections
152
 
 *
153
 
 *  \param proj_info1
154
 
 *  \param proj_units1
155
 
 *  \param proj_info2
156
 
 *  \param proj_units2
157
 
 *  \return -1 if not the same projection, -2 if linear unit translation to 
158
 
 *          meters fails, -4 if not the same ellipsoid,
159
 
 *          -5 if UTM zone differs, -6 if UTM hemisphere differs,
160
 
 *          -7 if false easting differs, -8 if false northing differs,
161
 
 *          else TRUE if projections match.
162
 
 *          
163
 
 */
164
 
 
165
 
int
166
 
G_compare_projections(const struct Key_Value *proj_info1,
167
 
                      const struct Key_Value *proj_units1,
168
 
                      const struct Key_Value *proj_info2,
169
 
                      const struct Key_Value *proj_units2)
 
95
/*!
 
96
 * \brief Compare projections including units
 
97
 *
 
98
 *  \param proj_info1   projection info to compare
 
99
 *  \param proj_units1  projection units to compare
 
100
 *  \param proj_info2   projection info to compare
 
101
 *  \param proj_units2  projection units to compare
 
102
 
 
103
 *  \return -1 if not the same projection
 
104
 *  \return -2 if linear unit translation to meters fails
 
105
 *  \return -4 if not the same ellipsoid,
 
106
 *  \return -5 if UTM zone differs
 
107
 *  \return -6 if UTM hemisphere differs,
 
108
 *  \return -7 if false easting differs
 
109
 *  \return -8 if false northing differs,
 
110
 *  \return 1  if projections match.
 
111
 */
 
112
int G_compare_projections(const struct Key_Value *proj_info1,
 
113
                          const struct Key_Value *proj_units1,
 
114
                          const struct Key_Value *proj_info2,
 
115
                          const struct Key_Value *proj_units2)
170
116
{
171
117
    const char *proj1, *proj2;
172
118
 
191
137
    /* -------------------------------------------------------------------- */
192
138
    /* prevent seg fault in G_find_key_value */
193
139
    if (proj_units1 == NULL && proj_units2 == NULL)
194
 
        return TRUE;
 
140
        return 1;
195
141
 
196
142
    if (proj_units1 == NULL || proj_units2 == NULL)
197
143
        return -2;
246
192
    /* -------------------------------------------------------------------- */
247
193
 
248
194
    {
249
 
        char *x_0_1 = NULL, *x_0_2 = NULL;
 
195
        const char *x_0_1 = NULL, *x_0_2 = NULL;
250
196
 
251
197
        x_0_1 = G_find_key_value("x_0", proj_info1);
252
198
        x_0_2 = G_find_key_value("x_0", proj_info2);
260
206
    /* -------------------------------------------------------------------- */
261
207
 
262
208
    {
263
 
        char *y_0_1 = NULL, *y_0_2 = NULL;
 
209
        const char *y_0_1 = NULL, *y_0_2 = NULL;
264
210
 
265
211
        y_0_1 = G_find_key_value("y_0", proj_info1);
266
212
        y_0_2 = G_find_key_value("y_0", proj_info2);
273
219
    /*      Add more details in later.                                      */
274
220
    /* -------------------------------------------------------------------- */
275
221
 
276
 
    return TRUE;
 
222
    return 1;
277
223
}