Commit 706db0c1 authored by sw897's avatar sw897

Initial commit

Shapefile C Library (origin at http://shapelib.maptools.org)
parents
dbfadd
dbfcreate
dbfdump
shpadd
shpcreate
shpdump
shprewind
shptreedump
shptest
*.dll
*.lib
*.exp
*.la
*.lo
.libs
This diff is collapsed.
Producing Shapelib Releases
===========================
1) Update web/release.html with change notes on the new release and commit.
2) Build the release files.
./mkrelease.sh 1.3.0
3) Upload:
scp shapelib-1.3.0{.tar.gz,.zip} warmerdam@upload.osgeo.org:/osgeo/download/shapelib
... add upload notes to maptools.org ...
4) Announce on shapelib mailing list.
This diff is collapsed.
PREFIX = /usr/local
CFLAGS = -g -Wall -fPIC
#CFLAGS = -g -DUSE_CPL
#CC = g++
LIBOBJ = shpopen.o dbfopen.o safileio.o shptree.o
SHPBIN = shpcreate shpadd shpdump shprewind dbfcreate dbfadd dbfdump \
shptreedump
default: all
all: $(SHPBIN) shptest lib
shpopen.o: shpopen.c shapefil.h
$(CC) $(CFLAGS) -c shpopen.c
shptree.o: shptree.c shapefil.h
$(CC) $(CFLAGS) -c shptree.c
dbfopen.o: dbfopen.c shapefil.h
$(CC) $(CFLAGS) -c dbfopen.c
safileio.o: safileio.c shapefil.h
$(CC) $(CFLAGS) -c safileio.c
shpcreate: shpcreate.c shpopen.o safileio.o
$(CC) $(CFLAGS) shpcreate.c shpopen.o safileio.o $(LINKOPT) -o shpcreate
shpadd: shpadd.c shpopen.o safileio.o
$(CC) $(CFLAGS) shpadd.c shpopen.o safileio.o $(LINKOPT) -o shpadd
shpdump: shpdump.c shpopen.o safileio.o
$(CC) $(CFLAGS) shpdump.c shpopen.o safileio.o $(LINKOPT) -o shpdump
shprewind: shprewind.c shpopen.o safileio.o
$(CC) $(CFLAGS) shprewind.c shpopen.o safileio.o $(LINKOPT) -o shprewind
dbfcreate: dbfcreate.c dbfopen.o safileio.o
$(CC) $(CFLAGS) dbfcreate.c dbfopen.o safileio.o $(LINKOPT) -o dbfcreate
dbfadd: dbfadd.c dbfopen.o safileio.o
$(CC) $(CFLAGS) dbfadd.c dbfopen.o safileio.o $(LINKOPT) -o dbfadd
dbfdump: dbfdump.c dbfopen.o safileio.o
$(CC) $(CFLAGS) dbfdump.c dbfopen.o safileio.o $(LINKOPT) -o dbfdump
shptest: shptest.c shpopen.o safileio.o
$(CC) $(CFLAGS) shptest.c shpopen.o safileio.o $(LINKOPT) -o shptest
shputils: shputils.c shpopen.o safileio.o dbfopen.o
$(CC) $(CFLAGS) shputils.c shpopen.o safileio.o dbfopen.o $(LINKOPT) -o shputils
shptreedump: shptreedump.c shptree.o shpopen.o safileio.o
$(CC) $(CFLAGS) shptreedump.c shptree.o shpopen.o safileio.o $(LINKOPT) \
-o shptreedump
clean:
rm -f *.o shptest $(SHPBIN) libshp.a
test: test2 test3
#
# Note this stream only works if example data is accessable.
# Fetch ftp://gdal.velocet.ca/pub/outgoing/shape_eg_data.zip
#
test1:
@./stream1.sh > s1.out
@if test "`diff s1.out stream1.out`" = '' ; then \
echo "******* Stream 1 Succeeded *********"; \
rm s1.out; \
else \
echo "******* Stream 1 Failed *********"; \
diff s1.out stream1.out; \
fi
test2:
@./stream2.sh > s2.out
@if test "`diff s2.out stream2.out`" = '' ; then \
echo "******* Stream 2 Succeeded *********"; \
rm s2.out; \
rm test*.s??; \
else \
echo "******* Stream 2 Failed *********"; \
diff s2.out stream2.out; \
fi
test3:
@./makeshape.sh > s3.out
@if test "`diff s3.out stream3.out`" = '' ; then \
echo "******* Stream 3 Succeeded *********"; \
rm s3.out; \
rm test.*; \
else \
echo "******* Stream 3 Failed *********"; \
diff s3.out stream3.out; \
fi
lib: libshp.a
libshp.a: $(LIBOBJ)
ar r libshp.a $(LIBOBJ)
lib_install: libshp.a
cp libshp.a $(PREFIX)/lib
cp shapefil.h $(PREFIX)/include
bin_install: $(SHPBIN)
cp $(SHPBIN) $(PREFIX)/bin
install: lib_install bin_install
Please read shapelib.html.
Building on Unix
----------------
1) Edit Makefile, and set CFLAGS, and CC macros as required for the
target system. Often the defaults will work fine.
2) type "make"
The result should be:
Core shapelib support.
shpopen.o
dbfopen.o
safileio.o
shptree.o
libshp.a
Utility/demonstration programs:
shpcreate, shpdump, shpadd, dbfcreate, dbfdump, dbfadd, shptreedump
3) To test type:
make test
4) To libshp.a and the test binaries in /usr/local:
make install
Building on Windows
-------------------
If you have run the VC++ VCVARS32.BAT, you should be able to type the
following in a command window to build the code and executables:
C:> nmake /f makefile.vc
Otherwise create your own VC++ project. There aren't many files to deal with
here!
Venkat,
I have completed the planned Shapefile quadtree mechanism. The additions
to the traditional Shapelib are found in shptree.c (functions supporting
quad tree searching and query). There are also some new prototypes for
the tree stuff in shapefil.h ... including some prototypes for functions
you don't require and hence that I haven't implemented at this time.
I have also prepared a demonstration program using the API. That is
the ``shpdumptree'' program, with the source code in shpdumptree.c. The
shpdumptree program has two functions. One is to dump an ASCII rendering
of the internal quadtree, and the other is example use of a quad tree
searching function.
Dumping the Tree
----------------
The tree dumping is done as shown below. The "-maxdepth" commandline
switch can be used to control the maximum depth, otherwise it internally
computes a ``reasonable depth'' to use based on the number of structures
in the shapefile.
warmerda@gdal[207]% shptreedump -maxdepth 6 eg_data/polygon.shp
( SHPTreeNode
Min = (471127.19,4751545.00)
Max = (489292.31,4765610.50)
Shapes(0):
( SHPTreeNode
Min = (471127.19,4751545.00)
Max = (481118.01,4765610.50)
Shapes(0):
( SHPTreeNode
Min = (471127.19,4751545.00)
Max = (481118.01,4759281.03)
Shapes(0):
( SHPTreeNode
Min = (471127.19,4751545.00)
Max = (476622.14,4759281.03)
Shapes(0):
( SHPTreeNode
Min = (471127.19,4751545.00)
Max = (476622.14,4755799.81)
Shapes(0):
( SHPTreeNode
Min = (471127.19,4751545.00)
Max = (474149.41,4755799.81)
Shapes(6): 395 397 402 404 405 422
)
( SHPTreeNode
Min = (473599.92,4751545.00)
Max = (476622.14,4755799.81)
Shapes(10): 392 394 403 413 414 417 426 433 434 447
)
)
...
A structure like the following represents one node in the tree. In
this case it cover the region of 473599.92 < X < 476622.14,and
4751545.0 < Y < 4755799.81. There are ten shapes within this region
who's shapeids are 392, 394 ... 447. This node has no children nodes.
( SHPTreeNode
Min = (473599.92,4751545.00)
Max = (476622.14,4755799.81)
Shapes(10): 392 394 403 413 414 417 426 433 434 447
)
The heirarchy of indentation is intended to show the parent, child
relationship between nodes, with the tree being deeper the further to the
right you go.
The `-v' flag to the program can be used to expand the report to include
the full information about shapes, not just their shapeid. This can result
in a report looking more like this:
...
( SHPTreeNode
Min = (478095.78,4751545.00)
Max = (481118.01,4755799.81)
Shapes(3):
( Shape
ShapeId = 448
Min = (479988.09,4753300.00)
Max = (480705.59,4754236.50)
Vertex[0] = (480136.59,4754174.50)
Vertex[1] = (480229.97,4754182.00)
Vertex[2] = (480370.09,4754200.50)
Vertex[3] = (480695.12,4754236.50)
Vertex[4] = (480687.97,4754129.50)
Vertex[5] = (480650.47,4754075.50)
Vertex[6] = (480520.62,4753948.00)
Vertex[7] = (480490.00,4753900.00)
Vertex[8] = (480499.78,4753840.50)
Vertex[9] = (480500.97,4753820.50)
Vertex[10] = (480534.75,4753660.50)
Vertex[11] = (480560.00,4753565.00)
Vertex[12] = (480574.91,4753550.50)
...
While it is possible to part the output of the shptreedump program, and
insert it into your database, my intention was that the shptreedump program
would serve as an example of how to pre-order traversal of the quad tree,
and collect the information you will need to insert into your database.
I would then expect you to write a new program based on shptreedump that
calls a C API for your database to insert objects instead of printing them
out. Alternatively there may be an ASCII format for loading tables that
you could modify the program to output.
Searching
---------
The other thing that you can do with the shptreedump program is to
perform a search on the quadtree. For instance the following shows
searching on a small region.
% shptreedump -search 471127 4751545 476622 4759281 eg_data/polygon.shp
Shape 17: not in area of interest, but fetched.
Shape 31: not in area of interest, but fetched.
Shape 52: not in area of interest, but fetched.
Shape 76: not in area of interest, but fetched.
Shape 82: not in area of interest, but fetched.
Shape 104: not in area of interest, but fetched.
Shape 124: not in area of interest, but fetched.
Shape 134: not in area of interest, but fetched.
Shape 139: not in area of interest, but fetched.
Shape 154: not in area of interest, but fetched.
Shape 175: not in area of interest, but fetched.
Shape 177: not in area of interest, but fetched.
Shape 185: not in area of interest, but fetched.
Shape 192: not in area of interest, but fetched.
Shape 196: appears to be in area of interest.
....
I have included this capability (and the SHPTreeFindLikelyShapes() function)
so that you can see a working example of how to search this quad tree.
Note that searching is a multi-stage affair.
First a pass is made over the quadtree, collecting the shapeids of all
shapes contained in a quadtree node for which the bounding rectangle overlaps
the search rectangle. This is all accomplished by SHPTreeFindLikelyShapes()
in shptree.c.
The second phase is to fetch the actual shapes, and verify if their bounding
box falls within the area of interest. This is necessary because the shape
will tend to have a significantly smaller bounding rectangle than the tree
node in which it is found. This can result ``false positives'' on the first
phase search, as indicated by teh ``not in area of interest, but fetched''
messages above. This stage is done in the SHPTreeNodeSearchAndDump()
function in shptreedump.c.
A possible third phase is to verify that the actualy line segments in the
shape actually cross the area of interest. I don't both with this as it
is complicated, and assuming that the drawing engine takes care of clipping
it is quite a bit easier to let it fall through.
Building
--------
I have added a makefile.vc to the shapelib distribution. After you have
unpacked the shapefile you should have a shapelib subdirectory. If you
cd to that directory, and enter ``nmake -f makefile.vc'' in a DOS window
you should be able to build everything with VC++ (assuming it is properly
installed and in your path).
You can also create a project in VC just including the files
shpopen.c, shptree.c and shptreedump.c, building as a Win32 console
application.
For your convenience I am including prebuild .obj files, and .exe files
in the distribution.
shpdxf
dbfinfo
shpcentrd
shpdata
shpwkb
dbfcat
shpinfo
shpfix
shpcat
Shape_PointInPoly
shpproj
shpsort
#LINKOPT = /usr/local/lib/libdbmalloc.a
#CFLAGS = -g
# Endian: define either _LITTLE_ENDIAN or _BIG_ENDIAN
ENDIAN = -D_LITTLE_ENDIAN
CFLAGS = -g -I.. -I$(HOME)/bld/include -DPROJ4 $(ENDIAN) -DDEBUG -DDEBUG2
SHPOBJ = ../shpopen.o ../dbfopen.o ../safileio.o
SHPGOBJ = ../shpopen.o ../dbfopen.o ../safileio.o shpgeo.o
GEOOBJ = ./shpgeo.o -lm -L$(HOME)/bld/lib -lproj
default: all
all: shpdxf shpproj dbfinfo shpcentrd shpdata shpwkb dbfinfo dbfcat shpinfo shpfix shpcat Shape_PointInPoly shpsort
clean:
rm -f shpdxf shpproj dbfinfo shpcentrd shpdata shpwkb dbfcat dbfinfo shpinfo shpfix shpcat *.o
shpgeo.o: shpgeo.c shpgeo.h
$(CC) $(CFLAGS) -c shpgeo.c
shpdxf: shpdxf.c $(SHPOBJ)
$(CC) $(CFLAGS) shpdxf.c ${SHPOBJ} $(LINKOPT) -o shpdxf
shpcentrd: shpcentrd.c $(SHPGOBJ)
$(CC) $(CFLAGS) shpcentrd.c ${SHPOBJ} $(LINKOPT) $(GEOOBJ) -o shpcentrd
shpdata: shpdata.c $(SHPGOBJ)
$(CC) $(CFLAGS) shpdata.c ${SHPOBJ} $(LINKOPT) $(GEOOBJ) -o shpdata
shpinfo: shpinfo.c $(SHPOBJ)
$(CC) $(CFLAGS) shpinfo.c ${SHPOBJ} $(LINKOPT) $(GEOOBJ) -o shpinfo
shpfix: shpfix.c $(SHPOBJ)
$(CC) $(CFLAGS) shpfix.c ${SHPOBJ} $(LINKOPT) -o shpfix
shpcat: shpcat.c $(SHPOBJ)
$(CC) $(CFLAGS) shpcat.c ${SHPOBJ} $(LINKOPT) -o shpcat
shpwkb: shpwkb.c $(SHPGOBJ)
$(CC) $(CFLAGS) shpwkb.c ${SHPOBJ} $(LINKOPT) $(GEOOBJ) -o shpwkb
shpproj: shpproj.c $(SHPGOBJ)
$(CC) $(CFLAGS) shpproj.c $(SHPOBJ) $(GEOOBJ) -lm -lproj $(LINKOPT) -o shpproj
shpsort: shpsort.c $(SHPOBJ)
$(CC) $(CFLAGS) shpsort.c ${SHPOBJ} $(LINKOPT) -lm -o shpsort
dbfinfo: dbfinfo.c $(SHPOBJ)
$(CC) $(CFLAGS) dbfinfo.c $(SHPOBJ) $(LINKOPT) -o dbfinfo
dbfcat: dbfcat.c $(SHPOBJ)
$(CC) $(CFLAGS) dbfcat.c $(SHPOBJ) $(LINKOPT) -o dbfcat
Shape_PointInPoly: Shape_PointInPoly.cpp $(SHPOBJ)
$(CXX) $(CFLAGS) Shape_PointInPoly.cpp $(SHPOBJ) $(LINKOPT) \
-o Shape_PointInPoly
check: testproj
testproj:
tests/shpproj.sh
This diff is collapsed.
/******************************************************************************
* $Id: Shape_PointInPoly.cpp,v 1.1 2004-01-09 16:47:57 fwarmerdam Exp $
*
* Project: Shapelib
* Purpose: Commandline program to generate points-in-polygons from a
* shapefile as a shapefile.
* Author: Marko Podgorsek, d-mon@siol.net
*
******************************************************************************
* Copyright (c) 2004, Marko Podgorsek, d-mon@siol.net
*
* 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: Shape_PointInPoly.cpp,v $
* Revision 1.1 2004-01-09 16:47:57 fwarmerdam
* New
*
*/
static char rcsid[] =
"$Id: Shape_PointInPoly.cpp,v 1.1 2004-01-09 16:47:57 fwarmerdam Exp $";
#include <string.h>
#include <stdlib.h>
#include <math.h>
#include <shapefil.h>
#define MAXINTERSECTIONPOINTS 255
enum loopDir {
kExterior,
kInterior,
kError
};
struct DPoint2d
{
DPoint2d()
{
x = y = 0.0;
};
DPoint2d(double x, double y)
{
this->x = x;
this->y = y;
};
double x,y;
};
struct IntersectPoint
{
IntersectPoint(void)
{
x = y = 0.0;
boundry_nmb = 0;
loopdirection = kError;
};
double x,y;
int boundry_nmb;
loopDir loopdirection;
};
loopDir LoopDirection(DPoint2d *vertices, int vertsize)
{
int i;
double sum = 0.0;
for(i=0;i<vertsize-1;i++)
{
sum += (vertices[i].x*vertices[i+1].y)-(vertices[i].y*vertices[i+1].x);
}
if(sum>0)
return kInterior;
else
return kExterior;
}
DPoint2d CreatePointInPoly(SHPObject *psShape, int quality)
{
int i, j, k, end, vert, pointpos;
double part, dx, xmin, xmax, ymin, ymax, y, x3, x4, y3, y4, len, maxlen = 0;
DPoint2d *vertices;
loopDir direction;
IntersectPoint mp1, mp2, point1, point2, points[MAXINTERSECTIONPOINTS];
xmin = psShape->dfXMin;
ymin = psShape->dfYMin;
xmax = psShape->dfXMax;
ymax = psShape->dfYMax;
part = (ymax-ymin)/(quality+1);
dx = xmax-xmin;
for(i=0;i<quality;i++)
{
y = ymin+part*(i+1);
pointpos = 0;
for(j=0;j<psShape->nParts;j++)
{
if(j==psShape->nParts-1)
end = psShape->nVertices;
else
end = psShape->panPartStart[j+1];
vertices = new DPoint2d [end-psShape->panPartStart[j]];
for(k=psShape->panPartStart[j],vert=0;k<end;k++)
{
vertices[vert].x = psShape->padfX[k];
vertices[vert++].y = psShape->padfY[k];
}
direction = LoopDirection(vertices, vert);
for(k=0;k<vert-1;k++)
{
y3 = vertices[k].y;
y4 = vertices[k+1].y;
if((y3 >= y && y4 < y) || (y3 <= y && y4 > y)) //I check >= only once, because if it's not checked now (y3) it will be in the next iteration (which is y4 now)
{
point1.boundry_nmb = j;
point1.loopdirection = direction;
x3 = vertices[k].x;
x4 = vertices[k+1].x;
if(y3==y)
{
point1.y = y3;
point1.x = x3;
if(direction == kInterior) //add point 2 times if the direction is interior, so that the final count of points is even
{
points[pointpos++]=point1;
}
}
else
{
point1.x = xmin+(((((x4-x3)*(y-y3))-((y4-y3)*(xmin-x3)))/((y4-y3)*dx))*dx); //striped down calculation of intersection of 2 lines
point1.y = y;
}
points[pointpos++]=point1;
}
}
delete [] vertices;
}
for(j=1;j<pointpos;j++) //sort the found intersection points by x value
{
for(k=j;k>0;k--)
{
if(points[k].x < points[k-1].x)
{
point1 = points[k];
points[k] = points[k-1];
points[k-1] = point1;
}
else
{
break;
}
}
}
for(j=0;j<pointpos-1;j++)
{
point1 = points[j];
point2 = points[j+1];
if((point1.loopdirection == kExterior && //some checkings for valid point
point2.loopdirection == kExterior &&
point1.boundry_nmb == point2.boundry_nmb &&
j%2==0) ||
(point1.loopdirection == kExterior &&
point2.loopdirection == kInterior) ||
(point1.loopdirection == kInterior &&
point2.loopdirection == kExterior))
{
len = sqrt(pow(point1.x-point2.x, 2)+pow(point1.y-point2.y, 2));
if(len >= maxlen)
{
maxlen = len;
mp1 = point1;
mp2 = point2;
}
}
}
}
return DPoint2d((mp1.x+mp2.x)*0.5, (mp1.y+mp2.y)*0.5);
}
int main(int argc, char* argv[])
{
if(argc != 3)
{
printf("Usage: %s shpfile_path quality\n", argv[0]);
return 1;
}
int i, nEntities, quality;
SHPHandle hSHP;
SHPObject *psShape;
DPoint2d pt;
quality = atoi(argv[2]);
hSHP = SHPOpen(argv[1], "rb");
SHPGetInfo(hSHP, &nEntities, NULL, NULL, NULL);
printf("PointInPoly v1.0, by Marko Podgorsek\n----------------\n");
for( i = 0; i < nEntities; i++ )
{
psShape = SHPReadObject( hSHP, i );
if(psShape->nSHPType == SHPT_POLYGON)
{
pt = CreatePointInPoly(psShape, quality);
printf("%d: x=%f y=%f\n",i, pt.x,pt.y);
}
SHPDestroyObject( psShape );
}
SHPClose(hSHP);
return 0;
}
===============================================================================
Project: Shape_PoinInPoly
Purpose: Sample and the function for calculatin a point in a polygon
(complex,compound - it doesn't matter). Can be used for labeling.
Author: Copyright (c) 2004, Marko Podgorsek, d-mon@siol.net
===============================================================================
Requires: shapelib 1.2 (http://shapelib.maptools.org/)
Tested and created on platform:
Windows 2000 Professional
Visual Studio .NET 7.0
P4 2.4 GHz
1GB RAM
I just found out about the ShapeLib, GDAL and OGR and I must say that they're
all great projects.
I belive I'll use some of those libraries in the future. Right now I'm using
only shapelib.
The thing that led me to the http://wwww.maptools.org was the need of finding
the point in poly...but as I found out that even OGR didn't support it. So
there I was. I was forced to make my own function. Well, it was fun. I learned
a lot.
I wrote this function for the Autodesk Autocad 2004 MPolygon, because there was
no function to do this in the Object Arx SDK (the Acad programming SDK). Well,
it will be in the 2005 release...but, still. There is a function in the
Autodesk Map 2004 version...in the menu.
Not usefull when you need the coordinates, not the point on the screen...
So when the Acad version was done I was thinking of doing it on the Shape files,
too. A little bit of changing the structures and variable
types (so they're not using Object Arx) and I was done.
And here it is....Contribution from me to the ShapeLib world :)...and maybe even
OGR (a little bit of changing there).
Some statistics:
For about 69000 polygons in Autocad picture (.dwg files)
Autodesk Map 2004 was creating centroids (the menu command) about 45s (1 scan
line)
My function, with 3 scan lines took about 5s. And I was drawing the dots on the
picture...
-------------------------------------------------------------------------------
DPoint2d CreatePointInPoly(SHPObject *psShape, int quality)
The second parameter quality tell the function just how many rays shall it use
to get the point.
quality = 3 works very well, but anything below 5 is good.
This doesn't mean that the execution will slow down, but it just finds a good
point. That's all.
The qality shows on the compound objects (multiple poligons with more than one
exterior loop) - if not enough rays, then there may be no centroid.
Or the U shaped thin polygon, only the bootom (below the y center line) is fat.
Autodesk Map with one scan line will create the centroid on one of the thin
parts, because it only uses the y center line. If you have more rays, one will
surely pass the fat area and centroid will be created there.
-------------------------------------------------------------------------------
Anyone using this function:
Just send me an e-mail, so I'll see if I did anything good for the public.
And you can send me e-mail with questions also.
This diff is collapsed.
/*
* Copyright (c) 1995 Frank Warmerdam
*
* This code is in the public domain.
*
* $Log: dbfcat.c,v $
* Revision 1.2 2011-07-24 03:17:46 fwarmerdam
* include string.h and stdlib.h where needed in contrib (#2146)
*
* Revision 1.1 1999-05-26 02:56:31 candrsn
* updates to shpdxf, dbfinfo, port from Shapelib 1.1.5 of dbfcat and shpinfo
*
* Revision 1.3 1995/10/21 03:15:01 warmerda
* Changed to use binary file access.
*
* Revision 1.2 1995/08/04 03:16:22 warmerda
* Added header.
*
*/
static char rcsid[] =
"$Id: dbfcat.c,v 1.2 2011-07-24 03:17:46 fwarmerdam Exp $";
#include <stdlib.h>
#include <string.h>
#include "shapefil.h"
int main( int argc, char ** argv )
{
DBFHandle hDBF;
int *panWidth, i, iRecord;
char szFormat[32], szField[1024];
char cTitle[32], nTitle[32];
int nWidth, nDecimals;
int cnWidth, cnDecimals;
DBFHandle cDBF;
DBFFieldType hType,cType;
int ci, ciRecord;
char tfile[160];
int hflds, j, cflds;
int verbose = 0;
int force = 0;
int mismatch = 0;
int matches = 0;
char fld_m[256];
int shift = 0;
char type_names[4][15] = {"integer", "string", "double", "double"};
if( argc < 3 )
{
printf( "dbfcat [-v] [-f] from_DBFfile to_DBFfile\n" );
exit( 1 );
}
if ( strcmp ("-v", argv[1] ) == 0 ) { shift = 1; verbose = 1; }
if ( strcmp ("-f", argv[1 + shift] ) == 0 ) { shift ++; force = 1; }
if ( strcmp ("-v", argv[1 + shift] ) == 0 ) { shift ++; verbose = 1; }
strcpy (tfile, argv[1 + shift]);
strcat (tfile, ".dbf");
hDBF = DBFOpen( tfile, "rb" );
if( hDBF == NULL )
{
printf( "DBFOpen(%s.dbf,\"r\") failed for From_DBF.\n", tfile );
exit( 2 );
}
strcpy (tfile, argv[2 + shift]);
strcat (tfile, ".dbf");
cDBF = DBFOpen( tfile, "rb+" );
if( cDBF == NULL )
{
printf( "DBFOpen(%s.dbf,\"rb+\") failed for To_DBF.\n", tfile );
exit( 2 );
}
if( DBFGetFieldCount(hDBF) == 0 )
{
printf( "There are no fields in this table!\n" );
exit( 3 );
}
hflds = DBFGetFieldCount(hDBF);
cflds = DBFGetFieldCount(cDBF);
matches = 0;
for( i = 0; i < hflds; i++ )
{
char szTitle[18];
char cname[18];
int j;
hType = DBFGetFieldInfo( hDBF, i, szTitle, &nWidth, &nDecimals );
fld_m[i] = -1;
for ( j = 0; j < cflds; j ++ )
{
cType = DBFGetFieldInfo( cDBF, j, cname, &cnWidth, &cnDecimals );
if ( strcmp (cname, szTitle) == 0 )
{
if ( hType != cType )
{ printf ("Incompatible fields %s(%s) != %s(%s),\n",
type_names[hType],nTitle,type_names[cType],cTitle);
mismatch = 1;
}
fld_m[i] = j;
if ( verbose )
{ printf("%s %s(%d,%d) <- %s %s(%d,%d)\n", cname, type_names[cType],
cnWidth, cnDecimals,
szTitle, type_names[hType], nWidth, nDecimals); }
j = cflds;
matches = 1;
}
}
}
if ( (matches == 0 ) && !force ) {
printf ("ERROR: No field names match for tables, cannot proceed\n use -f to force processing using blank records\n");
exit(-1); }
if ( mismatch && !force ) {
printf ("ERROR: field type mismatch cannot proceed\n use -f to force processing using attempted conversions\n");
exit(-1); }
for( iRecord = 0; iRecord < DBFGetRecordCount(hDBF); iRecord++ )
{
ciRecord = DBFGetRecordCount( cDBF );
for ( i = 0; i < hflds;i ++ )
{
double cf;
ci = fld_m[i];
if ( ci != -1 )
{
cType = DBFGetFieldInfo( cDBF, ci, cTitle, &cnWidth, &cnDecimals );
hType = DBFGetFieldInfo( hDBF, i, nTitle, &nWidth, &nDecimals );
switch( cType )
{
case FTString:
DBFWriteStringAttribute(cDBF, ciRecord, ci,
(char *) DBFReadStringAttribute( hDBF, iRecord, i ) );
break;
case FTInteger:
DBFWriteIntegerAttribute(cDBF, ciRecord, ci,
(int) DBFReadIntegerAttribute( hDBF, iRecord, i ) );
break;
case FTDouble:
/* cf = DBFReadDoubleAttribute( hDBF, iRecord, i );
printf ("%s <- %s (%f)\n", cTitle, nTitle, cf);
*/
DBFWriteDoubleAttribute(cDBF, ciRecord, ci,
(double) DBFReadDoubleAttribute( hDBF, iRecord, i ) );
break;
}
}
} /* fields names match */
}
if ( verbose ) { printf (" %d records appended \n\n", iRecord); }
DBFClose( hDBF );
DBFClose( cDBF );
return( 0 );
}
/*
* Copyright (c) 1999 Carl Anderson
*
* This code is in the public domain.
*
* This code is based in part on the earlier work of Frank Warmerdam
*
* requires shapelib 1.2
* gcc dbfinfo dbfopen.o dbfinfo
*
*
* $Log: dbfinfo.c,v $
* Revision 1.3 2011-07-24 03:17:46 fwarmerdam
* include string.h and stdlib.h where needed in contrib (#2146)
*
* Revision 1.2 1999-05-26 02:56:31 candrsn
* updates to shpdxf, dbfinfo, port from Shapelib 1.1.5 of dbfcat and shpinfo
*
*
*/
#include <stdlib.h>
#include <string.h>
#include "shapefil.h"
int main( int argc, char ** argv )
{
DBFHandle hDBF;
int *panWidth, i, iRecord;
char szFormat[32], szField[1024];
char ftype[15], cTitle[32], nTitle[32];
int nWidth, nDecimals;
int cnWidth, cnDecimals;
DBFHandle cDBF;
DBFFieldType hType,cType;
int ci, ciRecord;
/* -------------------------------------------------------------------- */
/* Display a usage message. */
/* -------------------------------------------------------------------- */
if( argc != 2 )
{
printf( "dbfinfo xbase_file\n" );
exit( 1 );
}
/* -------------------------------------------------------------------- */
/* Open the file. */
/* -------------------------------------------------------------------- */
hDBF = DBFOpen( argv[1], "rb" );
if( hDBF == NULL )
{
printf( "DBFOpen(%s,\"r\") failed.\n", argv[1] );
exit( 2 );
}
printf ("Info for %s\n",argv[1]);
/* -------------------------------------------------------------------- */
/* If there is no data in this file let the user know. */
/* -------------------------------------------------------------------- */
i = DBFGetFieldCount(hDBF);
printf ("%ld Columns, %ld Records in file\n",i,DBFGetRecordCount(hDBF));
/* -------------------------------------------------------------------- */
/* Compute offsets to use when printing each of the field */
/* values. We make each field as wide as the field title+1, or */
/* the field value + 1. */
/* -------------------------------------------------------------------- */
panWidth = (int *) malloc( DBFGetFieldCount( hDBF ) * sizeof(int) );
for( i = 0; i < DBFGetFieldCount(hDBF); i++ )
{
char szTitle[12];
DBFFieldType eType;
switch ( DBFGetFieldInfo( hDBF, i, szTitle, &nWidth, &nDecimals )) {
case FTString:
strcpy (ftype, "string");;
break;
case FTInteger:
strcpy (ftype, "integer");
break;
case FTDouble:
strcpy (ftype, "float");
break;
case FTInvalid:
strcpy (ftype, "invalid/unsupported");
break;
default:
strcpy (ftype, "unknown");
break;
}
printf ("%15.15s\t%15s (%d,%d)\n",szTitle, ftype, nWidth, nDecimals);
}
DBFClose( hDBF );
return( 0 );
}
shpproj
using the projection tools proj and invproj
we have implemented a shapefile projection utility
shpproj shp_file new_shp ( -i=in_proj_file | -i="in_params" | -i=geographic ) ( -o=out_info_file | -o="out_params" | -o=geographic )
INPUT
Input can come from one of three sources. A projection parameter file,
directly through parameters or geographic. If the shapefile has an associated
prj file, name the same as the shapefile but ending in ".prj" it will be used
by default ignoring all other parameters. If input is omitted it defaults to
geographic, unless the default prj file exists..
OUTPUT
Output can come from one of three sources. A projection parameter file,
directly through parameters or geographic. If output is omitted it defaults
to geographic.
PROJECTION PARAMETER FILE
This file MUST end with the extension ".prj". It has the form of one
projection parameter per line. Parameters can be in any order.
PROJECTION PARAMETERS
Are the same as used by proj and invproj.
use proj -lP to see available projections
proj -lu to see available units
proj -le to see available ellipsoid
USAGE
the following example projects file rowtest to row3, moving data from Stateplane NAD83 zone 1002 to utm zone 16 in meters
shpproj rowtest row -i="init=nad83:1002 units=us-ft" -o="proj=utm zone=16 units=m"
shpproj rowtest row3 -o="proj=utm zone=18 units=m" -i="zone=16 proj=utm units=us-ft"
shpproj rowtest row3 -o="proj=utm zone=18 units=m"
shpproj rowtest row3 -i=myfile.prj -o=geographic
shpproj rowtest row3 -is=myfile.prj
Datums
Stateplane data can be accessed by the init function
init=nad83:1003
which is of the form
datum:FIPSzone
To move from NAD27 to NAD83 or otherwise the shpproj utility moves to
geographic coordinates and the back to the desired datum. proj can handle
direct datum to datum conversions but it is umimplemented here.
for a complete listing of FIPSZones by datum read nad/nad.lst in the PROJ4
library
CAUTION
It is possible to get very poor results for projection values when using
coordinates VERY far away from the intended scope of the projection.
An example of a poor projection choice would be to move from nad83:1002
(Georgia West) for data in California into utm. The resulting data will be
much less accurate than if it had started as geographic.
CFLAGS = /Ox /MD /nologo /I.. /IC:\OSGeo4W\include \
/D_LITTLE_ENDIAN -DPROJ4
LINKARGS = ..\shapelib_i.lib C:\OSGeo4W\lib\proj_i.lib
default: all
all: shpcat.exe shpcentrd.exe shpdxf.exe shpinfo.exe shpproj.exe
shpcat.exe: shpcat.obj shpgeo.obj
$(CC) $(CFLAGS) shpcat.obj $(LINKARGS)
if exist $@.manifest mt -manifest $@.manifest -outputresource:$@;1
shpcentrd.exe: shpcentrd.obj shpgeo.obj
$(CC) $(CFLAGS) shpcentrd.obj shpgeo.obj $(LINKARGS)
if exist $@.manifest mt -manifest $@.manifest -outputresource:$@;1
shpproj.exe: shpproj.obj shpgeo.obj
$(CC) $(CFLAGS) shpproj.obj shpgeo.obj $(LINKARGS)
if exist $@.manifest mt -manifest $@.manifest -outputresource:$@;1
shpdxf.exe: shpdxf.obj shpgeo.obj
$(CC) $(CFLAGS) shpdxf.obj $(LINKARGS)
if exist $@.manifest mt -manifest $@.manifest -outputresource:$@;1
shpinfo.exe: shpinfo.obj shpgeo.obj
$(CC) $(CFLAGS) shpinfo.obj $(LINKARGS)
if exist $@.manifest mt -manifest $@.manifest -outputresource:$@;1
clean:
-del *.obj
-del *.exe
-del *.manifest
/* `NAN' constant for IEEE 754 machines.
Copyright (C) 1992, 1996 Free Software Foundation, Inc.
This file is part of the GNU C Library.
The GNU C Library is free software; you can redistribute it and/or
modify it under the terms of the GNU Library General Public License as
published by the Free Software Foundation; either version 2 of the
License, or (at your option) any later version.
The GNU C Library 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
Library General Public License for more details.
You should have received a copy of the GNU Library General Public
License along with the GNU C Library; see the file COPYING.LIB. If not,
write to the Free Software Foundation, Inc., 59 Temple Place - Suite 330,
Boston, MA 02111-1307, USA. */
#ifndef _GNU_NAN_H
#define _GNU_NAN_H 1
/* hacked to define NAN on Solaris 2.7 if it wasn't defined */
/* IEEE Not A Number. */
#ifdef _BIG_ENDIAN
# define __nan_bytes { 0x7f, 0xf8, 0, 0, 0, 0, 0, 0 }
#endif
#ifdef _LITTLE_ENDIAN
# define __nan_bytes { 0, 0, 0, 0, 0, 0, 0xf8, 0x7f }
#endif
#ifdef __GNUC__
# define NAN \
(__extension__ ((union { unsigned char __c[8]; \
double __d; }) \
{ __nan_bytes }).__d)
#else /* Not GCC. */
static const char __nan[8] = __nan_bytes;
# define NAN (*(const double *) __nan)
#endif
#endif /* gnu_nan.h */
/******************************************************************************
* Copyright (c) 1999, Carl Anderson
*
* This code is based in part on the earlier work of Frank Warmerdam
*
* 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.
******************************************************************************
*
* shpcat
*
* gcc shpcat.c ../shpopen.o -o shpcat
*
* Utility program to concatenate two shapefiles
* Must be used in concert with dbfcat
*
*/
#include <stdlib.h>
#include <string.h>
#include "shapefil.h"
int dbfcat_main( int argc, char ** argv );
int main( int argc, char ** argv )
{
SHPHandle hSHP, cSHP;
int nShapeType, i, nEntities, nShpInFile;
SHPObject *shape;
/* -------------------------------------------------------------------- */
/* Display a usage message. */
/* -------------------------------------------------------------------- */
if( argc != 3 )
{
printf( "shpcat from_shpfile to_shpfile\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 );
}
SHPGetInfo( hSHP, &nEntities, &nShapeType, NULL, NULL );
fprintf(stderr,"Opened From File %s, with %d shapes\n",argv[1],nEntities);
/* -------------------------------------------------------------------- */
/* Open the passed shapefile. */
/* -------------------------------------------------------------------- */
cSHP = SHPOpen( argv[2], "rb+" );
if( cSHP == NULL )
{
printf( "Unable to open:%s\n", argv[2] );
exit( 1 );
}
SHPGetInfo( cSHP, &nShpInFile, NULL, NULL, NULL );
fprintf(stderr,"Opened to file %s with %d shapes, ready to add %d\n",
argv[2],nShpInFile,nEntities);
/* -------------------------------------------------------------------- */
/* Skim over the list of shapes, printing all the vertices. */
/* -------------------------------------------------------------------- */
for( i = 0; i < nEntities; i++ )
{
shape = SHPReadObject( hSHP, i );
SHPWriteObject( cSHP, -1, shape );
SHPDestroyObject ( shape );
}
SHPClose( hSHP );
SHPClose( cSHP );
exit( 0 );
}
/******************************************************************************
* Copyright (c) 1999, Carl Anderson
*
* this code is based in part on the earlier work of Frank Warmerdam
*
* 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.
******************************************************************************
*
* shpcentrd.c - compute XY centroid for complex shapes
* and create a new SHPT_PT file of then
* specifically undo compound objects but not complex ones
*
*
* $Log: shpcentrd.c,v $
* Revision 1.3 2011-07-24 03:17:46 fwarmerdam
* include string.h and stdlib.h where needed in contrib (#2146)
*
* Revision 1.2 1999-05-26 02:56:31 candrsn
* updates to shpdxf, dbfinfo, port from Shapelib 1.1.5 of dbfcat and shpinfo
*
*
*
*/
/* the centroid is defined as
*
* Cx = sum (x dArea ) / Total Area
* and
* Cy = sum (y dArea ) / Total Area
*/
#include <stdlib.h>
#include <string.h>
#include "shapefil.h"
#include "shpgeo.h"
int main( int argc, char ** argv )
{
SHPHandle old_SHP, new_SHP;
DBFHandle old_DBF, new_DBF;
int nShapeType, nEntities, nVertices, nParts, *panParts, i, iPart;
double *padVertices, adBounds[4];
const char *pszPlus;
DBFFieldType idfld_type;
int idfld, nflds;
char kv[257] = "";
char idfldName[120] = "";
char fldName[120] = "";
char shpFileName[120] = "";
char dbfFileName[120] = "";
double apeture[4];
char *DBFRow = NULL;
int Cpan[2] = { 0,0 };
int byRing = 1;
PT Centrd;
SHPObject *psCShape, *cent_pt;
if( argc < 3 )
{
printf( "shpcentrd shp_file new_shp_file\n" );
exit( 1 );
}
old_SHP = SHPOpen (argv[1], "rb" );
old_DBF = DBFOpen (argv[1], "rb");
if( old_SHP == NULL || old_DBF == NULL )
{
printf( "Unable to open old files:%s\n", argv[1] );
exit( 1 );
}
SHPGetInfo( old_SHP, &nEntities, &nShapeType, NULL, NULL );
new_SHP = SHPCreate ( argv[2], SHPT_POINT );
new_DBF = DBFCloneEmpty (old_DBF, argv[2]);
if( new_SHP == NULL || new_DBF == NULL )
{
printf( "Unable to create new files:%s\n", argv[2] );
exit( 1 );
}
DBFRow = (char *) malloc ( (old_DBF->nRecordLength) + 15 );
#ifdef DEBUG
printf ("ShpCentrd using shpgeo \n");
#endif
for( i = 0; i < nEntities; i++ )
{
int res ;
psCShape = SHPReadObject( old_SHP, i );
if ( byRing == 1 ) {
int ring;
for ( ring = 0; ring < psCShape->nParts; ring ++ ) {
SHPObject *psO;
psO = SHPClone ( psCShape, ring, ring + 1 );
Centrd = SHPCentrd_2d ( psO );
cent_pt = SHPCreateSimpleObject ( SHPT_POINT, 1,
(double*) &(Centrd.x), (double*) &(Centrd.y), NULL );
SHPWriteObject ( new_SHP, -1, cent_pt );
memcpy ( DBFRow, DBFReadTuple ( old_DBF, i ),
old_DBF->nRecordLength );
DBFWriteTuple ( new_DBF, new_DBF->nRecords, DBFRow );
SHPDestroyObject ( cent_pt );
SHPDestroyObject ( psO );
}
}
else {
Centrd = SHPCentrd_2d ( psCShape );
cent_pt = SHPCreateSimpleObject ( SHPT_POINT, 1,
(double*) &(Centrd.x), (double*) &(Centrd.y), NULL );
SHPWriteObject ( new_SHP, -1, cent_pt );
memcpy ( DBFRow, DBFReadTuple ( old_DBF, i ),
old_DBF->nRecordLength );
DBFWriteTuple ( new_DBF, new_DBF->nRecords, DBFRow );
SHPDestroyObject ( cent_pt );
}
}
SHPClose( old_SHP );
SHPClose( new_SHP );
DBFClose( old_DBF );
DBFClose( new_DBF );
printf ("\n");
}
/******************************************************************************
* Copyright (c) 1999, Carl Anderson
*
* this code is based in part on the earlier work of Frank Warmerdam
*
* 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.
******************************************************************************
*
* shpdata.c - utility program for testing elements of the libraries
*
*
* $Log: shpdata.c,v $
* Revision 1.2 1999-05-26 02:56:31 candrsn
* updates to shpdxf, dbfinfo, port from Shapelib 1.1.5 of dbfcat and shpinfo
*
*
*
*/
#include "shapefil.h"
#include "shpgeo.h"
int main( int argc, char ** argv )
{
SHPHandle old_SHP, new_SHP;
DBFHandle old_DBF, new_DBF;
int nShapeType, nEntities, nVertices, nParts, *panParts, i, iPart;
double *padVertices, adBounds[4];
const char *pszPlus;
DBFFieldType idfld_type;
int idfld, nflds;
char kv[257] = "";
char idfldName[120] = "";
char fldName[120] = "";
char shpFileName[120] = "";
char dbfFileName[120] = "";
char *DBFRow = NULL;
int Cpan[2] = { 0,0 };
int byRing = 1;
PT oCentrd, ringCentrd;
SHPObject *psCShape, *cent_pt;
double oArea = 0.0, oLen = 0.0;
if( argc < 2 )
{
printf( "shpdata shp_file \n" );
exit( 1 );
}
old_SHP = SHPOpen (argv[1], "rb" );
old_DBF = DBFOpen (argv[1], "rb");
if( old_SHP == NULL || old_DBF == NULL )
{
printf( "Unable to open old files:%s\n", argv[1] );
exit( 1 );
}
SHPGetInfo( old_SHP, &nEntities, &nShapeType, NULL, NULL );
for( i = 0; i < nEntities; i++ )
{
int res ;
psCShape = SHPReadObject( old_SHP, i );
if ( byRing == 1 ) {
int ring, prevStart, ringDir;
double ringArea;
prevStart = psCShape->nVertices;
for ( ring = (psCShape->nParts - 1); ring >= 0; ring-- ) {
SHPObject *psO;
int j, numVtx, rStart;
rStart = psCShape->panPartStart[ring];
if ( ring == (psCShape->nParts -1) )
{ numVtx = psCShape->nVertices - rStart; }
else
{ numVtx = psCShape->panPartStart[ring+1] - rStart; }
printf ("(shpdata) Ring(%d) (%d for %d) \n", ring, rStart, numVtx);
psO = SHPClone ( psCShape, ring, ring + 1 );
ringDir = SHPRingDir_2d ( psO, 0 );
ringArea = RingArea_2d (psO->nVertices,(double*) psO->padfX,
(double*) psO->padfY);
RingCentroid_2d ( psO->nVertices, (double*) psO->padfX,
(double*) psO->padfY, &ringCentrd, &ringArea);
printf ("(shpdata) Ring %d, %f Area %d dir \n",
ring, ringArea, ringDir );
SHPDestroyObject ( psO );
printf ("(shpdata) End Ring \n");
} /* (ring) [0,nParts */
} /* by ring */
oArea = SHPArea_2d ( psCShape );
oLen = SHPLength_2d ( psCShape );
oCentrd = SHPCentrd_2d ( psCShape );
printf ("(shpdata) Part (%d) %f Area %f length, C (%f,%f)\n",
i, oArea, oLen, oCentrd.x, oCentrd.y );
}
SHPClose( old_SHP );
DBFClose( old_DBF );
printf ("\n");
}
/******************************************************************************
* Copyright (c) 1999, Carl Anderson
*
* This code is based in part on the earlier work of Frank Warmerdam
*
* 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.
******************************************************************************
*
* shp2dxf.c
*
* derived from a ESRI Avenue Script
* and DXF specification from AutoCad 3 (yes 1984)
*
* modifications Carl Andrson 11/96
* modifications Carl Andrson 3/97
*
* converted to C code 12/98
*
* requires shapelib 1.2
* gcc shpdxf.c shpopen.o dbfopen.o -o shpdxf
*
*/
#include <stdlib.h>
#include <string.h>
#include "shapefil.h"
#define FLOAT_PREC "%16.5f\r\n"
void dxf_hdr (x1,y1,x2,y2,df)
double x1,y1,x2,y2;
FILE *df;
{
/* Create HEADER section */
fprintf( df, " 0\r\n");
fprintf( df, "SECTION\r\n");
fprintf( df, " 2\r\n" );
fprintf( df, "HEADER\r\n" );
fprintf( df, " 9\r\n" );
fprintf( df, "$EXTMAX\r\n" );
fprintf( df, " 10\r\n" );
fprintf( df, FLOAT_PREC, x2 );
fprintf( df, " 20\r\n" );
fprintf( df, FLOAT_PREC, y2 );
fprintf( df, " 9\r\n" );
fprintf( df, "$EXTMIN\r\n" );
fprintf( df, " 10\r\n" );
fprintf( df, FLOAT_PREC, x1 );
fprintf( df, " 20\r\n" );
fprintf( df, FLOAT_PREC, y1 );
fprintf( df, " 9\r\n" );
fprintf( df, "$LUPREC\r\n" );
fprintf( df, " 70\r\n" );
fprintf( df, " 14\r\n" );
fprintf( df, " 0\r\n" );
fprintf( df, "ENDSEC\r\n" );
/* ' Create TABLES section */
fprintf( df, " 0\r\n" );
fprintf( df, "SECTION\r\n" );
fprintf( df, " 2\r\n" );
fprintf( df, "TABLES\r\n" );
/* ' Table 1 - set up line type */
fprintf( df, " 0\r\n" );
fprintf( df, "TABLE\r\n" );
fprintf( df, " 2\r\n" );
fprintf( df, "LTYPE\r\n" );
fprintf( df, " 70\r\n" );
fprintf( df, "2\r\n" );
/* ' Entry 1 of Table 1 */
fprintf( df, " 0\r\n" );
fprintf( df, "LTYPE\r\n" );
fprintf( df, " 2\r\n" );
fprintf( df, "CONTINUOUS\r\n" );
fprintf( df, " 70\r\n" );
fprintf( df, "64\r\n" );
fprintf( df, " 3\r\n" );
fprintf( df, "Solid line\r\n" );
fprintf( df, " 72\r\n" );
fprintf( df, "65\r\n" );
fprintf( df, " 73\r\n" );
fprintf( df, "0\r\n" );
fprintf( df, " 40\r\n" );
fprintf( df, "0.0\r\n" );
fprintf( df, " 0\r\n" );
fprintf( df, "ENDTAB\r\n" );
/* End of TABLES section */
fprintf( df, " 0\r\n" );
fprintf( df, "ENDSEC\r\n" );
/* Create BLOCKS section */
fprintf( df, " 0\r\n" );
fprintf( df, "SECTION\r\n" );
fprintf( df, " 2\r\n" );
fprintf( df, "BLOCKS\r\n" );
fprintf( df, " 0\r\n" );
fprintf( df, "ENDSEC\r\n" );
fprintf( df, " 0\r\n" );
fprintf( df, "SECTION\r\n" );
fprintf( df, " 2\r\n" );
fprintf( df, "ENTITIES\r\n" );
}
void
dxf_ent_preamble (dxf_type, id, df)
int dxf_type;
char *id;
FILE *df;
{
fprintf( df, " 0\r\n" );
switch (dxf_type) {
case SHPT_POLYGON:
case SHPT_ARC: fprintf (df, "POLYLINE\r\n");
break;
default: fprintf(df, "POINT\r\n");
}
fprintf( df, " 8\r\n", df);
fprintf( df, "%s\r\n", id );
switch ( dxf_type ) {
case SHPT_ARC:
fprintf( df, " 6\r\n" );
fprintf( df, "CONTINUOUS\r\n" );
fprintf( df, " 66\r\n" );
fprintf( df, "1\r\n" );
break;
case SHPT_POLYGON:
fprintf( df, " 6\r\n" );
fprintf( df, "CONTINUOUS\r\n" );
fprintf( df, " 66\r\n" );
fprintf( df, "1\r\n" );
fprintf( df, " 70\r\n");
fprintf (df, "1\r\n");
default: break;
}
}
void
dxf_ent (id, x, y, z, dxf_type, df)
char *id;
double x,y,z;
int dxf_type;
FILE *df;
{
if ((dxf_type == SHPT_ARC) || ( dxf_type == SHPT_POLYGON)) {
fprintf( df, " 0\r\n");
fprintf( df, "VERTEX\r\n");
fprintf( df, " 8\r\n");
fprintf( df, "%s\r\n", id);
}
fprintf( df, " 10\r\n" );
fprintf( df, FLOAT_PREC, x );
fprintf( df, " 20\r\n" );
fprintf( df, FLOAT_PREC, y );
fprintf( df, " 30\r\n" );
if ( z != 0 )
fprintf( df, FLOAT_PREC, z );
else
fprintf( df, "0.0\r\n" );
}
void
dxf_ent_postamble (dxf_type, df)
int dxf_type;
FILE *df;
{
if ((dxf_type == SHPT_ARC) || ( dxf_type == SHPT_POLYGON))
fprintf( df, " 0\r\nSEQEND\r\n 8\r\n0\r\n", df);
}
int
main (int argc, char **argv)
{
char shpFileName[80] = "", dbfFileName[80] = "";
char dxfFileName[80] = "";
char idfldName[15];
char zfldName[6] = "ELEV";
char fldName[15];
char id[255];
double elev;
int parts, *panParts, nParts, nVertices;
FILE *dxf;
SHPHandle shp;
DBFHandle dbf;
DBFFieldType idfld_type;
double adfBoundsMin[4], adfBoundsMax[4];
int vrtx, shp_type, shp_numrec, zfld, idfld, nflds, recNum, part;
unsigned int MaxElem = -1;
if ( argc < 2 ) {
printf ("usage: shpdxf shapefile {idfield}\r\n");
exit (-1);
}
strcpy (shpFileName,argv[1]);
strncpy (dbfFileName, shpFileName, strlen(shpFileName)-3);
strcat (dbfFileName,"dbf");
strncpy (dxfFileName, shpFileName,strlen(shpFileName)-3);
strcat( dxfFileName, "dxf");
shp = SHPOpen (shpFileName, "rb");
dbf = DBFOpen (dbfFileName, "rb");
dxf = fopen( dxfFileName, "w");
printf("Starting conversion %s(%s) -> %s\r\n",
shpFileName,dbfFileName,dxfFileName);
SHPGetInfo (shp, &shp_numrec, &shp_type, adfBoundsMin, adfBoundsMax );
printf ("file has %d objects\r\n", shp_numrec);
dxf_hdr(adfBoundsMin[0], adfBoundsMin[1], adfBoundsMax[0], adfBoundsMax[1],
dxf);
/* Before proceeding, allow the user to specify the ID field to use or default to the record number.... */
if ( argc > 3 ) MaxElem = atoi(argv[3]);
nflds = DBFGetFieldCount(dbf);
if ( argc > 2 ) {
strcpy (idfldName, argv[2]);
for ( idfld=0; idfld < nflds; idfld++ ) {
idfld_type = DBFGetFieldInfo( dbf, idfld, fldName, NULL, NULL);
if (!strcmp (idfldName, fldName ))
break;
}
if ( idfld >= nflds ) {
printf ("Id field %s not found, using default\r\n",idfldName);
idfld = -1;
} else
printf ("proceeding with field %s for LayerNames\r\n",fldName);
}
else
idfld = -1;
for ( zfld=0; zfld < nflds; zfld++ ) {
DBFGetFieldInfo( dbf, zfld, fldName, NULL, NULL);
if (!strcmp (zfldName, fldName ))
break;
}
if ( zfld >= nflds )
zfld = -1;
// printf ("proceeding with id = %d, elevation = %d\r\n",idfld, zfld);
/* Proceed to process data..... */
for ( recNum = 0; (recNum < shp_numrec) && (recNum < MaxElem); recNum++) {
SHPObject *shape;
if ( idfld >= 0 )
switch (idfld_type) {
case FTString: sprintf (id, "lvl_%s",DBFReadStringAttribute ( dbf, recNum, idfld ));
break;
default: sprintf(id, "%-20.0lf", DBFReadDoubleAttribute (dbf, recNum, idfld));
}
else
sprintf (id,"lvl_%-20d",(recNum +1 ));
if ( zfld >= 0 )
elev = 0;
else
elev = DBFReadDoubleAttribute ( dbf, recNum, zfld );
#ifdef DEBUG
printf("\r\nworking on obj %d", recNum);
#endif
shape = SHPReadObject( shp, recNum );
nVertices = shape->nVertices;
nParts = shape->nParts;
panParts = shape->panPartStart;
part = 0;
for (vrtx=0; vrtx < nVertices; vrtx ++ ) {
#ifdef DEBUG
printf("\rworking on part %d, vertex %d", part,vrtx);
#endif
if ( panParts[part] == vrtx ) {
#ifdef DEBUG
printf ("object preamble\r\n");
#endif
dxf_ent_preamble (shp_type, id, dxf);
}
dxf_ent (id, shape->padfX[vrtx], shape->padfY[vrtx],
elev, shp_type, dxf);
if ((panParts[part] == (vrtx + 1))|| (vrtx == (nVertices -1)) ) {
dxf_ent_postamble (shp_type, dxf);
part ++;
}
}
SHPDestroyObject( shape );
}
/* close out DXF file */
fprintf( dxf, "0\r\n" );
fprintf( dxf, "ENDSEC\r\n" );
fprintf( dxf, "0\r\n" );
fprintf( dxf, "EOF\r\n" );
SHPClose (shp);
DBFClose (dbf);
fclose (dxf);
}
/******************************************************************************
* Copyright (c) 1999, Carl Anderson
*
* This code is based in part on the earlier work of Frank Warmerdam
*
* 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.
******************************************************************************
* shpfix
*
*
* gcc -c shpfix.c shpopen.c -o shpfix
*
* Utility program to fix nulls and inconsistencies in Shapefiles
* as happens from time to time
*
* Simply load and rewrite each record, parameter fixrex allow user to null
* a particularly nasty record if needed
*
*/
#include <stdlib.h>
#include <string.h>
#include "shapefil.h"
int main( int argc, char ** argv )
{
SHPHandle hSHP, cSHP;
int nShapeType, cShapeType, nEntities, i;
double adBounds[4];
SHPObject *shape;
int fix_rec;
/* -------------------------------------------------------------------- */
/* Display a usage message. */
/* -------------------------------------------------------------------- */
if( argc <= 3 )
{
printf( "shpfix shpfile new_file <Record# to Blank>\n" );
exit( 1 );
}
fix_rec = atoi (argv[3]);
fix_rec --;
/* -------------------------------------------------------------------- */
/* Open the passed shapefile. */
/* -------------------------------------------------------------------- */
hSHP = SHPOpen( argv[1], "rb+" );
if( hSHP == NULL )
{
printf( "Unable to open:%s\n", argv[1] );
exit( 1 );
}
SHPGetInfo( hSHP, &nEntities, &nShapeType, NULL, NULL );
/* -------------------------------------------------------------------- */
/* Open the passed shapefile. */
/* -------------------------------------------------------------------- */
cSHP = SHPCreate( argv[2], nShapeType );
if( cSHP == NULL )
{
printf( "Unable to open:%s\n", argv[2] );
exit( 1 );
}
SHPGetInfo( cSHP, NULL, &cShapeType, &(adBounds[0]), &(adBounds[2]) );
/* -------------------------------------------------------------------- */
/* Skim over the list of shapes, printing all the vertices. */
/* -------------------------------------------------------------------- */
for( i = 0; i < nEntities; i++ )
{
shape = SHPReadObject( hSHP, i );
if ( i == fix_rec )
{ shape->nParts = 0;
shape->nVertices = 0;
}
SHPWriteObject( cSHP, -1, shape );
SHPDestroyObject ( shape );
}
SHPClose ( hSHP );
SHPClose ( cSHP );
}
This diff is collapsed.
/******************************************************************************
* Copyright (c) 1999, Carl Anderson
*
* This code is based in part on the earlier work of Frank Warmerdam
*
* 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.
******************************************************************************
* shpgeo.h
*
* support for geometric and other additions to shapelib
*
*
* $Log: shpgeo.h,v $
* Revision 1.3 2002-01-15 14:36:56 warmerda
* upgrade to use proj_api.h
*
* Revision 1.2 1999/05/26 02:56:31 candrsn
* updates to shpdxf, dbfinfo, port from Shapelib 1.1.5 of dbfcat and shpinfo
*
*
*/
/* I'm using some shorthand throughout this file
* R+ is a Clockwise Ring and is the positive portion of an object
* R- is a CounterClockwise Ring and is a hole in a R+
* A complex object is one having at least one R-
* A compound object is one having more than one R+
* A simple object has one and only one element (R+ or R-)
*
* The closed ring constraint is for polygons and assumed here
* Arcs or LineStrings I am calling Rings (generically open or closed)
* Point types are vertices or lists of vertices but not Rings
*
* SHPT_POLYGON, SHPT_POLYGONZ, SHPT_POLYGONM and SHPT_MULTIPATCH
* can have SHPObjects that are compound as well as complex
*
* SHP_POINT and its Z and M derivatives are strictly simple
* MULTI_POINT, SHPT_ARC and their derivatives may be simple or compound
*
*/
#ifndef SHPGEO_H
#define SHPGEO_H
#ifdef __cplusplus
extern "C" {
#endif
#include "proj_api.h"
#define SHPD_POINT 1
#define SHPD_LINE 2
#define SHPD_AREA 4
#define SHPD_Z 8
#define SHPD_MEASURE 16
/* move these into a ogis header file ogis.h */
#define OGIST_UNKNOWN 0
#define OGIST_POINT 1
#define OGIST_LINESTRING 2
#define OGIST_POLYGON 3
#define OGIST_MULTIPOINT 4
#define OGIST_MULTILINE 5
#define OGIST_MULTIPOLYGON 6
#define OGIST_GEOMCOLL 7
typedef struct { int StreamPos;
int NeedSwap;
char *wStream;
} WKBStreamObj;
typedef struct { double x; double y; } PT;
typedef struct { int cParts;
SHPObject *SHPObj;
} SHPObjectList;
#define LSB_ORDER (int) 1
extern char * asFileName ( const char *fil, char *ext );
extern int SHPProject ( SHPObject *psCShape,
projPJ inproj, projPJ outproj );
extern projPJ SHPSetProjection ( int param_cnt, char **params );
extern int SHPFreeProjection ( projPJ p);
extern int SHPDimension ( int SHPType );
extern double SHPArea_2d ( SHPObject *psCShape );
extern int SHPRingDir_2d ( SHPObject *psCShape, int Ring );
extern double SHPLength_2d ( SHPObject *psCShape );
extern PT SHPCentrd_2d ( SHPObject *psCShape );
extern PT SHPPointinPoly_2d ( SHPObject *psCShape );
extern PT* SHPPointsinPoly_2d ( SHPObject *psCShape );
extern int RingCentroid_2d ( int nVertices, double *a, double *b, PT *C,
double *Area );
extern double RingLength_2d ( int nVertices, double *a, double *b );
extern int RingDir_2d ( int nVertices, double *a, double *b );
extern double RingArea_2d ( int nVertices, double *a, double *b );
extern SHPObject* SHPClone ( SHPObject *psCShape, int lowPart, int highPart );
extern SHPObject* SHPUnCompound ( SHPObject *psCShape, int * ringNumber );
extern SHPObject* SHPIntersect_2d ( SHPObject* a, SHPObject* b );
extern int SHPWriteOGisWKB ( WKBStreamObj *stream_obj, SHPObject *psCShape );
extern SHPObject* SHPReadOGisWKB ( WKBStreamObj *stream_obj );
int SHPWriteOGisPolygon ( WKBStreamObj *stream_obj, SHPObject *psCShape );
int SHPWriteOGisLine ( WKBStreamObj *stream_obj, SHPObject *psCShape );
int SHPWriteOGisPoint ( WKBStreamObj *stream_obj, SHPObject *psCShape );
SHPObject* SHPReadOGisPolygon ( WKBStreamObj *stream_obj );
SHPObject* SHPReadOGisLine ( WKBStreamObj *stream_obj );
SHPObject* SHPReadOGisPoint ( WKBStreamObj *stream_obj );
extern int SHPClean ( SHPObject *psCShape );
extern int SHPOGisType ( int GeomType, int toOGis);
void swapD (void *so, unsigned char *in, long bytes);
void swapW (void *so, unsigned char *in, long bytes);
void SwapG( void *so, void *in, int this_cnt, int this_size );
#ifdef __cplusplus
}
#endif
#endif /* ndef SHPGEO_H */
/******************************************************************************
* Copyright (c) 1999, Carl Anderson
*
* This code is based in part on the earlier work of Frank Warmerdam
*
* 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.
******************************************************************************
*
* requires shapelib 1.2
* gcc shpinfoj shpopen.o -o shpinfo
*
*
* $Log: shpinfo.c,v $
* Revision 1.4 2011-07-24 03:17:46 fwarmerdam
* include string.h and stdlib.h where needed in contrib (#2146)
*
* Revision 1.3 2002-04-15 21:33:03 warmerda
* Avoid dereference arrays.
*
* Revision 1.2 2002/04/15 18:40:31 warmerda
* Fixed size of adfBnds{Min,Max} as per bug from David Fowler.
*
* Revision 1.1 1999/05/26 02:56:31 candrsn
* updates to shpdxf, dbfinfo, port from Shapelib 1.1.5 of dbfcat and shpinfo
*
*
*/
#include <stdlib.h>
#include <string.h>
#include "shapefil.h"
int main( int argc, char ** argv )
{
SHPHandle hSHP, cSHP;
int nShapeType, nEntities, nVertices, nParts, *panParts, i, iPart;
double *padVertices, adfBndsMin[4], adfBndsMax[4];
const char *pszPlus;
int cShapeType, cEntities, cVertices, cParts, *cpanParts, ci, cPart;
double *cpadVertices, cadBounds[4];
const char *cpszPlus;
char sType [15]= "";
/* -------------------------------------------------------------------- */
/* Display a usage message. */
/* -------------------------------------------------------------------- */
if( argc != 2 )
{
printf( "shpinfo 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 );
}
SHPGetInfo( hSHP, &nEntities, &nShapeType, adfBndsMin, adfBndsMax );
switch ( nShapeType ) {
case SHPT_POINT:
strcpy(sType,"Point");
break;
case SHPT_ARC:
strcpy(sType,"Polyline");
break;
case SHPT_POLYGON:
strcpy(sType,"Polygon");
break;
case SHPT_MULTIPOINT:
strcpy(sType,"MultiPoint");
break;
}
/* -------------------------------------------------------------------- */
printf ("Info for %s\n",argv[1]);
printf ("%s(%d), %ld Records in file\n",sType,nShapeType,nEntities);
/* -------------------------------------------------------------------- */
/* Print out the file bounds. */
/* -------------------------------------------------------------------- */
printf( "File Bounds: (%15.10lg,%15.10lg)\n\t(%15.10lg,%15.10lg)\n",
adfBndsMin[0], adfBndsMin[1], adfBndsMax[0], adfBndsMax[1] );
SHPClose( hSHP );
}
/******************************************************************************
* Copyright (c) 1999, Carl Anderson
*
* This code is based in part on the earlier work of Frank Warmerdam
*
* 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.
******************************************************************************
*
* requires shapelib 1.2
* gcc shpproj ../shpopen.o ../dbfopen.o shpgeo.o -lm -lproj -o shpproj
*
* this requires linking with the PROJ4.3 projection library available from
*
* ftp://kai.er.usgs.gov/ftp/PROJ.4
*
*
* SHPGeo must be compiled with -DPROJ4 support
*
* $Log: shpproj.c,v $
* Revision 1.10 2011-07-24 03:17:46 fwarmerdam
* include string.h and stdlib.h where needed in contrib (#2146)
*
* Revision 1.9 2002-01-15 14:37:08 warmerda
* upgrade to use proj_api.h
*
* Revision 1.8 2002/01/11 15:47:26 warmerda
* several fixes
*
* Revision 1.7 2002/01/11 15:23:28 warmerda
* use text mode reading and writing .prj files
*
* Revision 1.6 1999/05/26 02:56:31 candrsn
* updates to shpdxf, dbfinfo, port from Shapelib 1.1.5 of dbfcat and shpinfo
*
* Revision 1.2 1999/05/13 19:30:52 warmerda
* Removed libgen.h, added url for PROJ.4, and corrected unsafe return of
* local variable in asFileName().
*
*/
#include <stdlib.h>
#include <string.h>
#include <stdarg.h>
#include "shapefil.h"
#include "shpgeo.h"
int main( int argc, char ** argv )
{
SHPHandle old_SHP, new_SHP;
DBFHandle old_DBF, new_DBF;
int nShapeType, nEntities, nVertices, nParts, *panParts, i, iPart, j;
double *padVertices, adBounds[4];
const char *pszPlus;
DBFFieldType idfld_type;
SHPObject *psCShape;
FILE *ifp = NULL;
int idfld, nflds;
char kv[257] = "";
char idfldName[120] = "";
char fldName[120] = "";
char shpFileName[120] = "";
char dbfFileName[120] = "";
char prjFileName[120] = "";
char parg[80];
double apeture[4];
int inarg, outarg;
char *DBFRow = NULL;
/* for testing only
char *in_args[] = { "init=nad83:1002", "units=us-ft" };
char *out_args[] = { "proj=utm", "zone=16", "units=m" };
*/
char *in_args[16];
char *out_args[16];
int in_argc = 0 , out_argc = 0, outf_arg;
char *arglst;
projPJ orig_prj, new_prj;
va_list myargs, moargs;
if( argc < 4)
{
printf( "shpproj shp_file new_shp ( -i=in_proj_file | -i=\"in_params\" | -i=geographic ) ( -o=out_info_file | -o=\"out_params\" | -o=geographic ) \n" );
exit( 1 );
}
old_SHP = SHPOpen( argv[1], "rb" );
old_DBF = DBFOpen( argv[1], "rb" );
if( old_SHP == NULL || old_DBF == NULL )
{
printf( "Unable to open old files:%s\n", argv[1] );
exit( 1 );
}
outf_arg = 2;
inarg = 0;
outarg = 0;
for ( i = 3; i < argc; i++ ) {
if ( !strncmp ("-i=", argv[i], 3 )) inarg = i;
if ( !strncmp ("-o=", argv[i], 3 )) outarg = i;
}
/* if shapefile has a prj component then use that
else try for a file then read args as list */
if( inarg == 0 )
{
strcpy( prjFileName, argv[1] );
ifp = fopen( asFileName ( prjFileName, "prj" ),"rt");
}
else
{
ifp = fopen( asFileName ( argv[inarg] + 3, "prj" ),"rt");
}
i = 0;
if ( ifp ) {
if( inarg == 0 )
printf ("using default file proj params from <- %s\n",
asFileName ( prjFileName, "prj" ) );
else
printf ("using file proj params from <- %s\n",
asFileName ( argv[inarg] + 3, "prj" ) );
while( fscanf( ifp, "%s", parg) != EOF ) {
in_args[i] = malloc ( strlen(parg)+1 );
strcpy ( in_args[i], parg);
i++;
}
in_argc = i;
fclose (ifp);
}
else {
if ( inarg > 0 ) {
arglst = argv[inarg] + 3;
j = 0;
i = 0;
while ( j < strlen (arglst) ) {
in_argc += sscanf ( arglst + j, "%s", parg);
in_args[i] = malloc( strlen (parg)+1);
strcpy (in_args[i], parg);
i++;
j += strlen (parg) +1;
if ( arglst[j] + 1 == 0 ) j = strlen (argv[inarg]);
}
}
}
i = 0;
if ( outarg > 0 ) ifp = fopen( asFileName ( argv[outarg] + 3, "prj" ),"rt");
if ( ifp ) {
while( fscanf( ifp, "%s", parg) != EOF ) {
out_args[i] = malloc ( strlen(parg));
strcpy ( out_args[i], parg);
i++;
}
out_argc = i;
fclose (ifp);
}
else {
if ( outarg > 0 ) {
arglst = argv[outarg] + 3;
j = 0;
i = 0;
while ( j < strlen (arglst) ) {
out_argc += sscanf ( arglst + j, "%s", parg);
out_args[i] = malloc( strlen (parg)+1);
strcpy (out_args[i], parg);
i++;
j += strlen (parg) +1;
if ( arglst[j] + 1 == 0 ) j = strlen (argv[outarg]);
}
}
}
if ( !strcmp( argv[inarg], "-i=geographic" )) in_argc = 0;
if ( !strcmp( argv[outarg], "-o=geographic" )) out_argc = 0;
orig_prj = SHPSetProjection ( in_argc, in_args );
new_prj = SHPSetProjection ( out_argc, out_args );
if ( !(( (!in_argc) || orig_prj) && ( (!out_argc) || new_prj) )) {
fprintf (stderr, "unable to process projection, exiting...\n");
exit(1);
}
SHPGetInfo( old_SHP, &nEntities, &nShapeType, NULL, NULL);
new_SHP = SHPCreate ( argv[outf_arg], nShapeType );
new_DBF = DBFCloneEmpty (old_DBF, argv[outf_arg]);
if( new_SHP == NULL || new_DBF == NULL )
{
printf( "Unable to create new files:%s\n", argv[outf_arg] );
exit( 1 );
}
DBFRow = (char *) malloc ( (old_DBF->nRecordLength) + 15 );
for( i = 0; i < nEntities; i++ )
{
int j;
psCShape = SHPReadObject ( old_SHP, i );
SHPProject (psCShape, orig_prj, new_prj );
SHPWriteObject ( new_SHP, -1, psCShape );
SHPDestroyObject ( psCShape );
memcpy ( DBFRow, DBFReadTuple ( old_DBF, i ), old_DBF->nRecordLength );
DBFWriteTuple ( new_DBF, new_DBF->nRecords, DBFRow );
}
SHPFreeProjection ( orig_prj );
SHPFreeProjection ( new_prj );
/* store projection params into prj file */
ifp = fopen( asFileName ( argv[outf_arg], "prj" ),"wt");
if ( ifp ) {
if ( out_argc == 0 )
{ fprintf( ifp, "proj=geographic\n" ); }
else
{ for ( i = 0; i < out_argc; i++ )
fprintf( ifp, "%s\n", out_args[i]);
}
fclose (ifp);
}
SHPClose( old_SHP );
SHPClose( new_SHP );
DBFClose( old_DBF );
DBFClose( new_DBF );
printf ("\n");
}
This diff is collapsed.
============================= ABOUT ===========================================
The program "shpsort" is a standalone program for sorting shapefiles
on one or more fields (including the pseudofield "SHAPE") and outputing
the results. People may find it useful for forcing drawing order.
============================= AUTHOR ==========================================
Eric G. Miller
California Department of Fish and Game
2004-06-30
============================= USAGE ===========================================
shpsort <INFILE> <OUTFILE> <SORT_FIELD;SORT_FIELD...> {SORT_ORDER;SORT_ORDER...}
============================= DETAILS =========================================
INFILE The input shapefile
OUTFILE The output shapefile
SORT_FIELD Any attribute field of the shapefile, including "SHAPE"
SORT_ORDER Specify "ASCENDING" or "DESCENDING" for each SORT_FIELD.
This field is optional, and is assumed to be ASCENDING
unless the exact word "DESCENDING" is specified (case
matters).
When sorting on the "SHAPE" the records are sorted as follows:
* Null shapes are treated as any other null field and will
sort to the top in ASCENDING mode. A warning is issued
for each null shape encountered.
* POINT, POINTM, POINTZ, MULTIPOINT, MULTIPOINTM, MULTIPOINTZ
and MULTIPATCH are all sorted by the maximum "Y" value of
their envelopes (not particularly useful).
* POLYLINE, POLYLINEZ and POLYLINEM are sorted by total 2d
shape length.
* POLYGON, POLYGONZ and POLYGONM are sorted by the 2d shape
area. Shapes are assumed to be in canonical ordering, so that
the area of interior rings (if any) is subtracted from the
area of exterior rings.
This diff is collapsed.
#!/bin/sh
cd tests
rm test*
shpcreate test point
shpadd test -83.54949956 34.992401
shpadd test -83.52162155 34.99276748
shpadd test -84.01681518 34.67275985
shpadd test -84.15596023 34.64862437
shpadd test -83.61951463 34.54927047
dbfcreate test -s fd 30
dbfadd test "1"
dbfadd test "2"
dbfadd test "3"
dbfadd test "4"
dbfadd test "5"
../shpproj test test_1 -i=geographic -o="init=nad83:1002 units=us-ft"
../shpproj test_1 test_2 -o="proj=utm zone=16 units=m"
../shpproj test_2 test_3 -o=geographic
shpdump test > test.out
shpdump test_3 > test_3.out
result=`diff test.out test_3.out`
if [ -z "${result}" ]; then
echo success...
else
echo failure...
fi
rm test*
cd ..
This diff is collapsed.
This diff is collapsed.
This diff is collapsed.
This diff is collapsed.
This diff is collapsed.
#!/bin/sh
#
# Use example programs to create a very simple dataset that
# should display in ARCView II.
#
./shpcreate test polygon
./dbfcreate test.dbf -s Description 30 -n TestInt 6 0 -n TestDouble 16 5
./shpadd test 0 0 100 0 100 100 0 100 0 0 + 20 20 20 30 30 30 20 20
./dbfadd test.dbf "Square with triangle missing" 1.5 2.5
./shpadd test 150 150 160 150 180 170 150 150
./dbfadd test.dbf "Smaller triangle" 100 1000.25
./shpadd test 150 150 160 150 180 170 150 150
./dbfadd test.dbf "" "" ""
./shpdump test.shp
./dbfdump test.dbf
#!/bin/sh
if [ $# -lt 1 ] ; then
echo "Usage: mkdist.sh <version>"
echo " <version> - version number used in name of generated archive."
echo "Example: mkdist.sh 1.1.4"
exit
fi
VERSION=$1
rm -rf dist_wrk
mkdir dist_wrk
cd dist_wrk
REP=:pserver:cvsanon@cvs.maptools.org:/cvs/maptools/cvsroot
if cvs -d $REP co shapelib ; then
echo checkout succeeds.
else
cvs -d $REP login
cvs -d $REP co shapelib
fi
if [ ! -d shapelib ] ; then
exit 1
fi
mv shapelib shapelib-$VERSION
find . -name CVS -type d -exec echo rm -rf {} \;
tar czvf ../shapelib-$VERSION.tar.gz shapelib-$VERSION
zip -r ../shapelib-$VERSION.zip shapelib-$VERSION
cd ..
rm -rf dist_wrk
This diff is collapsed.
This diff is collapsed.
This diff is collapsed.
This diff is collapsed.
This diff is collapsed.
This diff is collapsed.
This diff is collapsed.
This diff is collapsed.
This diff is collapsed.
This diff is collapsed.
This diff is collapsed.
This diff is collapsed.
This diff is collapsed.
This diff is collapsed.
This diff is collapsed.
This diff is collapsed.
This diff is collapsed.
This diff is collapsed.
This diff is collapsed.
robots.txt
dl
This diff is collapsed.
This diff is collapsed.
This diff is collapsed.
This diff is collapsed.
This diff is collapsed.
This diff is collapsed.
This diff is collapsed.
This diff is collapsed.
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment