shpadd.c 7.98 KB
Newer Older
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241
/******************************************************************************
 * $Id: shpadd.c,v 1.18 2016-12-05 12:44:05 erouault Exp $
 *
 * Project:  Shapelib
 * Purpose:  Sample application for adding a shape to a shapefile.
 * Author:   Frank Warmerdam, warmerdam@pobox.com
 *
 ******************************************************************************
 * Copyright (c) 1999, Frank Warmerdam
 *
 * This software is available under the following "MIT Style" license,
 * or at the option of the licensee under the LGPL (see COPYING).  This
 * option is discussed in more detail in shapelib.html.
 *
 * --
 * 
 * Permission is hereby granted, free of charge, to any person obtaining a
 * copy of this software and associated documentation files (the "Software"),
 * to deal in the Software without restriction, including without limitation
 * the rights to use, copy, modify, merge, publish, distribute, sublicense,
 * and/or sell copies of the Software, and to permit persons to whom the
 * Software is furnished to do so, subject to the following conditions:
 *
 * The above copyright notice and this permission notice shall be included
 * in all copies or substantial portions of the Software.
 *
 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS
 * OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
 * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
 * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
 * DEALINGS IN THE SOFTWARE.
 ******************************************************************************
 *
 * $Log: shpadd.c,v $
 * Revision 1.18  2016-12-05 12:44:05  erouault
 * * Major overhaul of Makefile build system to use autoconf/automake.
 *
 * * Warning fixes in contrib/
 *
 * Revision 1.17  2016-12-04 15:30:15  erouault
 * * shpopen.c, dbfopen.c, shptree.c, shapefil.h: resync with
 * GDAL Shapefile driver. Mostly cleanups. SHPObject and DBFInfo
 * structures extended with new members. New functions:
 * DBFSetLastModifiedDate, SHPOpenLLEx, SHPRestoreSHX,
 * SHPSetFastModeReadObject
 *
 * * sbnsearch.c: new file to implement original ESRI .sbn spatial
 * index reading. (no write support). New functions:
 * SBNOpenDiskTree, SBNCloseDiskTree, SBNSearchDiskTree,
 * SBNSearchDiskTreeInteger, SBNSearchFreeIds
 *
 * * Makefile, makefile.vc, CMakeLists.txt, shapelib.def: updates
 * with new file and symbols.
 *
 * * commit: helper script to cvs commit
 *
 * Revision 1.16  2010-06-21 20:41:52  fwarmerdam
 * reformat white space
 *
 * Revision 1.15  2007-12-30 16:57:32  fwarmerdam
 * add support for z and m
 *
 * Revision 1.14  2004/09/26 20:09:35  fwarmerdam
 * avoid rcsid warnings
 *
 * Revision 1.13  2002/01/15 14:36:07  warmerda
 * updated email address
 *
 * Revision 1.12  2001/05/31 19:35:29  warmerda
 * added support for writing null shapes
 *
 * Revision 1.11  2000/07/07 13:39:45  warmerda
 * removed unused variables, and added system include files
 *
 * Revision 1.10  2000/05/24 15:09:22  warmerda
 * Added logic to graw vertex lists of needed.
 *
 * Revision 1.9  1999/11/05 14:12:04  warmerda
 * updated license terms
 *
 * Revision 1.8  1998/12/03 16:36:26  warmerda
 * Use r+b rather than rb+ for binary access.
 *
 * Revision 1.7  1998/11/09 20:57:04  warmerda
 * Fixed SHPGetInfo() call.
 *
 * Revision 1.6  1998/11/09 20:19:16  warmerda
 * Changed to use SHPObject based API.
 *
 * Revision 1.5  1997/03/06 14:05:02  warmerda
 * fixed typo.
 *
 * Revision 1.4  1997/03/06 14:01:16  warmerda
 * added memory allocation checking, and free()s.
 *
 * Revision 1.3  1995/10/21 03:14:37  warmerda
 * Changed to use binary file access
 *
 * Revision 1.2  1995/08/04  03:18:01  warmerda
 * Added header.
 *
 */

#include <stdlib.h>
#include <string.h>
#include "shapefil.h"

SHP_CVSID("$Id: shpadd.c,v 1.18 2016-12-05 12:44:05 erouault Exp $")

