shptree.c 5.63 KB
Newer Older
1
/******************************************************************************
2
 * $Id$
3 4 5 6 7 8 9 10 11 12 13 14 15 16 17
 *
 * Project:  MapServer
 * Purpose:  Commandline utility to generate .qix shapefile spatial indexes.
 * Author:   Steve Lime and the MapServer team.
 *
 ******************************************************************************
 * Copyright (c) 1996-2005 Regents of the University of Minnesota.
 *
 * 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:
 *
18
 * The above copyright notice and this permission notice shall be included in
19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34
 * all copies of this Software or works derived from this 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.
 ****************************************************************************/

#include "mapserver.h"
#include "maptree.h"
#include <string.h>


35 36 37 38 39 40

char* AddFileSuffix ( const char * Filename, const char * Suffix )
{
  char  *pszFullname, *pszBasename;
  int i;

41
  /* -------------------------------------------------------------------- */
42 43
  /*  Compute the base (layer) name.  If there is any extension     */
  /*  on the passed in filename we will strip it off.         */
44
  /* -------------------------------------------------------------------- */
45
  pszBasename = (char *) msSmallMalloc(strlen(Filename)+5);
46
  strcpy( pszBasename, Filename );
47
  for( i = strlen(pszBasename)-1;
48
       i > 0 && pszBasename[i] != '.' && pszBasename[i] != '/'
49
       && pszBasename[i] != '\\';
50
       i-- ) {}
51

52 53
  if( pszBasename[i] == '.' )
    pszBasename[i] = '\0';
54

55
  /* -------------------------------------------------------------------- */
56 57
  /*  Open the .shp and .shx files.  Note that files pulled from      */
  /*  a PC to Unix with upper case filenames won't work!        */
58
  /* -------------------------------------------------------------------- */
59
  pszFullname = (char *) msSmallMalloc(strlen(pszBasename) + 5);
60 61
  sprintf( pszFullname, "%s%s", pszBasename, Suffix);

62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80
  free(pszBasename);
  return (pszFullname);
}


int main(int argc, char *argv[])
{
  shapefileObj shapefile;

  treeObj *tree;
  int byte_order = MS_NEW_LSB_ORDER, i;
  int depth=0;

  if(argc > 1 && strcmp(argv[1], "-v") == 0) {
    printf("%s\n", msGetVersion());
    exit(0);
  }

  /* -------------------------------------------------------------------- */
81
  /*  Establish the byte order on this machine to decide default        */
82 83
  /*    index format                                                      */
  /* -------------------------------------------------------------------- */
84 85 86 87 88
  i = 1;
  if( *((uchar *) &i) == 1 )
    byte_order = MS_NEW_LSB_ORDER;
  else
    byte_order = MS_NEW_MSB_ORDER;
89 90 91


  if(argc<2) {
92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108
    fprintf(stdout,"Syntax:\n");
    fprintf(stdout,"    shptree <shpfile> [<depth>] [<index_format>]\n" );
    fprintf(stdout,"Where:\n");
    fprintf(stdout," <shpfile> is the name of the .shp file to index.\n");
    fprintf(stdout," <depth>   (optional) is the maximum depth of the index\n");
    fprintf(stdout,"           to create, default is 0 meaning that shptree\n");
    fprintf(stdout,"           will calculate a reasonable default depth.\n");
    fprintf(stdout," <index_format> (optional) is one of:\n");
    fprintf(stdout,"           NL: LSB byte order, using new index format\n");
    fprintf(stdout,"           NM: MSB byte order, using new index format\n");
    fprintf(stdout,"       The following old format options are deprecated:\n");
    fprintf(stdout,"           N:  Native byte order\n");
    fprintf(stdout,"           L:  LSB (intel) byte order\n");
    fprintf(stdout,"           M:  MSB byte order\n");
    fprintf(stdout,"       The default index_format on this system is: %s\n\n",
            (byte_order == MS_NEW_LSB_ORDER) ? "NL" : "NM" );
    exit(0);
109 110 111 112 113
  }

  if(argc >= 3)
    depth = atoi(argv[2]);

114
  if(argc >= 4) {
115
    if( !strcasecmp(argv[3],"N" ))
116
      byte_order = MS_NATIVE_ORDER;
117
    if( !strcasecmp(argv[3],"L" ))
118
      byte_order = MS_LSB_ORDER;
119
    if( !strcasecmp(argv[3],"M" ))
120
      byte_order = MS_MSB_ORDER;
121
    if( !strcasecmp(argv[3],"NL" ))
122
      byte_order = MS_NEW_LSB_ORDER;
123
    if( !strcasecmp(argv[3],"NM" ))
124
      byte_order = MS_NEW_MSB_ORDER;
125
  }
126

127 128 129 130 131
  if(msShapefileOpen(&shapefile, "rb", argv[1], MS_TRUE) == -1) {
    fprintf(stdout, "Error opening shapefile %s.\n", argv[1]);
    exit(0);
  }

132 133 134
  printf( "creating index of %s %s format\n",(byte_order < 1 ? "old (deprecated)" :"new"),
          ((byte_order == MS_NATIVE_ORDER) ? "native" :
           ((byte_order == MS_LSB_ORDER) || (byte_order == MS_NEW_LSB_ORDER)? " LSB":"MSB")));
135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155

  tree = msCreateTree(&shapefile, depth);
  if(!tree) {
#if MAX_SUBNODE == 2
    fprintf(stdout, "Error generating binary tree.\n");
#else
    fprintf(stdout, "Error generating quadtree.\n");
#endif
    exit(0);
  }

  msWriteTree(tree, AddFileSuffix(argv[1], MS_INDEX_EXTENSION), byte_order);
  msDestroyTree(tree);

  /*
  ** Clean things up
  */
  msShapefileClose(&shapefile);

  return(0);
}