User:Jackaranga/shp2gmt.c
From Wikipedia, the free encyclopedia
- This is small program used to convert a shapefile to a .xy file (GMT multisegment) for use in Generic Mapping Tools, you need shapelib to compile, get it from http://dl.maptools.org/dl/shapelib/
- More information coming soon.
- WIN32 .exe I compiled, no guarantees of fitness for any purpose, rapidshare : http://rapidshare.com/files/62146836/shp2gmt.zip
This work is free software; you can redistribute it and/or modify it under the terms of the GNU Lesser General Public License as published by the Free Software Foundation; either version 2.1 of the License, or (at your option) any later version.
This work is distributed in the hope that it will be useful, but without any warranty; without even the implied warranty of merchantability or fitness for a particular purpose. See the GNU Lesser General Public License for more details.
You should have received a copy of the GNU Lesser General Public License along with this library; if not, write to the Free Software Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
/******************************************************************************
* $Id: shp2gmt.c,v 1.0 2001/01/10 22:30:37 ben Exp$
* [formerly $Id: shpdump.c,v 1.7 1999/11/05 14:12:04 warmerda Exp $]
*
* Project: Shapelib add-on
* Purpose: Application for dumping contents of a shapefile to
* the terminal in human readable form (GMT multisegment).
* Author: Frank Warmerdam, warmerda@hidden.domain
* Modified: Benjamin Horner-Johnson, bch-j@hidden.domain
******************************************************************************
* 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 LICENSE.LGPL). 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: shp2gmt.c,v $
* Revision 1.8 2001/01/10 22:30:37 ben
* modified shpdump.c to dump in GMT multi-segment file format
*
* Revision 1.7 1999/11/05 14:12:04 warmerda
* updated license terms
*
* Revision 1.6 1998/12/03 15:48:48 warmerda
* Added report of shapefile type, and total number of shapes.
*
* Revision 1.5 1998/11/09 20:57:36 warmerda
* use SHPObject.
*
* Revision 1.4 1995/10/21 03:14:49 warmerda
* Changed to use binary file access.
*
* Revision 1.3 1995/08/23 02:25:25 warmerda
* Added support for bounds.
*
* Revision 1.2 1995/08/04 03:18:11 warmerda
* Added header.
*
*/
static char rcsid[] =
"$Id: shpdump.c,v 1.7 1999/11/05 14:12:04 warmerda Exp $";
#include "shapefil.h"
int main( int argc, char ** argv )
{
SHPHandle hSHP;
int nShapeType, nEntities, i, iPart;
const char *pszPlus;
double adfMinBound[4], adfMaxBound[4];
/* -------------------------------------------------------------------- */
/* Display a usage message. */
/* -------------------------------------------------------------------- */
if( argc != 2 )
{
printf( "shp2gmt shp_file\n" );
exit( 1 );
}
/* -------------------------------------------------------------------- */
/* Open the passed shapefile. */
/* -------------------------------------------------------------------- */
hSHP = SHPOpen( argv[1], "rb" );
if( hSHP == NULL )
{
printf( "Unable to open:%s\n", argv[1] );
exit( 1 );
}
/* -------------------------------------------------------------------- */
/* Print out the file bounds. */
/* -------------------------------------------------------------------- */
SHPGetInfo( hSHP, &nEntities, &nShapeType, adfMinBound, adfMaxBound );
/* printf( "Shapefile Type: %s # of Shapes: %d\n\n",
SHPTypeName( nShapeType ), nEntities );
printf( "File Bounds: (%12.3f,%12.3f,%lg,%lg)\n"
" to (%12.3f,%12.3f,%lg,%lg)\n",
adfMinBound[0],
adfMinBound[1],
adfMinBound[2],
adfMinBound[3],
adfMaxBound[0],
adfMaxBound[1],
adfMaxBound[2],
adfMaxBound[3] ); */
/* -------------------------------------------------------------------- */
/* Skim over the list of shapes, printing all the vertices. */
/* -------------------------------------------------------------------- */
for( i = 0; i < nEntities; i++ )
{
int j;
SHPObject *psShape;
psShape = SHPReadObject( hSHP, i );
printf( "> Shape # %d (%s) nVertices=%d, nParts=%d\n",
i, SHPTypeName(psShape->nSHPType),
psShape->nVertices, psShape->nParts);
for( j = 0, iPart = 1; j < psShape->nVertices; j++ )
{
const char *pszPartType = "";
if( j == 0 && psShape->nParts > 0 )
pszPartType = SHPPartTypeName( psShape->panPartType[0] );
if( iPart < psShape->nParts
&& psShape->panPartStart[iPart] == j )
{
pszPartType = SHPPartTypeName( psShape->panPartType[iPart] );
iPart++;
pszPlus = ">";
}
else
pszPlus = " ";
printf("%s %12.3f %12.3f %lg %lg %s\n",
pszPlus,
psShape->padfX[j],
psShape->padfY[j],
psShape->padfZ[j],
psShape->padfM[j],
pszPartType );
}
SHPDestroyObject( psShape );
}
SHPClose( hSHP );
#ifdef USE_DBMALLOC
malloc_dump(2);
#endif
exit( 0 );
}

