/****************************************************************************** * $Id$ * * Project: MapServer * Purpose: Command line utility to sort a shapefile based on a single * attribute in ascending or decending order. Useful for * prioritizing drawing or labeling of shapes. * 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. ****************************************************************************/ #include #include #include #include #include "mapserver.h" MS_CVSID("$Id$") 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