int main( int argc, char ** argv )

{
    SHPHandle	hSHP;
    int		nShapeType, nVertices, nParts, *panParts, i, nVMax;
    double	*padfX, *padfY, *padfZ = NULL, *padfM = NULL;
    SHPObject	*psObject;
    const char  *tuple = "";
    const char  *filename;

/* -------------------------------------------------------------------- */
/*      Display a usage message.                                        */
/* -------------------------------------------------------------------- */
    if( argc < 2 )
    {
        printf( "shpadd shp_file [[x y] [+]]*\n" );
        printf( "  or\n" );
        printf( "shpadd shp_file -m [[x y m] [+]]*\n" );
        printf( "  or\n" );
        printf( "shpadd shp_file -z [[x y z] [+]]*\n" );
        printf( "  or\n" );
        printf( "shpadd shp_file -zm [[x y z m] [+]]*\n" );
        exit( 1 );
    }

    filename = argv[1];
    argv++;
    argc--;

/* -------------------------------------------------------------------- */
/*      Check for tuple description options.                            */
/* -------------------------------------------------------------------- */
    if( argc > 1 
        && (strcmp(argv[1],"-z") == 0 
            || strcmp(argv[1],"-m") == 0 
            || strcmp(argv[1],"-zm") == 0) )
    {
        tuple = argv[1] + 1;
        argv++;
        argc--;
    }

/* -------------------------------------------------------------------- */
/*      Open the passed shapefile.                                      */
/* -------------------------------------------------------------------- */
    hSHP = SHPOpen( filename, "r+b" );

    if( hSHP == NULL )
    {
        printf( "Unable to open:%s\n", filename );
        exit( 1 );
    }

    SHPGetInfo( hSHP, NULL, &nShapeType, NULL, NULL );

    if( argc == 1 )
        nShapeType = SHPT_NULL;

/* -------------------------------------------------------------------- */
/*	Build a vertex/part list from the command line arguments.	*/
/* -------------------------------------------------------------------- */
    nVMax = 1000;
    padfX = (double *) malloc(sizeof(double) * nVMax);
    padfY = (double *) malloc(sizeof(double) * nVMax);

    if( strchr(tuple,'z') )
        padfZ = (double *) malloc(sizeof(double) * nVMax);
    if( strchr(tuple,'m') )
        padfM = (double *) malloc(sizeof(double) * nVMax);
    
    nVertices = 0;

    if( (panParts = (int *) malloc(sizeof(int) * 1000 )) == NULL )
    {
        printf( "Out of memory\n" );
        exit( 1 );
    }
    
    nParts = 1;
    panParts[0] = 0;

    for( i = 1; i < argc;  )
    {
        if( argv[i][0] == '+' )
        {
            panParts[nParts++] = nVertices;
            i++;
        }
        else if( i < argc-1-(int)strlen(tuple) )
        {
            if( nVertices == nVMax )
            {
                nVMax = nVMax * 2;
                padfX = (double *) realloc(padfX,sizeof(double)*nVMax);
                padfY = (double *) realloc(padfY,sizeof(double)*nVMax);
                if( padfZ )
                    padfZ = (double *) realloc(padfZ,sizeof(double)*nVMax);
                if( padfM )
                    padfM = (double *) realloc(padfM,sizeof(double)*nVMax);
            }

            sscanf( argv[i++], "%lg", padfX+nVertices );
            sscanf( argv[i++], "%lg", padfY+nVertices );
            if( padfZ )
                sscanf( argv[i++], "%lg", padfZ+nVertices );
            if( padfM )
                sscanf( argv[i++], "%lg", padfM+nVertices );
                
            nVertices += 1;
        }
    }

/* -------------------------------------------------------------------- */
/*      Write the new entity to the shape file.                         */
/* -------------------------------------------------------------------- */
    psObject = SHPCreateObject( nShapeType, -1, nParts, panParts, NULL,
                                nVertices, padfX, padfY, padfZ, padfM );
    SHPWriteObject( hSHP, -1, psObject );
    SHPDestroyObject( psObject );
    
    SHPClose( hSHP );

    free( panParts );
    free( padfX );
    free( padfY );
    free( padfZ );
    free( padfM );

    return 0;
}