/****************************************************************************** * * Project: MapServer * Purpose: Commandline utility to ... sort shapes? Not sure what its for. * 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: * * The above copyright notice and this permission notice shall be included in * 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. ****************************************************************************** * * $Log: sortshp.c,v $ * Revision 1.6 2005/07/15 13:57:50 frank * bug 1418: fixed memory leak of shapes * * Revision 1.5 2005/06/14 16:03:35 dan * Updated copyright date to 2005 * * Revision 1.4 2005/01/28 06:16:54 sdlime * Applied patch to make function prototypes ANSI C compliant. Thanks to Petter Reinholdtsen. This fixes but 1181. * * Revision 1.3 2004/10/21 04:30:55 frank * Added standardized headers. Added MS_CVSID(). * */ #include #include #include #include #include "map.h" MS_CVSID("$Id: sortshp.c,v 1.6 2005/07/15 13:57:50 frank Exp $") typedef struct { double number; char string[255]; int index; } sortStruct; static int compare_string_descending(const void *a, const void *b) { const sortStruct *i = a, *j = b; return(strcmp(j->string, i->string)); } static int compare_string_ascending(const void *a, const void *b) { const sortStruct *i = a, *j = b; return(strcmp(i->string, j->string)); } static int compare_number_descending(const void *a, const void *b) { const sortStruct *i = a, *j = b; if(i->number > j->number) return(-1); if(i->number < j->number) return(1); return(0); } static int compare_number_ascending(const void *a, const void *b) { const sortStruct *i = a, *j = b; if(i->number > j->number) return(1); if(i->number < j->number) return(-1); return(0); } int main(int argc, char *argv[]) { SHPHandle inSHP,outSHP; /* ---- Shapefile file pointers ---- */ DBFHandle inDBF,outDBF; /* ---- DBF file pointers ---- */ sortStruct *array; shapeObj shape; int shpType, nShapes; int fieldNumber=-1; /* ---- Field number of item to be sorted on ---- */ DBFFieldType dbfField; char fName[20]; int fWidth,fnDecimals; char buffer[1024]; int i,j; int num_fields, num_records; if(argc > 1 && strcmp(argv[1], "-v") == 0) { printf("%s\n", msGetVersion()); exit(0); } /* ------------------------------------------------------------------------------- */ /* Check the number of arguments, return syntax if not correct */ /* ------------------------------------------------------------------------------- */ if( argc != 5 ) { fprintf(stderr,"Syntax: sortshp [infile] [outfile] [item] [ascending|descending]\n" ); exit(0); } /* ------------------------------------------------------------------------------- */ /* Open the shapefile */ /* ------------------------------------------------------------------------------- */ inSHP = msSHPOpen(argv[1], "rb" ); if( !inSHP ) { fprintf(stderr,"Unable to open %s shapefile.\n",argv[1]); exit(0); } msSHPGetInfo(inSHP, &nShapes, &shpType); /* ------------------------------------------------------------------------------- */ /* Open the dbf file */ /* ------------------------------------------------------------------------------- */ sprintf(buffer,"%s.dbf",argv[1]); inDBF = msDBFOpen(buffer,"rb"); if( inDBF == NULL ) { fprintf(stderr,"Unable to open %s XBASE file.\n",buffer); exit(0); } num_fields = msDBFGetFieldCount(inDBF); num_records = msDBFGetRecordCount(inDBF); for(i=0;i