Skip to content

Instantly share code, notes, and snippets.

@springmeyer
Created August 12, 2011 16:51
Show Gist options
  • Save springmeyer/1142451 to your computer and use it in GitHub Desktop.
Save springmeyer/1142451 to your computer and use it in GitHub Desktop.
Index: utils/shapeindex/shapeindex.cpp
===================================================================
--- utils/shapeindex/shapeindex.cpp (revision 3106)
+++ utils/shapeindex/shapeindex.cpp (working copy)
@@ -23,6 +23,7 @@
#include <iostream>
+#include <fstream>
#include <vector>
#include <string>
@@ -135,6 +136,14 @@
clog << "error : cannot open " << shapename_full << endl;
continue;
}
+
+ std::fstream file((shapename+".index").c_str(),
+ std::ios::in | std::ios::out | std::ios::trunc | std::ios::binary);
+ if (!file) {
+ clog << "cannot open index file for writing file \""
+ << (shapename+".index") << "\"" << endl;
+ continue;
+ }
int code = shp.read_xdr_integer(); //file_code == 9994
clog << code << endl;
@@ -143,6 +152,10 @@
int file_length=shp.read_xdr_integer();
int version=shp.read_ndr_integer();
int shape_type=shp.read_ndr_integer();
+ if (shape_type == shape_io::shape_multipatch) {
+ clog << "shapeindex: shapefile multipatch type is not supported" << endl;
+ return 1;
+ }
box2d<double> extent;
shp.read_envelope(extent);
@@ -161,23 +174,27 @@
long offset=shp.pos();
int record_number=shp.read_xdr_integer();
int content_length=shp.read_xdr_integer();
+ int geom_type=shp.read_ndr_integer(); // should be same as shape_type unless null
//std::clog << "rec number = "<< record_number << "\n";
//std::clog << "content length = "<< content_length << "\n";
- shp.skip(4);
+ //shp.skip(4);
//std::clog << "offset= "<< offset << std::endl;
box2d<double> item_ext;
- if (shape_type==shape_io::shape_point)
+ bool valid = false;
+ if (geom_type==shape_io::shape_point)
{
+ valid = true;
double x=shp.read_double();
double y=shp.read_double();
item_ext=box2d<double>(x,y,x,y);
}
- else if (shape_type==shape_io::shape_pointm)
+ else if (geom_type==shape_io::shape_pointm)
{
+ valid = true;
double x=shp.read_double();
double y=shp.read_double();
// skip m
@@ -185,8 +202,9 @@
item_ext=box2d<double>(x,y,x,y);
}
- else if (shape_type==shape_io::shape_pointz)
+ else if (geom_type==shape_io::shape_pointz)
{
+ valid = true;
double x=shp.read_double();
double y=shp.read_double();
// skip z
@@ -199,15 +217,18 @@
item_ext=box2d<double>(x,y,x,y);
}
- else
+ else if (geom_type!=shape_io::shape_null)
{
+ valid = true;
shp.read_envelope(item_ext);
shp.skip(2*content_length-4*8-4);
}
- tree.insert(offset,item_ext);
- if (verbose) {
- clog << "record number " << record_number << " box=" << item_ext << endl;
+ if (valid) {
+ tree.insert(offset,item_ext);
+ if (verbose) {
+ clog << "record number " << record_number << " box=" << item_ext << endl;
+ }
}
pos+=4+content_length;
@@ -220,12 +241,7 @@
clog << " number shapes=" << count << endl;
- std::fstream file((shapename+".index").c_str(),
- std::ios::in | std::ios::out | std::ios::trunc | std::ios::binary);
- if (!file) {
- clog << "cannot open index file for writing file \""
- << (shapename+".index") << "\"" << endl;
- } else {
+ if (file) {
tree.trim();
std::clog<<" number nodes="<<tree.count()<<std::endl;
file.exceptions(std::ios::failbit | std::ios::badbit);
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment