Esempio n. 1
0
bool shape::load(string filename)
{
    OGRRegisterAll();
	

	// First open the shape file
	ds = OGRSFDriverRegistrar::Open( filename.c_str(), FALSE );

	// Now to load in the points .....
	points = vector<vector<glm::vec2>>();

	// Grab the first layer
    OGRLayer* layer = ds->GetLayer(0);

    // Grab the spatial reference and create a coordinate transform function
    sr = layer->GetSpatialRef();
	
    // Taking from http://www.compsci.wm.edu/SciClone/documentation/software/geo/gdal-1.9.0/html/ogr/ogr_apitut.html
    OGRFeature *poFeature;

    layer->ResetReading();
    while( (poFeature = layer->GetNextFeature()) != NULL )
    {
        OGRFeatureDefn *poFDefn = layer->GetLayerDefn();
        int iField;

        OGRGeometry *poGeometry;

        poGeometry = poFeature->GetGeometryRef();
        if( poGeometry != NULL 
            && wkbFlatten(poGeometry->getGeometryType()) == wkbPoint )
        {
            OGRPoint *poPoint = (OGRPoint *) poGeometry;

            //printf( "%.3f,%3.f\n", poPoint->getX(), poPoint->getY() );
        }
        else if (poGeometry != NULL 
            && wkbFlatten(poGeometry->getGeometryType()) == wkbLineString)
        {
            //cout << "LINES!!!!" << endl;
            OGRLineString* ls= (OGRLineString*)poGeometry;
            points.push_back(vector<glm::vec2>());
            for(int i = 0; i < ls->getNumPoints(); i++ )
            {
            	OGRPoint p;
            	ls->getPoint(i,&p);

            	// This function can transform a larget set of points.....
            	double x = p.getX();
            	double y = p.getY();
            	points[points.size()-1].push_back(glm::vec2(x,y));
            	//poTransform->Transform (1, &x, &y);
            	//cout << p.getX() << " " << p.getY()  << "Transformed!: " << x << " " << y << endl;
            }
        }  
        else if (poGeometry != NULL 
            && wkbFlatten(poGeometry->getGeometryType()) == wkbPolygon)
        {

            OGRLineString* ls= (OGRLineString*)poGeometry->getBoundary();
            points.push_back(vector<glm::vec2>());
            cout << "POLYGON" << ls->getNumPoints() << endl;
            //exit(0);
            for(int i = 0; i < ls->getNumPoints(); i++ )
            {
                OGRPoint p;
                ls->getPoint(i,&p);

                // This function can transform a larget set of points.....
                double x = p.getX();
                double y = p.getY();
                points[points.size()-1].push_back(glm::vec2(x,y));
                //poTransform->Transform (1, &x, &y);
                //cout << p.getX() << " " << p.getY()  << "Transformed!: " << x << " " << y << endl;
            }
        }      
        OGRFeature::DestroyFeature( poFeature );
    }
    return true;
}