static void Test_WritePoints( int nSHPType, const char *pszFilename ) { SHPHandle hSHPHandle; SHPObject *psShape; double x, y, z, m; hSHPHandle = SHPCreate( pszFilename, nSHPType ); x = 1.0; y = 2.0; z = 3.0; m = 4.0; psShape = SHPCreateObject( nSHPType, -1, 0, NULL, NULL, 1, &x, &y, &z, &m ); SHPWriteObject( hSHPHandle, -1, psShape ); SHPDestroyObject( psShape ); x = 10.0; y = 20.0; z = 30.0; m = 40.0; psShape = SHPCreateObject( nSHPType, -1, 0, NULL, NULL, 1, &x, &y, &z, &m ); SHPWriteObject( hSHPHandle, -1, psShape ); SHPDestroyObject( psShape ); SHPClose( hSHPHandle ); }
static void Test_WriteMultiPoints( int nSHPType, const char *pszFilename ) { SHPHandle hSHPHandle; SHPObject *psShape; double x[4], y[4], z[4], m[4]; int i, iShape; hSHPHandle = SHPCreate( pszFilename, nSHPType ); for( iShape = 0; iShape < 3; iShape++ ) { for( i = 0; i < 4; i++ ) { x[i] = iShape * 10 + i + 1.15; y[i] = iShape * 10 + i + 2.25; z[i] = iShape * 10 + i + 3.35; m[i] = iShape * 10 + i + 4.45; } psShape = SHPCreateObject( nSHPType, -1, 0, NULL, NULL, 4, x, y, z, m ); SHPWriteObject( hSHPHandle, -1, psShape ); SHPDestroyObject( psShape ); } SHPClose( hSHPHandle ); }
void Builder::addPoint( const DL_PointData& data ) { if ( shapefileType != SHPT_POINT ) { QgsDebugMsg( "ignoring point" ); return; } QgsDebugMsg( QString( "point (%1,%2,%3)" ).arg( data.x ).arg( data.y ).arg( data.z ) ); if ( ignoringBlock ) { QgsDebugMsg( "skipping point in block." ); return; } double x = data.x + currentBlockX; double y = data.y + currentBlockY; double z = data.z; SHPObject *psObject; psObject = SHPCreateObject( shapefileType, fetchedprims, 0, NULL, NULL, 1, &x, &y, &z, NULL ); shpObjects.push_back( psObject ); fetchedprims++; }
void Builder::FinalizeAnyPolyline() { // Save the last polyline / polygon if one exists. if ( current_polyline_pointcount > 0 ) { if ( current_polyline_willclose ) { polyVertex << DL_VertexData( closePolyX, closePolyY, closePolyZ ); } int dim = polyVertex.size(); QVector<double> xv( dim ); QVector<double> yv( dim ); QVector<double> zv( dim ); for ( int i = 0; i < dim; i++ ) { xv[i] = polyVertex[i].x; yv[i] = polyVertex[i].y; zv[i] = polyVertex[i].z; } shpObjects << SHPCreateObject( shapefileType, shpObjects.size(), 0, NULL, NULL, dim, xv.data(), yv.data(), zv.data(), NULL ); polyVertex.clear(); QgsDebugMsg( QString( "Finalized adding of polyline shape %1" ).arg( shpObjects.size() - 1 ) ); current_polyline_pointcount = 0; } }
// --------------------------------------------------------------------------- // // ----------- SHPObject* bSHPTable::vxs2shape(int o, ivertices* vxs){ int count; int ptype; double bmin[4],bmax[4]; SHPObject* obj; SHPGetInfo(_shp,&count,&ptype,bmin,bmax); if(ptype==SHPT_NULL){ obj=SHPCreateSimpleObject(ptype,0,NULL,NULL,NULL); } else{ double* x=(double*)malloc(ivs_n_vxs(vxs)*sizeof(double)); double* y=(double*)malloc(ivs_n_vxs(vxs)*sizeof(double)); int np=(ivs_n_parts(vxs)>1)?ivs_n_parts(vxs):0; int* p=(ivs_n_parts(vxs)>1)?(int*)malloc(ivs_n_parts(vxs)*sizeof(int)):NULL; int* pt=(ivs_n_parts(vxs)>1)?(int*)malloc(ivs_n_parts(vxs)*sizeof(int)):NULL; dvertices* dvxs=NULL; vxs_i2d(&dvxs,vxs,_reso); dvs_move(dvxs,_ox,_oy); transform_a2t(dvxs); if(p){ memmove(p,vxs->offs,vxs->no*sizeof(int)); memset(pt,SHPP_RING,vxs->no*sizeof(int)); } for(int i=0;i<ivs_n_vxs(vxs);i++){ x[i]=dvxs->vx.vx2[i].x; y[i]=dvxs->vx.vx2[i].y; } dvs_free(dvxs); obj=SHPCreateObject(ptype,o-1,np,p,pt,ivs_n_vxs(vxs),x,y,NULL,NULL); } return(obj); }
void Builder::addLine( const DL_LineData& data ) { //data.x1, data.y1, data.z1 //data.x2, data.y2, data.z2 if ( shapefileType != SHPT_ARC ) { QgsDebugMsg( "ignoring line" ); return; } QgsDebugMsg( QString( "line %1,%2,%3 %4,%5,%6" ) .arg( data.x1 ).arg( data.y1 ).arg( data.z1 ) .arg( data.x2 ).arg( data.y2 ).arg( data.z2 ) ); if ( ignoringBlock ) { QgsDebugMsg( "skipping line in block." ); return; } double xv[2] = { data.x1, data.x2 }; double yv[2] = { data.y1, data.y2 }; double zv[2] = { data.z1, data.z2 }; shpObjects << SHPCreateObject( shapefileType, shpObjects.size(), 0, NULL, NULL, 2, xv, yv, zv, NULL ); }
// Returns: index in shapefile inline int AddShape(Geometry const& geometry) { // Note: we MIGHT design a small wrapper class which destroys in destructor ::SHPObject* obj = SHPCreateObject(geometry); int result = SHPWriteObject(m_shp, -1, obj ); ::SHPDestroyObject( obj ); return result; }
void Builder::addCircle( const DL_CircleData& data ) { if ( shapefileType != SHPT_ARC && shapefileType != SHPT_POLYGON ) { QgsDebugMsg( "ignoring circle" ); return; } QgsDebugMsg( QString( "circle (%1,%2,%3 r=%4)" ).arg( data.cx ).arg( data.cy ).arg( data.cz ).arg( data.radius ) ); if ( ignoringBlock ) { QgsDebugMsg( "skipping circle in block" ); return; } std::vector <DL_PointData> circlePoints; DL_PointData myPoint; // Approximate the circle with 360 line segments connecting points along that circle register long shpIndex = 0; for ( double i = 0.0; i <= 2*M_PI; i += M_PI / 180.0, shpIndex++ ) { myPoint.x = data.radius * cos( i ) + data.cx + currentBlockX; myPoint.y = data.radius * sin( i ) + data.cy + currentBlockY; myPoint.z = data.cz; circlePoints.push_back( myPoint ); } SHPObject *psShape; int dim = circlePoints.size(); double *xv = new double[dim]; double *yv = new double[dim]; double *zv = new double[dim]; for ( int i = 0; i < dim; i++ ) { xv[i] = circlePoints[i].x; yv[i] = circlePoints[i].y; zv[i] = circlePoints[i].z; } psShape = SHPCreateObject( shapefileType, fetchedprims, 0, NULL, NULL, dim, xv, yv, zv, NULL ); delete [] xv; delete [] yv; delete [] zv; shpObjects.push_back( psShape ); fetchedprims++; circlePoints.clear(); }
void Builder::addCircle( const DL_CircleData& data ) { if ( shapefileType != SHPT_ARC && shapefileType != SHPT_POLYGON ) { QgsDebugMsg( "ignoring circle" ); return; } QgsDebugMsg( QString( "circle (%1,%2,%3 r=%4)" ).arg( data.cx ).arg( data.cy ).arg( data.cz ).arg( data.radius ) ); if ( ignoringBlock ) { QgsDebugMsg( "skipping circle in block" ); return; } std::vector <DL_PointData> circlePoints; DL_PointData myPoint; // Approximate the circle with 360 line segments connecting points along that circle long shpIndex = 0; for ( double i = 0.0; i <= 2*M_PI; i += M_PI / 180.0, shpIndex++ ) { myPoint.x = data.radius * cos( i ) + data.cx; myPoint.y = data.radius * sin( i ) + data.cy; myPoint.z = data.cz; circlePoints.push_back( myPoint ); } int dim = circlePoints.size(); QVector<double> xv( dim ); QVector<double> yv( dim ); QVector<double> zv( dim ); for ( int i = 0; i < dim; i++ ) { xv[i] = circlePoints[i].x; yv[i] = circlePoints[i].y; zv[i] = circlePoints[i].z; } shpObjects << SHPCreateObject( shapefileType, shpObjects.size(), 0, NULL, NULL, dim, xv.data(), yv.data(), zv.data(), NULL ); circlePoints.clear(); }
void Builder::FinalizeAnyPolyline() { // Save the last polyline / polygon if one exists. if ( current_polyline_pointcount > 0 ) { if ( current_polyline_willclose ) { DL_VertexData myVertex; myVertex.x = closePolyX; myVertex.y = closePolyY; myVertex.z = closePolyZ; polyVertex.push_back( myVertex ); } SHPObject *psObject; int dim = polyVertex.size(); double *xv = new double[dim]; double *yv = new double[dim]; double *zv = new double[dim]; for ( int i = 0; i < dim; i++ ) { xv[i] = polyVertex[i].x; yv[i] = polyVertex[i].y; zv[i] = polyVertex[i].z; } psObject = SHPCreateObject( shapefileType, fetchedprims, 0, NULL, NULL, dim, xv, yv, zv, NULL ); delete [] xv; delete [] yv; delete [] zv; shpObjects.push_back( psObject ); polyVertex.clear(); fetchedprims++; QgsDebugMsg( QString( "Finalized adding of polyline shape %1" ).arg( fetchedprims - 1 ) ); current_polyline_pointcount = 0; } }
void Builder::addPoint( const DL_PointData& data ) { if ( shapefileType != SHPT_POINT ) { QgsDebugMsg( "ignoring point" ); return; } QgsDebugMsg( QString( "point (%1,%2,%3)" ).arg( data.x ).arg( data.y ).arg( data.z ) ); if ( ignoringBlock ) { QgsDebugMsg( "skipping point in block." ); return; } double x = data.x, y = data.y, z = data.z; shpObjects << SHPCreateObject( shapefileType, shpObjects.size(), 0, NULL, NULL, 1, &x, &y, &z, NULL ); }
void surfaceVectorField::WriteShapePoint(double xpt, double ypt, double spd, long dir, long view_dir, long map_dir) { long NumRecord; double zpt=0; SHPObject *pSHP; pSHP=SHPCreateObject(SHPT_POINT, -1, 0, NULL, NULL, 1, &xpt, &ypt, &zpt, NULL); SHPWriteObject(hSHP, -1, pSHP); SHPDestroyObject(pSHP); NumRecord = DBFGetRecordCount(hDBF); // DBFWriteDoubleAttribute(hDBF, NumRecord, 0, xpt); // DBFWriteDoubleAttribute(hDBF, NumRecord, 1, ypt); // DBFWriteIntegerAttribute(hDBF, NumRecord, 2, spd); // DBFWriteIntegerAttribute(hDBF, NumRecord, 3, dir); DBFWriteDoubleAttribute(hDBF, NumRecord, 0, spd); DBFWriteIntegerAttribute(hDBF, NumRecord, 1, dir); DBFWriteIntegerAttribute(hDBF, NumRecord, 2, view_dir); DBFWriteIntegerAttribute(hDBF, NumRecord, 3, map_dir); }
void Builder::addLine( const DL_LineData& data ) { //data.x1, data.y1, data.z1 //data.x2, data.y2, data.z2 if ( shapefileType != SHPT_ARC ) { QgsDebugMsg( "ignoring line" ); return; } QgsDebugMsg( QString( "line %1,%2,%3 %4,%5,%6" ) .arg( data.x1 ).arg( data.y1 ).arg( data.z1 ) .arg( data.x2 ).arg( data.y2 ).arg( data.z2 ) ); if ( ignoringBlock ) { QgsDebugMsg( "skipping line in block." ); return; } double xv[2], yv[2], zv[2]; xv[0] = data.x1 + currentBlockX; yv[0] = data.y1 + currentBlockY; zv[0] = data.z1; xv[1] = data.x2 + currentBlockX; yv[1] = data.y2 + currentBlockY; zv[1] = data.z2; SHPObject *psObject; psObject = SHPCreateObject( shapefileType, fetchedprims, 0, NULL, NULL, 2, xv, yv, zv, NULL ); shpObjects.push_back( psObject ); fetchedprims++; }
void Builder::print_shpObjects() { int dim = shpObjects.size(); int dimTexts = textObjects.size(); QgsDebugMsg( QString( "Number of primitives: %1" ).arg( dim ) ); QgsDebugMsg( QString( "Number of text fields: %1" ).arg( dimTexts ) ); SHPHandle hSHP; if ( fname.substr( fname.length() - 4 ).compare( ".shp" ) == 0 ) { outputdbf = fname; outputdbf = outputdbf.replace(( outputdbf.length() - 3 ), outputdbf.length(), "dbf" ); outputshp = fname; outputshp = outputshp.replace(( outputshp.length() - 3 ), outputshp.length(), "shp" ); outputtdbf = fname; outputtdbf = outputtdbf.replace(( outputtdbf.length() - 4 ), outputtdbf.length(), "_texts.dbf" ); outputtshp = fname; outputtshp = outputtshp.replace(( outputtshp.length() - 4 ), outputtshp.length(), "_texts.shp" ); } else { outputdbf = outputtdbf = fname + ".dbf"; outputshp = outputtshp = fname + ".shp"; } DBFHandle dbffile = DBFCreate( outputdbf.c_str() ); DBFAddField( dbffile, "myid", FTInteger, 10, 0 ); hSHP = SHPCreate( outputshp.c_str(), shapefileType ); QgsDebugMsg( "Writing to main shp file..." ); for ( int i = 0; i < dim; i++ ) { SHPWriteObject( hSHP, -1, shpObjects[i] ); SHPDestroyObject( shpObjects[i] ); DBFWriteIntegerAttribute( dbffile, i, 0, i ); } SHPClose( hSHP ); DBFClose( dbffile ); QgsDebugMsg( "Done!" ); if ( convertText && dimTexts > 0 ) { SHPHandle thSHP; DBFHandle Tdbffile = DBFCreate( outputtdbf.c_str() ); thSHP = SHPCreate( outputtshp.c_str(), SHPT_POINT ); DBFAddField( Tdbffile, "tipx", FTDouble, 20, 10 ); DBFAddField( Tdbffile, "tipy", FTDouble, 20, 10 ); DBFAddField( Tdbffile, "tipz", FTDouble, 20, 10 ); DBFAddField( Tdbffile, "tapx", FTDouble, 20, 10 ); DBFAddField( Tdbffile, "tapy", FTDouble, 20, 10 ); DBFAddField( Tdbffile, "tapz", FTDouble, 20, 10 ); DBFAddField( Tdbffile, "height", FTDouble, 20, 10 ); DBFAddField( Tdbffile, "scale", FTDouble, 20, 10 ); DBFAddField( Tdbffile, "flags", FTInteger, 10, 0 ); DBFAddField( Tdbffile, "hjust", FTInteger, 10, 0 ); DBFAddField( Tdbffile, "vjust", FTInteger, 10, 0 ); DBFAddField( Tdbffile, "text", FTString, 50, 0 ); DBFAddField( Tdbffile, "style", FTString, 50, 0 ); DBFAddField( Tdbffile, "angle", FTDouble, 20, 10 ); QgsDebugMsg( "Writing Texts' shp File..." ); for ( int i = 0; i < dimTexts; i++ ) { SHPObject *psObject; double x = textObjects[i].ipx; double y = textObjects[i].ipy; double z = textObjects[i].ipz; psObject = SHPCreateObject( SHPT_POINT, i, 0, NULL, NULL, 1, &x, &y, &z, NULL ); SHPWriteObject( thSHP, -1, psObject ); DBFWriteDoubleAttribute( Tdbffile, i, 0, textObjects[i].ipx ); DBFWriteDoubleAttribute( Tdbffile, i, 1, textObjects[i].ipy ); DBFWriteDoubleAttribute( Tdbffile, i, 2, textObjects[i].ipz ); DBFWriteDoubleAttribute( Tdbffile, i, 3, textObjects[i].apx ); DBFWriteDoubleAttribute( Tdbffile, i, 4, textObjects[i].apy ); DBFWriteDoubleAttribute( Tdbffile, i, 5, textObjects[i].apz ); DBFWriteDoubleAttribute( Tdbffile, i, 6, textObjects[i].height ); DBFWriteDoubleAttribute( Tdbffile, i, 7, textObjects[i].xScaleFactor ); DBFWriteIntegerAttribute( Tdbffile, i, 8, textObjects[i].textGenerationFlags ); DBFWriteIntegerAttribute( Tdbffile, i, 9, textObjects[i].hJustification ); DBFWriteIntegerAttribute( Tdbffile, i, 10, textObjects[i].vJustification ); DBFWriteStringAttribute( Tdbffile, i, 11, textObjects[i].text.c_str() ); DBFWriteStringAttribute( Tdbffile, i, 12, textObjects[i].style.c_str() ); DBFWriteDoubleAttribute( Tdbffile, i, 13, textObjects[i].angle ); SHPDestroyObject( psObject ); } SHPClose( thSHP ); DBFClose( Tdbffile ); QgsDebugMsg( "Done!" ); } }
void Builder::addArc( const DL_ArcData& data ) { if ( shapefileType != SHPT_ARC ) { QgsDebugMsg( "ignoring arc" ); return; } int fromAngle = ( int ) data.angle1 + 1; int toAngle = ( int ) data.angle2 + 1; QgsDebugMsg( QString( "arc (%1,%2,%3 r=%4 a1=%5 a2=%6)" ) .arg( data.cx ).arg( data.cy ).arg( data.cz ) .arg( data.radius ) .arg( data.angle1 ).arg( data.angle2 ) ); if ( ignoringBlock ) { QgsDebugMsg( "skipping arc in block" ); return; } int i = 0; long shpIndex = 0; // Approximate the arc double radianMeasure; std::vector <DL_PointData> arcPoints; DL_PointData myPoint; for ( i = fromAngle; ; i++, shpIndex++ ) { if ( i > 360 ) i = 0; if ( shpIndex > 1000 ) break; radianMeasure = i * M_PI / 180.0; myPoint.x = data.radius * cos( radianMeasure ) + data.cx; myPoint.y = data.radius * sin( radianMeasure ) + data.cy; myPoint.z = data.cz; arcPoints.push_back( myPoint ); if ( i == toAngle ) break; } // Finalize int dim = arcPoints.size(); QVector<double> xv( dim ); QVector<double> yv( dim ); QVector<double> zv( dim ); for ( int i = 0; i < dim; i++ ) { xv[i] = arcPoints[i].x; yv[i] = arcPoints[i].y; zv[i] = arcPoints[i].z; } shpObjects << SHPCreateObject( shapefileType, shpObjects.size(), 0, NULL, NULL, dim, xv.data(), yv.data(), zv.data(), NULL ); arcPoints.clear(); }
void Builder::addPolyline( const DL_PolylineData& data ) { if ( shapefileType != SHPT_ARC && shapefileType != SHPT_POLYGON ) { QgsDebugMsg( "ignoring polyline" ); return; } QgsDebugMsg( "reading polyline - expecting vertices" ); if ( ignoringBlock ) { QgsDebugMsg( "skipping polyline in block" ); return; } // Add previously created polyline if not finalized yet if ( current_polyline_pointcount > 0 ) { if ( current_polyline_willclose ) { DL_VertexData myVertex; myVertex.x = closePolyX; myVertex.y = closePolyY; myVertex.z = closePolyZ; polyVertex.push_back( myVertex ); } int dim = polyVertex.size(); QVector<double> xv( dim ); QVector<double> yv( dim ); QVector<double> zv( dim ); for ( int i = 0; i < dim; i++ ) { xv[i] = polyVertex[i].x; yv[i] = polyVertex[i].y; zv[i] = polyVertex[i].z; } shpObjects << SHPCreateObject( shapefileType, shpObjects.size(), 0, NULL, NULL, dim, xv.data(), yv.data(), zv.data(), NULL ); polyVertex.clear(); QgsDebugMsg( QString( "polyline prepared: %1" ).arg( shpObjects.size() - 1 ) ); current_polyline_pointcount = 0; } // Now that the currently-adding polyline (if any) is // finalized, parse out the flag options if ( data.flags == 1 || data.flags == 32 ) { current_polyline_willclose = true; store_next_vertex_for_polyline_close = true; } else { current_polyline_willclose = false; store_next_vertex_for_polyline_close = false; } current_polyline_pointcount = 0; }
static void WritePolygonShapefile( const char * pszShapefile, SDTSTransfer * poTransfer, const char * pszMODN ) { SDTSPolygonReader *poPolyReader; /* -------------------------------------------------------------------- */ /* Fetch a reference to the indexed polygon reader. */ /* -------------------------------------------------------------------- */ poPolyReader = (SDTSPolygonReader *) poTransfer->GetLayerIndexedReader( poTransfer->FindLayer( pszMODN ) ); if( poPolyReader == NULL ) { fprintf( stderr, "Failed to open %s.\n", poTransfer->GetCATD()->GetModuleFilePath( pszMODN ) ); return; } /* -------------------------------------------------------------------- */ /* Assemble polygon geometries from all the line layers. */ /* -------------------------------------------------------------------- */ poPolyReader->AssembleRings( poTransfer, poTransfer->FindLayer(pszMODN) ); /* -------------------------------------------------------------------- */ /* Create the Shapefile. */ /* -------------------------------------------------------------------- */ SHPHandle hSHP; hSHP = SHPCreate( pszShapefile, SHPT_POLYGON ); if( hSHP == NULL ) { fprintf( stderr, "Unable to create shapefile `%s'\n", pszShapefile ); return; } /* -------------------------------------------------------------------- */ /* Create the database file, and our basic set of attributes. */ /* -------------------------------------------------------------------- */ DBFHandle hDBF; int nSDTSRecordField; char szDBFFilename[1024]; sprintf( szDBFFilename, "%s.dbf", pszShapefile ); hDBF = DBFCreate( szDBFFilename ); if( hDBF == NULL ) { fprintf( stderr, "Unable to create shapefile .dbf for `%s'\n", pszShapefile ); return; } nSDTSRecordField = DBFAddField( hDBF, "SDTSRecId", FTInteger, 8, 0 ); char **papszModRefs = poPolyReader->ScanModuleReferences(); AddPrimaryAttrToDBFSchema( hDBF, poTransfer, papszModRefs ); CSLDestroy( papszModRefs ); /* ==================================================================== */ /* Process all the polygon features in the module. */ /* ==================================================================== */ SDTSRawPolygon *poRawPoly; poPolyReader->Rewind(); while( (poRawPoly = (SDTSRawPolygon *) poPolyReader->GetNextFeature()) != NULL ) { int iShape; /* -------------------------------------------------------------------- */ /* Write out a shape with the vertices. */ /* -------------------------------------------------------------------- */ SHPObject *psShape; psShape = SHPCreateObject( SHPT_POLYGON, -1, poRawPoly->nRings, poRawPoly->panRingStart, NULL, poRawPoly->nVertices, poRawPoly->padfX, poRawPoly->padfY, poRawPoly->padfZ, NULL ); iShape = SHPWriteObject( hSHP, -1, psShape ); SHPDestroyObject( psShape ); /* -------------------------------------------------------------------- */ /* Write out the attributes. */ /* -------------------------------------------------------------------- */ DBFWriteIntegerAttribute( hDBF, iShape, nSDTSRecordField, poRawPoly->oModId.nRecord ); WritePrimaryAttrToDBF( hDBF, iShape, poTransfer, poRawPoly ); if( !poPolyReader->IsIndexed() ) delete poRawPoly; } /* -------------------------------------------------------------------- */ /* Close, and cleanup. */ /* -------------------------------------------------------------------- */ DBFClose( hDBF ); SHPClose( hSHP ); }
void Builder::addPolyline( const DL_PolylineData& data ) { if ( shapefileType != SHPT_ARC && shapefileType != SHPT_POLYGON ) { QgsDebugMsg( "ignoring polyline" ); return; } QgsDebugMsg( "reading polyline - expecting vertices" ); if ( ignoringBlock ) { QgsDebugMsg( "skipping polyline in block" ); return; } // Add previously created polyline if not finalized yet if ( current_polyline_pointcount > 0 ) { if ( current_polyline_willclose ) { DL_VertexData myVertex; myVertex.x = closePolyX; myVertex.y = closePolyY; myVertex.z = closePolyZ; polyVertex.push_back( myVertex ); } SHPObject *psShape; int dim = polyVertex.size(); double *xv = new double[dim]; double *yv = new double[dim]; double *zv = new double[dim]; for ( int i = 0; i < dim; i++ ) { xv[i] = polyVertex[i].x; yv[i] = polyVertex[i].y; zv[i] = polyVertex[i].z; } psShape = SHPCreateObject( shapefileType, fetchedprims, 0, NULL, NULL, dim, xv, yv, zv, NULL ); delete [] xv; delete [] yv; delete [] zv; shpObjects.push_back( psShape ); fetchedprims++; polyVertex.clear(); QgsDebugMsg( QString( "polyline prepared: %1" ).arg( fetchedprims - 1 ) ); current_polyline_pointcount = 0; } // Now that the currently-adding polyline (if any) is // finalized, parse out the flag options if ( data.flags == 1 || data.flags == 32 ) { current_polyline_willclose = true; store_next_vertex_for_polyline_close = true; } else { current_polyline_willclose = false; store_next_vertex_for_polyline_close = false; } current_polyline_pointcount = 0; }
static void Test_WriteArcPoly( int nSHPType, const char *pszFilename ) { SHPHandle hSHPHandle; SHPObject *psShape; double x[100], y[100], z[100], m[100]; int anPartStart[100]; int anPartType[100], *panPartType; int i, iShape; hSHPHandle = SHPCreate( pszFilename, nSHPType ); if( nSHPType == SHPT_MULTIPATCH ) panPartType = anPartType; else panPartType = NULL; for( iShape = 0; iShape < 3; iShape++ ) { x[0] = 1.0; y[0] = 1.0+iShape*3; x[1] = 2.0; y[1] = 1.0+iShape*3; x[2] = 2.0; y[2] = 2.0+iShape*3; x[3] = 1.0; y[3] = 2.0+iShape*3; x[4] = 1.0; y[4] = 1.0+iShape*3; for( i = 0; i < 5; i++ ) { z[i] = iShape * 10 + i + 3.35; m[i] = iShape * 10 + i + 4.45; } psShape = SHPCreateObject( nSHPType, -1, 0, NULL, NULL, 5, x, y, z, m ); SHPWriteObject( hSHPHandle, -1, psShape ); SHPDestroyObject( psShape ); } /* -------------------------------------------------------------------- */ /* Do a multi part polygon (shape). We close it, and have two */ /* inner rings. */ /* -------------------------------------------------------------------- */ x[0] = 0.0; y[0] = 0.0; x[1] = 0; y[1] = 100; x[2] = 100; y[2] = 100; x[3] = 100; y[3] = 0; x[4] = 0; y[4] = 0; x[5] = 10; y[5] = 20; x[6] = 30; y[6] = 20; x[7] = 30; y[7] = 40; x[8] = 10; y[8] = 40; x[9] = 10; y[9] = 20; x[10] = 60; y[10] = 20; x[11] = 90; y[11] = 20; x[12] = 90; y[12] = 40; x[13] = 60; y[13] = 40; x[14] = 60; y[14] = 20; for( i = 0; i < 15; i++ ) { z[i] = i; m[i] = i*2; } anPartStart[0] = 0; anPartStart[1] = 5; anPartStart[2] = 10; anPartType[0] = SHPP_RING; anPartType[1] = SHPP_INNERRING; anPartType[2] = SHPP_INNERRING; psShape = SHPCreateObject( nSHPType, -1, 3, anPartStart, panPartType, 15, x, y, z, m ); SHPWriteObject( hSHPHandle, -1, psShape ); SHPDestroyObject( psShape ); SHPClose( hSHPHandle ); }
bool TeExportQuerierToShapefile(TeQuerier* querier, const std::string& base) { // check initial conditions if (!querier) return false; if (!querier->loadInstances()) return false; // Get the list of attributes defined by the input querier bool onlyObjId = false; TeAttributeList qAttList = querier->getAttrList(); if (qAttList.empty()) { TeAttributeList qAttList; TeAttribute at; at.rep_.type_ = TeSTRING; at.rep_.numChar_ = 100; at.rep_.name_ = "ID"; at.rep_.isPrimaryKey_ = true; qAttList.push_back(at); onlyObjId = true; } // Handles to each type of geometries that will be created if necessary DBFHandle hDBFPol = 0; SHPHandle hSHPPol = 0; DBFHandle hDBFLin = 0; SHPHandle hSHPLin = 0; DBFHandle hDBFPt = 0; SHPHandle hSHPPt = 0; // Some auxiliary variables int totpoints; double* padfX; double* padfY; unsigned int nVertices; int* panPart; SHPObject *psObject; unsigned int posXY, npoints, nelem; int shpRes; // progress information if (TeProgress::instance()) TeProgress::instance()->setTotalSteps(querier->numElemInstances()); clock_t t0, t1, t2; t2 = clock(); t0 = t1 = t2; int dt = CLOCKS_PER_SEC/2; int dt2 = CLOCKS_PER_SEC; //* .000001; // Loop through the instances writting their geometries and attributes unsigned int iRecPol=0, iRecLin=0, iRecPt=0; unsigned int n, l, m; unsigned int nIProcessed = 0; TeSTInstance st; while(querier->fetchInstance(st)) { totpoints = 0; nVertices = 0; if (st.hasPolygons()) { TePolygonSet& polSet = st.getPolygons(); TePolygonSet::iterator itps; int nVerticesCount = 0; for (itps = polSet.begin(); itps != polSet.end(); ++itps) { nVertices = (*itps).size(); nVerticesCount += nVertices; for (n=0; n<nVertices;++n) totpoints += (*itps)[n].size(); } panPart = (int *) malloc(sizeof(int) * nVerticesCount); padfX = (double *) malloc(sizeof(double) * totpoints); padfY = (double *) malloc(sizeof(double) * totpoints); posXY = 0; nelem = 0; for (itps = polSet.begin(); itps != polSet.end(); ++itps) { TePolygon poly = *itps; for (l=0; l<poly.size(); ++l) { if (l==0) { if (TeOrientation(poly[l]) == TeCOUNTERCLOCKWISE) TeReverseLine(poly[l]); } else { if (TeOrientation(poly[l]) == TeCLOCKWISE) TeReverseLine(poly[l]); } npoints = poly[l].size(); panPart[nelem]=posXY; for (m=0; m<npoints; ++m) { padfX[posXY] = poly[l][m].x_; padfY[posXY] = poly[l][m].y_; posXY++; } nelem++; } } psObject = SHPCreateObject(SHPT_POLYGON, -1, nelem, panPart, NULL, posXY, padfX, padfY, NULL, NULL); if (hSHPPol == 0) { string fname = base + "_pol.shp"; hSHPPol = SHPCreate(fname.c_str(), SHPT_POLYGON); assert (hSHPPol != 0); } shpRes = SHPWriteObject(hSHPPol, -1, psObject); SHPDestroyObject(psObject); free(panPart); free(padfX); free(padfY); assert(shpRes != -1); if (hDBFPol == 0) { hDBFPol = TeCreateDBFFile(base + "_pol.dbf", qAttList); assert (hDBFPol != 0); } if (onlyObjId) { DBFWriteStringAttribute(hDBFPol, iRecPol, 0, st.objectId().c_str()); } else { string val; for (n=0; n<qAttList.size();++n) { st.getPropertyValue(val,n); if (qAttList[n].rep_.type_ == TeSTRING || qAttList[n].rep_.type_ == TeDATETIME) { DBFWriteStringAttribute(hDBFPol, iRecPol, n, val.c_str()); } else if (qAttList[n].rep_.type_ == TeINT) { DBFWriteIntegerAttribute(hDBFPol, iRecPol, n, atoi(val.c_str())); } else if (qAttList[n].rep_.type_ == TeREAL) { DBFWriteDoubleAttribute(hDBFPol, iRecPol, n, atof(val.c_str())); } } } ++iRecPol; } if (st.hasCells()) { TeCellSet& cellSet = st.getCells(); nVertices = cellSet.size(); totpoints = nVertices*5; panPart = (int *) malloc(sizeof(int) * nVertices); padfX = (double *) malloc(sizeof(double) * totpoints); padfY = (double *) malloc(sizeof(double) * totpoints); posXY = 0; nelem = 0; TeCellSet::iterator itcs; for (itcs=cellSet.begin(); itcs!=cellSet.end(); ++itcs) { panPart[nelem]=posXY; padfX[posXY] = (*itcs).box().lowerLeft().x(); padfY[posXY] = (*itcs).box().lowerLeft().y(); posXY++; padfX[posXY] = (*itcs).box().upperRight().x(); padfY[posXY] = (*itcs).box().lowerLeft().y(); posXY++; padfX[posXY] = (*itcs).box().upperRight().x(); padfY[posXY] = (*itcs).box().upperRight().y(); posXY++; padfX[posXY] = (*itcs).box().lowerLeft().x(); padfY[posXY] = (*itcs).box().upperRight().y(); posXY++; padfX[posXY] = (*itcs).box().lowerLeft().x(); padfY[posXY] = (*itcs).box().lowerLeft().y(); ++posXY; ++nelem; } psObject = SHPCreateObject(SHPT_POLYGON, -1, nelem, panPart, NULL,posXY, padfX, padfY, NULL, NULL); if (hSHPPol == 0) { string fname = base + "_pol.shp"; hSHPPol = SHPCreate(fname.c_str(), SHPT_POLYGON); assert (hSHPPol != 0); } shpRes = SHPWriteObject(hSHPPol, -1, psObject); SHPDestroyObject(psObject); free(panPart); free(padfX); free(padfY); assert(shpRes != -1); if (hDBFPol == 0) { hDBFPol = TeCreateDBFFile(base + "_pol.dbf", qAttList); assert (hDBFPol != 0); } if (onlyObjId) { DBFWriteStringAttribute(hDBFPol, iRecPol, 0, st.objectId().c_str()); } else { string val; for (n=0; n<qAttList.size();++n) { st.getPropertyValue(val,n); if (qAttList[n].rep_.type_ == TeSTRING || qAttList[n].rep_.type_ == TeDATETIME) { DBFWriteStringAttribute(hDBFPol, iRecPol, n, val.c_str()); } else if (qAttList[n].rep_.type_ == TeINT) { DBFWriteIntegerAttribute(hDBFPol, iRecPol, n, atoi(val.c_str())); } else if (qAttList[n].rep_.type_ == TeREAL) { DBFWriteDoubleAttribute(hDBFPol, iRecPol, n, atof(val.c_str())); } } } ++iRecPol; } if (st.hasLines()) { TeLineSet& lineSet = st.getLines(); nVertices = lineSet.size(); TeLineSet::iterator itls; for (itls=lineSet.begin(); itls!=lineSet.end(); ++itls) totpoints += (*itls).size(); panPart = (int *) malloc(sizeof(int) * nVertices); padfX = (double *) malloc(sizeof(double) * totpoints); padfY = (double *) malloc(sizeof(double) * totpoints); posXY = 0; nelem = 0; for (itls=lineSet.begin(); itls!=lineSet.end(); ++itls) { panPart[nelem]=posXY; for (l=0; l<(*itls).size(); ++l) { padfX[posXY] = (*itls)[l].x(); padfY[posXY] = (*itls)[l].y(); ++posXY; } ++nelem; } psObject = SHPCreateObject(SHPT_ARC, -1, nVertices, panPart, NULL, posXY, padfX, padfY, NULL, NULL); if (hSHPLin == 0) { string fname = base + "_lin.shp"; hSHPLin = SHPCreate(fname.c_str(), SHPT_ARC); assert (hSHPLin != 0); } shpRes = SHPWriteObject(hSHPLin, -1, psObject); SHPDestroyObject(psObject); free(panPart); free(padfX); free(padfY); assert(shpRes != -1); if (hDBFLin == 0) { hDBFLin = TeCreateDBFFile(base + "_lin.dbf", qAttList); assert (hDBFLin != 0); } if (onlyObjId) { DBFWriteStringAttribute(hDBFLin, iRecLin, 0, st.objectId().c_str()); } else { string val; for (n=0; n<qAttList.size();++n) { st.getPropertyValue(val,n); if (qAttList[n].rep_.type_ == TeSTRING || qAttList[n].rep_.type_ == TeDATETIME) { DBFWriteStringAttribute(hDBFLin, iRecLin, n, val.c_str()); } else if (qAttList[n].rep_.type_ == TeINT) { DBFWriteIntegerAttribute(hDBFLin, iRecLin, n, atoi(val.c_str())); } else if (qAttList[n].rep_.type_ == TeREAL) { DBFWriteDoubleAttribute(hDBFLin, iRecLin, n, atof(val.c_str())); } } } ++iRecLin; } if (st.hasPoints()) { TePointSet& pointSet = st.getPoints(); nVertices = pointSet.size(); panPart = (int *) malloc(sizeof(int) * nVertices); padfX = (double *) malloc(sizeof(double) * nVertices); padfY = (double *) malloc(sizeof(double) * nVertices); nelem = 0; TePointSet::iterator itpts; for (itpts=pointSet.begin(); itpts!=pointSet.end(); ++itpts) { panPart[nelem] = nelem; padfX[nelem] = (*itpts).location().x(); padfY[nelem] = (*itpts).location().y(); ++nelem; } psObject = SHPCreateObject(SHPT_POINT, -1, nVertices, panPart, NULL, nVertices, padfX, padfY, NULL, NULL ); if (hSHPPt == 0) { string fname = base + "_pt.shp"; hSHPPt = SHPCreate(fname.c_str(), SHPT_POINT); assert (hSHPPt != 0); } shpRes = SHPWriteObject(hSHPPt, -1, psObject); SHPDestroyObject(psObject); free(panPart); free(padfX); free(padfY); assert(shpRes != -1); if (hDBFPt == 0) { hDBFPt = TeCreateDBFFile(base + "_pt.dbf", qAttList); assert (hDBFPt != 0); } if (onlyObjId) { DBFWriteStringAttribute(hDBFPt, iRecPt, 0, st.objectId().c_str()); } else { string val; for (n=0; n<qAttList.size();++n) { st.getPropertyValue(val,n); if (qAttList[n].rep_.type_ == TeSTRING || qAttList[n].rep_.type_ == TeDATETIME) { DBFWriteStringAttribute(hDBFPt, iRecPt, n, val.c_str()); } else if (qAttList[n].rep_.type_ == TeINT) { DBFWriteIntegerAttribute(hDBFPt, iRecPt, n, atoi(val.c_str())); } else if (qAttList[n].rep_.type_ == TeREAL) { DBFWriteDoubleAttribute(hDBFPt, iRecPt, n, atof(val.c_str())); } } } ++iRecPt; } ++nIProcessed; if (TeProgress::instance() && int(t2-t1) > dt) { t1 = t2; if(((int)(t2-t0) > dt2)) { if (TeProgress::instance()->wasCancelled()) break; else TeProgress::instance()->setProgress(nIProcessed); } } } if (hDBFPol != 0) DBFClose(hDBFPol); if (hSHPPol != 0) SHPClose(hSHPPol); if (hDBFLin != 0) DBFClose(hDBFLin); if (hSHPLin != 0) SHPClose(hSHPLin); if (hDBFPt != 0) DBFClose(hDBFPt); if (hSHPPt != 0) SHPClose(hSHPPt); if (TeProgress::instance()) TeProgress::instance()->reset(); return true; }
int main( int argc, char ** argv ) { SHPHandle hSHP; int nShapeType, nVertices, nParts, *panParts, i, nVMax; double *padfX, *padfY; SHPObject *psObject; /* -------------------------------------------------------------------- */ /* Display a usage message. */ /* -------------------------------------------------------------------- */ if( argc < 2 ) { printf( "shpadd shp_file [[x y] [+]]*\n" ); exit( 1 ); } /* -------------------------------------------------------------------- */ /* Open the passed shapefile. */ /* -------------------------------------------------------------------- */ hSHP = SHPOpen( argv[1], "r+b" ); if( hSHP == NULL ) { printf( "Unable to open:%s\n", argv[1] ); exit( 1 ); } SHPGetInfo( hSHP, NULL, &nShapeType, NULL, NULL ); if( argc == 2 ) nShapeType = SHPT_NULL; /* -------------------------------------------------------------------- */ /* Build a vertex/part list from the command line arguments. */ /* -------------------------------------------------------------------- */ nVMax = 1000; padfX = (double *) malloc(sizeof(double) * nVMax); padfY = (double *) malloc(sizeof(double) * nVMax); nVertices = 0; if( (panParts = (int *) malloc(sizeof(int) * 1000 )) == NULL ) { printf( "Out of memory\n" ); exit( 1 ); } nParts = 1; panParts[0] = 0; for( i = 2; i < argc; ) { if( argv[i][0] == '+' ) { panParts[nParts++] = nVertices; i++; } else if( i < argc-1 ) { if( nVertices == nVMax ) { nVMax = nVMax * 2; padfX = (double *) realloc(padfX,sizeof(double)*nVMax); padfY = (double *) realloc(padfY,sizeof(double)*nVMax); } sscanf( argv[i], "%lg", padfX+nVertices ); sscanf( argv[i+1], "%lg", padfY+nVertices ); nVertices += 1; i += 2; } } /* -------------------------------------------------------------------- */ /* Write the new entity to the shape file. */ /* -------------------------------------------------------------------- */ psObject = SHPCreateObject( nShapeType, -1, nParts, panParts, NULL, nVertices, padfX, padfY, NULL, NULL ); SHPWriteObject( hSHP, -1, psObject ); SHPDestroyObject( psObject ); SHPClose( hSHP ); free( panParts ); free( padfX ); free( padfY ); return 0; }
void shapefileClip::CreateShapeFile(vector<ossimPolygon> &polys, const char *shapeFilename) { if (polys.empty()) { cerr << "The polygons do not exist...cannot create shape file" << endl << "Line: " << __LINE__ << " " << __FILE__ << endl; return; } SHPObject *psObject; SHPHandle hSHP; double *px, *py; vector<ossimPolygon>::iterator polyIter; vector<ossimDpt> vertices; vector<ossimDpt>::iterator vertIter; hSHP = SHPCreate(shapeFilename, SHPT_POLYGON); if( hSHP == NULL || shapeFilename == NULL) { cerr << "Unable to create: " << shapeFilename << endl << "Line: " << __LINE__ << " " << __FILE__ << endl; exit(EXIT_FAILURE); } // Build DBF. DBFHandle hDBF; hDBF = DBFCreate(shapeFilename); if( hDBF == NULL) { cerr << "Unable to create: " << shapeFilename << " DBF File" << endl << "Line: " << __LINE__ << " " << __FILE__ << endl; exit(EXIT_FAILURE); } // Add fields. DBFAddField(hDBF, "poly_num", FTString, 30, 0); polyIter = polys.begin(); int arrayIndex; int poly_num = 0; while (polyIter != polys.end()) { DBFWriteStringAttribute(hDBF, poly_num, 0, ossimString::toString(poly_num)); vertices = (*polyIter).getVertexList(); vertIter = vertices.begin(); double *x = new double[vertices.size()]; double *y = new double[vertices.size()]; int *panParts = new int[vertices.size()]; panParts[0] = 0; // SHPCreateObject deletes these px = &x[0]; py = &y[0]; arrayIndex = 0; while (vertIter != vertices.end()) { x[arrayIndex] = (*vertIter).x; y[arrayIndex++] = (*vertIter).y; vertIter++; } psObject = SHPCreateObject( SHPT_POLYGON, -1, 1, panParts, NULL, vertices.size(), px, py, NULL, NULL ); if (psObject != NULL) { SHPWriteObject( hSHP, -1, psObject ); SHPDestroyObject( psObject ); } else { cerr << "Shape object could not be written to file" << endl << "Line: " << __LINE__ << " " << __FILE__ << endl; SHPClose(hSHP); delete []x; delete []y; delete [] panParts; exit(EXIT_FAILURE); } polyIter++; poly_num++; delete []x; delete []y; delete [] panParts; } DBFClose(hDBF); SHPClose( hSHP ); }
bool _curv_save_shp(const d_curv * contour, const char * filename) { if (!contour->getName()) writelog(LOG_MESSAGE,"saving curv to ERSI shape file %s", filename); else writelog(LOG_MESSAGE,"saving curv \"%s\" to ERSI shape file %s", contour->getName(), filename); if (!contour) { writelog(LOG_WARNING, "NULL pointer to curv."); return false; }; DBFHandle hDBF; SHPHandle hSHP; hSHP = SHPOpen( filename, "rb+" ); hDBF = DBFOpen( filename, "rb+" ); if (hSHP == NULL || hDBF == NULL) { if (hSHP) SHPClose( hSHP ); if (hDBF) DBFClose( hDBF ); hSHP = SHPCreate( filename, SHPT_ARC ); if( hSHP == NULL ) { writelog(LOG_ERROR, "Unable to create:%s", filename ); return false; } hDBF = DBFCreate( filename ); } int shpType; SHPGetInfo(hSHP, NULL, &shpType, NULL, NULL); if (shpType != SHPT_ARC) { writelog(LOG_ERROR, "%s : Wrong shape type! Should be SHPT_ARC.", filename); SHPClose( hSHP ); DBFClose( hDBF ); return false; } int name_field = DBFGetFieldIndex( hDBF, "NAME" ); if (name_field == -1) { if( DBFAddField( hDBF, "NAME", FTString, 50, 0 ) == -1 ) { writelog(LOG_ERROR, "DBFAddField(%s,FTString) failed.", "NAME"); SHPClose( hSHP ); DBFClose( hDBF ); return false; } name_field = DBFGetFieldIndex( hDBF, "NAME" ); }; std::vector<REAL> data_x(contour->size()); std::vector<REAL> data_y(contour->size()); std::copy(contour->X->begin(), contour->X->end(), data_x.begin()); std::copy(contour->Y->begin(), contour->Y->end(), data_y.begin()); SHPObject * psObject = SHPCreateObject(SHPT_ARC, -1, 0, NULL, NULL, contour->size(), &*(data_x.begin()), &*(data_y.begin()), NULL, NULL); SHPComputeExtents(psObject); int pos = SHPWriteObject(hSHP, -1, psObject); SHPDestroyObject(psObject); if (contour->getName()) DBFWriteStringAttribute(hDBF, pos, name_field, contour->getName() ); SHPClose( hSHP ); DBFClose( hDBF ); return true; };
bool TeExportPolygonSet2SHP( const TePolygonSet& ps,const std::string& base_file_name ) { // creating files names std::string dbfFilename = base_file_name + ".dbf"; std::string shpFilename = base_file_name + ".shp"; // creating polygons attribute list ( max attribute size == 25 ) TeAttributeList attList; TeAttribute at; at.rep_.type_ = TeSTRING; //the id of the cell at.rep_.numChar_ = 25; at.rep_.name_ = "object_id_"; at.rep_.isPrimaryKey_ = true; attList.push_back(at); /* DBF output file handle creation */ DBFHandle hDBF = TeCreateDBFFile (dbfFilename, attList); if ( hDBF == 0 ) return false; /* SHP output file handle creation */ SHPHandle hSHP = SHPCreate( shpFilename.c_str(), SHPT_POLYGON ); if( hSHP == 0 ) { DBFClose( hDBF ); return false; } /* Writing polygons */ int iRecord = 0; int totpoints = 0; double *padfX, *padfY; SHPObject *psObject; int posXY, npoints, nelem; int nVertices; int* panParts; TePolygonSet::iterator itps; TePolygon poly; for (itps = ps.begin() ; itps != ps.end() ; itps++ ) { poly=(*itps); totpoints = 0; nVertices = poly.size(); for (unsigned int n=0; n<poly.size();n++) { totpoints += poly[n].size(); } panParts = (int *) malloc(sizeof(int) * nVertices); padfX = (double *) malloc(sizeof(double) * totpoints); padfY = (double *) malloc(sizeof(double) * totpoints); posXY = 0; nelem = 0; for (unsigned int l=0; l<poly.size(); ++l) { if (l==0) { if (TeOrientation(poly[l]) == TeCOUNTERCLOCKWISE) { TeReverseLine(poly[l]); } } else { if (TeOrientation(poly[l]) == TeCLOCKWISE) { TeReverseLine(poly[l]); } } npoints = poly[l].size(); panParts[nelem]=posXY; for (int m=0; m<npoints; m++ ) { padfX[posXY] = poly[l][m].x_; padfY[posXY] = poly[l][m].y_; posXY++; } nelem++; } psObject = SHPCreateObject( SHPT_POLYGON, -1, nelem, panParts, NULL, posXY, padfX, padfY, NULL, NULL ); int shpRes = SHPWriteObject( hSHP, -1, psObject ); if (shpRes == -1 ) { DBFClose( hDBF ); SHPClose( hSHP ); return false; } SHPDestroyObject( psObject ); free( panParts ); free( padfX ); free( padfY ); // writing attributes - same creation order DBFWriteStringAttribute(hDBF, iRecord, 0, poly.objectId().c_str() ); iRecord++; } DBFClose( hDBF ); SHPClose( hSHP ); return true; }
bool _pnts_save_shp(const d_points * pnts, const char * filename) { if (!pnts) { writelog(LOG_WARNING, "NULL pointer to points."); return false; }; DBFHandle hDBF; SHPHandle hSHP; hSHP = SHPOpen( filename, "rb+" ); hDBF = DBFOpen( filename, "rb+" ); if (hSHP == NULL || hDBF == NULL) { if (hSHP) SHPClose( hSHP ); if (hDBF) DBFClose( hDBF ); hSHP = SHPCreate( filename, SHPT_POINT ); if( hSHP == NULL ) { writelog(LOG_ERROR, "Unable to create:%s", filename ); return false; } hDBF = DBFCreate( filename ); } int shpType; SHPGetInfo(hSHP, NULL, &shpType, NULL, NULL); if (shpType != SHPT_POINT) { writelog(LOG_ERROR, "%s : Wrong shape type!", filename); SHPClose( hSHP ); DBFClose( hDBF ); return false; } int name_field = DBFGetFieldIndex( hDBF, "NAME" ); int val_field = DBFGetFieldIndex( hDBF, "VALUE" ); if (name_field == -1) { if( DBFAddField( hDBF, "NAME", FTString, 50, 0 ) == -1 ) { writelog(LOG_ERROR, "DBFAddField(%s,FTString) failed.", "NAME"); SHPClose( hSHP ); DBFClose( hDBF ); return false; } name_field = DBFGetFieldIndex( hDBF, "NAME" ); } if (val_field == -1) { if( DBFAddField( hDBF, "VALUE", FTDouble, 50, 0 ) == -1 ) { writelog(LOG_ERROR, "DBFAddField(%s,FTString) failed.", "VALUE"); SHPClose( hSHP ); DBFClose( hDBF ); return false; } val_field = DBFGetFieldIndex( hDBF, "VALUE" ); } char buf[1024]; size_t i; for (i = 0; i < pnts->size(); i++) { double x = (*(pnts->X))(i); double y = (*(pnts->Y))(i); SHPObject * psObject = SHPCreateObject(SHPT_POINT, -1, 0, NULL, NULL, 1, &x, &y, NULL, NULL); SHPComputeExtents(psObject); int pos = SHPWriteObject(hSHP, -1, psObject); SHPDestroyObject(psObject); if (pnts->names) DBFWriteStringAttribute(hDBF, pos, name_field, (*(pnts->names))(i) ); else { sprintf(buf,"%d",(int)i); DBFWriteStringAttribute(hDBF, pos, name_field, buf ); } REAL val = (*(pnts->Z))(i); DBFWriteDoubleAttribute(hDBF, pos, val_field, val); } SHPClose( hSHP ); DBFClose( hDBF ); return true; };
/* * Write the geometry for a set of polygons to a shape file * This needs to map between our internal structure for the * shape polygons and that expected by the shape file writer. */ int polyShapeWrite(PolyObject * poly /* set of polygons */ , char *name /* name of file to write to */ ) { SHPHandle hSHP; SHPObject *psObject; Vertex *v; PolyShape *ps; PolyShapeList *plist; static int v_size; static int p_size; int i, j, k, n; int np, nv; int nShapeType; int m; int nVertices; int *partsStart; double *vx, *vy; if(v_size == 0) { v_size = 1024; /* * allocate space for each of the vertices */ vx = (double *) malloc(2 * v_size * sizeof(double)); if(vx == NULL) { WARN("Memory allocation error in polyShapeWrite"); return 1; } vy = vx + v_size; } if(p_size == 0) { p_size = 1024; partsStart = (int *) malloc(p_size * sizeof(int)); if(partsStart == NULL) { WARN("Memory allocation error in polyShapeWrite"); return 1; } } nShapeType = poly->nSHPType; hSHP = SHPCreate(name, nShapeType); if(hSHP == NULL) { WARN2("Unable to create ", name); return 1; } /* * make sure space is available for all vertices - if it gets full, * reallocate with double the size */ n = poly->nObjects; plist = poly->plist; /* * loop over all polygons */ for(i = 0; i < n; i++) { ps = plist->ps; /* debug code BDB */ /*if(PolyArea(ps) < 0) printf("In polyShapeWrite area is negative for shape %d\n", i); */ np = ps->num_contours; nVertices = 0; while(np > p_size) { p_size *= 2; partsStart = (int *) realloc(partsStart, 2 * p_size * sizeof(int)); if(partsStart == NULL) { WARN("Memory allocation error in polyShapeWrite"); return 1; } } partsStart[0] = 0; /* * population of parts field for ShapeFiles */ for(j = 0; j < np; j++) { nv = ps->contour[j].num_vertices; nVertices += nv; partsStart[j + 1] = nVertices; } while(nVertices > v_size) { v_size *= 2; vx = (double *) realloc(vx, 2 * v_size * sizeof(double)); if(vx == NULL) { WARN("Memory allocation error in polyShapeWrite"); return 1; } vy = vx + v_size; } /* * population of vertices field for ShapeFiles */ m = 0; for(j = 0; j < np; j++) { nv = ps->contour[j].num_vertices; v = ps->contour[j].vertex; for(k = 0; k < nv; k++) { vx[m] = v[k].x; vy[m] = v[k].y; m++; } } /* * write a polygon to the .shp file */ psObject = SHPCreateObject(nShapeType, -1, np, partsStart, NULL, nVertices, vx, vy, NULL, NULL); SHPWriteObject(hSHP, -1, psObject); SHPDestroyObject(psObject); plist = plist->next; /* go to next polygon */ } /* end loop over polygons */ SHPClose(hSHP); return 0; } /* end of polyShapeWrite function */
void Builder::print_shpObjects() { QgsDebugMsg( QString( "Number of primitives: %1" ).arg( shpObjects.size() ) ); QgsDebugMsg( QString( "Number of text fields: %1" ).arg( textObjects.size() ) ); QgsDebugMsg( QString( "Number of inserts fields: %1" ).arg( insertObjects.size() ) ); SHPHandle hSHP; if ( fname.endsWith( ".shp", Qt::CaseInsensitive ) ) { QString fn( fname.mid( fname.length() - 4 ) ); outputdbf = fn + ".dbf"; outputshp = fn + ".shp"; outputtdbf = fn + "_texts.dbf"; outputtshp = fn + "_texts.shp"; outputidbf = fn + "_inserts.dbf"; outputishp = fn + "_inserts.shp"; } else { outputdbf = outputtdbf = outputidbf = fname + ".dbf"; outputshp = outputtshp = outputishp = fname + ".shp"; } DBFHandle dbffile = DBFCreate( outputdbf.toUtf8() ); DBFAddField( dbffile, "myid", FTInteger, 10, 0 ); hSHP = SHPCreate( outputshp.toUtf8(), shapefileType ); QgsDebugMsg( "Writing to main shp file..." ); for ( int i = 0; i < shpObjects.size(); i++ ) { SHPWriteObject( hSHP, -1, shpObjects[i] ); SHPDestroyObject( shpObjects[i] ); DBFWriteIntegerAttribute( dbffile, i, 0, i ); } SHPClose( hSHP ); DBFClose( dbffile ); QgsDebugMsg( "Done!" ); if ( !textObjects.isEmpty() ) { SHPHandle thSHP; DBFHandle Tdbffile = DBFCreate( outputtdbf.toUtf8() ); thSHP = SHPCreate( outputtshp.toUtf8(), SHPT_POINT ); DBFAddField( Tdbffile, "tipx", FTDouble, 20, 10 ); DBFAddField( Tdbffile, "tipy", FTDouble, 20, 10 ); DBFAddField( Tdbffile, "tipz", FTDouble, 20, 10 ); DBFAddField( Tdbffile, "tapx", FTDouble, 20, 10 ); DBFAddField( Tdbffile, "tapy", FTDouble, 20, 10 ); DBFAddField( Tdbffile, "tapz", FTDouble, 20, 10 ); DBFAddField( Tdbffile, "height", FTDouble, 20, 10 ); DBFAddField( Tdbffile, "scale", FTDouble, 20, 10 ); DBFAddField( Tdbffile, "flags", FTInteger, 10, 0 ); DBFAddField( Tdbffile, "hjust", FTInteger, 10, 0 ); DBFAddField( Tdbffile, "vjust", FTInteger, 10, 0 ); DBFAddField( Tdbffile, "text", FTString, 50, 0 ); DBFAddField( Tdbffile, "style", FTString, 50, 0 ); DBFAddField( Tdbffile, "angle", FTDouble, 20, 10 ); QgsDebugMsg( "Writing Texts' shp File..." ); for ( int i = 0; i < textObjects.size(); i++ ) { SHPObject *psObject; double x = textObjects[i].ipx; double y = textObjects[i].ipy; double z = textObjects[i].ipz; psObject = SHPCreateObject( SHPT_POINT, i, 0, NULL, NULL, 1, &x, &y, &z, NULL ); SHPWriteObject( thSHP, -1, psObject ); DBFWriteDoubleAttribute( Tdbffile, i, 0, textObjects[i].ipx ); DBFWriteDoubleAttribute( Tdbffile, i, 1, textObjects[i].ipy ); DBFWriteDoubleAttribute( Tdbffile, i, 2, textObjects[i].ipz ); DBFWriteDoubleAttribute( Tdbffile, i, 3, textObjects[i].apx ); DBFWriteDoubleAttribute( Tdbffile, i, 4, textObjects[i].apy ); DBFWriteDoubleAttribute( Tdbffile, i, 5, textObjects[i].apz ); DBFWriteDoubleAttribute( Tdbffile, i, 6, textObjects[i].height ); DBFWriteDoubleAttribute( Tdbffile, i, 7, textObjects[i].xScaleFactor ); DBFWriteIntegerAttribute( Tdbffile, i, 8, textObjects[i].textGenerationFlags ); DBFWriteIntegerAttribute( Tdbffile, i, 9, textObjects[i].hJustification ); DBFWriteIntegerAttribute( Tdbffile, i, 10, textObjects[i].vJustification ); DBFWriteStringAttribute( Tdbffile, i, 11, textObjects[i].text.c_str() ); DBFWriteStringAttribute( Tdbffile, i, 12, textObjects[i].style.c_str() ); DBFWriteDoubleAttribute( Tdbffile, i, 13, textObjects[i].angle ); SHPDestroyObject( psObject ); } SHPClose( thSHP ); DBFClose( Tdbffile ); QgsDebugMsg( "Done!" ); } if ( !insertObjects.isEmpty() ) { SHPHandle ihSHP; DBFHandle Idbffile = DBFCreate( outputidbf.toUtf8() ); ihSHP = SHPCreate( outputishp.toUtf8(), SHPT_POINT ); DBFAddField( Idbffile, "name", FTString, 200, 0 ); DBFAddField( Idbffile, "ipx", FTDouble, 20, 10 ); DBFAddField( Idbffile, "ipy", FTDouble, 20, 10 ); DBFAddField( Idbffile, "ipz", FTDouble, 20, 10 ); DBFAddField( Idbffile, "sx", FTDouble, 20, 10 ); DBFAddField( Idbffile, "sy", FTDouble, 20, 10 ); DBFAddField( Idbffile, "sz", FTDouble, 20, 10 ); DBFAddField( Idbffile, "angle", FTDouble, 20, 10 ); DBFAddField( Idbffile, "cols", FTInteger, 20, 0 ); DBFAddField( Idbffile, "rows", FTInteger, 20, 0 ); DBFAddField( Idbffile, "colsp", FTDouble, 20, 10 ); DBFAddField( Idbffile, "rowsp", FTDouble, 20, 10 ); QgsDebugMsg( "Writing Insert' shp File..." ); for ( int i = 0; i < insertObjects.size(); i++ ) { SHPObject *psObject; double &x = insertObjects[i].ipx; double &y = insertObjects[i].ipy; double &z = insertObjects[i].ipz; psObject = SHPCreateObject( SHPT_POINT, i, 0, NULL, NULL, 1, &x, &y, &z, NULL ); SHPWriteObject( ihSHP, -1, psObject ); int c = 0; DBFWriteStringAttribute( Idbffile, i, c++, insertObjects[i].name.c_str() ); DBFWriteDoubleAttribute( Idbffile, i, c++, insertObjects[i].ipx ); DBFWriteDoubleAttribute( Idbffile, i, c++, insertObjects[i].ipy ); DBFWriteDoubleAttribute( Idbffile, i, c++, insertObjects[i].ipz ); DBFWriteDoubleAttribute( Idbffile, i, c++, insertObjects[i].sx ); DBFWriteDoubleAttribute( Idbffile, i, c++, insertObjects[i].sy ); DBFWriteDoubleAttribute( Idbffile, i, c++, insertObjects[i].sz ); DBFWriteDoubleAttribute( Idbffile, i, c++, insertObjects[i].angle ); DBFWriteIntegerAttribute( Idbffile, i, c++, insertObjects[i].cols ); DBFWriteIntegerAttribute( Idbffile, i, c++, insertObjects[i].rows ); DBFWriteDoubleAttribute( Idbffile, i, c++, insertObjects[i].colSp ); DBFWriteDoubleAttribute( Idbffile, i, c++, insertObjects[i].rowSp ); SHPDestroyObject( psObject ); } SHPClose( ihSHP ); DBFClose( Idbffile ); QgsDebugMsg( "Done!" ); } }
void Builder::addArc( const DL_ArcData& data ) { if ( shapefileType != SHPT_ARC ) { QgsDebugMsg( "ignoring arc" ); return; } int fromAngle = ( int ) data.angle1 + 1; int toAngle = ( int ) data.angle2 + 1; QgsDebugMsg( QString( "arc (%1,%2,%3 r=%4 a1=%5 a2=%6)" ) .arg( data.cx ).arg( data.cy ).arg( data.cz ) .arg( data.radius ) .arg( data.angle1 ).arg( data.angle2 ) ); if ( ignoringBlock ) { QgsDebugMsg( "skipping arc in block" ); return; } register int i = 0; register long shpIndex = 0; // Approximate the arc double radianMeasure; std::vector <DL_PointData> arcPoints; DL_PointData myPoint; for ( i = fromAngle; ; i++, shpIndex++ ) { if ( i > 360 ) i = 0; if ( shpIndex > 1000 ) break; radianMeasure = i * M_PI / 180.0; myPoint.x = data.radius * cos( radianMeasure ) + data.cx + currentBlockX; myPoint.y = data.radius * sin( radianMeasure ) + data.cy + currentBlockY; myPoint.z = data.cz; arcPoints.push_back( myPoint ); if ( i == toAngle ) break; } // Finalize SHPObject *psShape; int dim = arcPoints.size(); double *xv = new double[dim]; double *yv = new double[dim]; double *zv = new double[dim]; for ( int i = 0; i < dim; i++ ) { xv[i] = arcPoints[i].x; yv[i] = arcPoints[i].y; zv[i] = arcPoints[i].z; } psShape = SHPCreateObject( shapefileType, fetchedprims, 0, NULL, NULL, dim, xv, yv, zv, NULL ); delete [] xv; delete [] yv; delete [] zv; shpObjects.push_back( psShape ); fetchedprims++; arcPoints.clear(); }
/////////////////////////////////////////////////////////////////////////////// // Export eines Objektes HRESULT CArcViewLayer::ExportData ( GSTRUCT *pGS, MFELD *pMF, LPCSTR pcUIdent, CArcViewLayerAttributes *pMap) { OBJECTTYPE rgType = GetType(); int iShapeId = -1; int iObjTyp = pGS -> Typ; _ASSERTE(rgType == ObjType2OBJECTTYPE(pGS -> Typ, true)); // Objekttyp muß stimmen // Geometrie erzeugen if (OBJECTTYPE_Area == rgType) { // Anzahl der Konturen feststellen und Konturfeld zusammenbauen int iKCnt = 1; vector<int> Cnts(1); int iCurr = 0; Cnts[0] = 0; for (int i = 0; 0 != pGS -> cnt[i]; ++i) { if (i > 0) Cnts.push_back(iCurr); iCurr += pGS -> cnt[i]; } iKCnt = i; _ASSERTE(iKCnt > 0 && iKCnt == Cnts.size()); // Objekt erzeugen CArcViewObject Obj(SHPCreateObject(m_nShapeType, -1, iKCnt, Cnts.begin(), NULL, pGS -> GSize, pGS -> x, pGS -> y, NULL, NULL)); iShapeId = SHPWriteObject(m_hSHP, -1, Obj); } else { // Objekt erzeugen CArcViewObject Obj(SHPCreateSimpleObject(m_nShapeType, pGS -> GSize, pGS -> x, pGS -> y, NULL)); iShapeId = SHPWriteObject(m_hSHP, -1, Obj); } if (iShapeId == -1) return E_FAIL; // Attribute sicherstellen (bei TRiAS-Datenquellen jedesmal) if (!HasFields() || DEX_GetTRiASDataSourceEx(DEX_GetObjectsProject(pGS -> Id))) { // sämtliche Attribute dieses Layers durchgehen und ggf. erzeugen for (CArcViewLayerAttributes::iterator it = pMap -> begin(); it != pMap -> end(); ++it) { // Feld in Datei erzeugen int iField = -1; CArcViewAttribute &rAttr = (*it).second; LPCSTR pcName = rAttr.GetName(); if (OBJECTTYPE_Text != m_rgType && !strcmp (pcName, g_cbLabelText)) continue; // Labeltext nur für Textobjekte if (FAILED(FieldExists (pcName, rAttr.GetTyp(), &iField))) { RETURN_FAILED_HRESULT(AddField(pcName, rAttr.GetTyp(), rAttr.GetLen(), rAttr.GetDecimals(), &iField)); } _ASSERTE(-1 != iField); // mehrerer Objekttypen eines Idents haben identischen Satz von Attributen _ASSERTE(-1 == (*it).second.GetField(iObjTyp) || (*it).second.GetField(iObjTyp) == iField || DEX_GetTRiASDataSourceEx(DEX_GetObjectsProject(pGS -> Id))); // Feldnummer beim Attribut speichern (*it).second.SetField(iField, iObjTyp); } // Textlabel für Textobjekte if (OBJECTTYPE_Text == m_rgType) { pair<CArcViewLayerAttributes::iterator, bool> p = pMap -> insert (CArcViewLayerAttributes::value_type (-1, CArcViewAttribute(g_cbLabelText, 'a', _MAX_PATH))); if (p.second) { int iField = -1; it = p.first; if (FAILED(FieldExists (g_cbLabelText, FTString, &iField))) { RETURN_FAILED_HRESULT(AddField(g_cbLabelText, FTString, _MAX_PATH, 0, &iField)); } _ASSERTE(-1 != iField); (*it).second.SetField(iField, iObjTyp); } } SetHasFields(); } // Attributwerte schreiben if (NULL != pMF) { // nur, wenn mindestens ein Attribut ausgegeben werden soll for (MFELD *pMFT = pMF; 0 != pMFT -> MCode; ++pMFT) { if (NULL != pMap) { CArcViewLayerAttributes::iterator it = pMap -> find (pMFT -> MCode); _ASSERTE(it != pMap -> end()); // Attribut sollte (jetzt) existieren if (it != pMap -> end()) { // Feld muß bereits erzeugt worden sein und Typ muß mit DBF übereinstimmen int iField = (*it).second.GetField(iObjTyp); _ASSERTE(-1 != iField); _ASSERTE((*it).second.GetTyp() == DBFGetFieldInfo (m_hDBF, iField, NULL, NULL, NULL)); // Wert je nach Typ in die Datenbank schreiben switch ((*it).second.GetTyp()) { case FTString: { char cbBuffer[_MAX_PATH] = { '\0' }; if (NULL != pMFT -> MText) OemToCharBuff(pMFT -> MText, cbBuffer, min(MAX_DBASEFIELD_LEN, strlen(pMFT -> MText))+1); // '\0' mit konvertieren DBFWriteStringAttribute(m_hDBF, iShapeId, iField, cbBuffer); } break; case FTInteger: DBFWriteIntegerAttribute(m_hDBF, iShapeId, iField, (NULL != pMFT -> MText) ? atol(pMFT -> MText) : 0); break; case FTDouble: DBFWriteDoubleAttribute(m_hDBF, iShapeId, iField, (NULL != pMFT -> MText) ? atof(pMFT -> MText) : 0.0); break; } } } else { _ASSERTE(NULL != pMap); // eigentlich sollte eine Map da sein // keine Map, also erstmal leeren Datensatz schreiben int iCnt = DBFGetFieldCount(m_hDBF); for (int i = 0; i < iCnt; ++i) { switch (DBFGetFieldInfo(m_hDBF, i, NULL, NULL, NULL)) { case FTString: DBFWriteStringAttribute(m_hDBF, iShapeId, i, g_cbNil); break; case FTInteger: DBFWriteIntegerAttribute(m_hDBF, iShapeId, i, 0); break; case FTDouble: DBFWriteDoubleAttribute(m_hDBF, iShapeId, i, 0.0); break; } } } } } return S_OK; }
int main( int argc, char ** argv ) { SHPHandle hSHP; int nShapeType, nVertices, nParts, *panParts, i, nVMax; double *padfX, *padfY, *padfZ = NULL, *padfM = NULL; SHPObject *psObject; const char *tuple = ""; const char *filename; /* -------------------------------------------------------------------- */ /* Display a usage message. */ /* -------------------------------------------------------------------- */ if( argc < 2 ) { printf( "shpadd shp_file [[x y] [+]]*\n" ); printf( " or\n" ); printf( "shpadd shp_file -m [[x y m] [+]]*\n" ); printf( " or\n" ); printf( "shpadd shp_file -z [[x y z] [+]]*\n" ); printf( " or\n" ); printf( "shpadd shp_file -zm [[x y z m] [+]]*\n" ); exit( 1 ); } filename = argv[1]; argv++; argc--; /* -------------------------------------------------------------------- */ /* Check for tuple description options. */ /* -------------------------------------------------------------------- */ if( argc > 1 && (strcmp(argv[1],"-z") == 0 || strcmp(argv[1],"-m") == 0 || strcmp(argv[1],"-zm") == 0) ) { tuple = argv[1] + 1; argv++; argc--; } /* -------------------------------------------------------------------- */ /* Open the passed shapefile. */ /* -------------------------------------------------------------------- */ hSHP = SHPOpen( filename, "r+b" ); if( hSHP == NULL ) { printf( "Unable to open:%s\n", filename ); exit( 1 ); } SHPGetInfo( hSHP, NULL, &nShapeType, NULL, NULL ); if( argc == 1 ) nShapeType = SHPT_NULL; /* -------------------------------------------------------------------- */ /* Build a vertex/part list from the command line arguments. */ /* -------------------------------------------------------------------- */ nVMax = 1000; padfX = (double *) malloc(sizeof(double) * nVMax); padfY = (double *) malloc(sizeof(double) * nVMax); if( strchr(tuple,'z') ) padfZ = (double *) malloc(sizeof(double) * nVMax); if( strchr(tuple,'m') ) padfM = (double *) malloc(sizeof(double) * nVMax); nVertices = 0; if( (panParts = (int *) malloc(sizeof(int) * 1000 )) == NULL ) { printf( "Out of memory\n" ); exit( 1 ); } nParts = 1; panParts[0] = 0; for( i = 1; i < argc; ) { if( argv[i][0] == '+' ) { panParts[nParts++] = nVertices; i++; } else if( i < argc-1-strlen(tuple) ) { if( nVertices == nVMax ) { nVMax = nVMax * 2; padfX = (double *) realloc(padfX,sizeof(double)*nVMax); padfY = (double *) realloc(padfY,sizeof(double)*nVMax); if( padfZ ) padfZ = (double *) realloc(padfZ,sizeof(double)*nVMax); if( padfM ) padfM = (double *) realloc(padfM,sizeof(double)*nVMax); } sscanf( argv[i++], "%lg", padfX+nVertices ); sscanf( argv[i++], "%lg", padfY+nVertices ); if( padfZ ) sscanf( argv[i++], "%lg", padfZ+nVertices ); if( padfM ) sscanf( argv[i++], "%lg", padfM+nVertices ); nVertices += 1; } } /* -------------------------------------------------------------------- */ /* Write the new entity to the shape file. */ /* -------------------------------------------------------------------- */ psObject = SHPCreateObject( nShapeType, -1, nParts, panParts, NULL, nVertices, padfX, padfY, padfZ, padfM ); SHPWriteObject( hSHP, -1, psObject ); SHPDestroyObject( psObject ); SHPClose( hSHP ); free( panParts ); free( padfX ); free( padfY ); free( padfZ ); free( padfM ); return 0; }