[gdal-dev] how to store a shapefile afterobtaining from intersection of two different shapeFiles
SIVA RAMA KRISHNA
s.r.kriishna at gmail.com
Thu Jun 7 23:08:49 PDT 2012
mywindow::mywindow(QWidget *parent) : QMainWindow(parent){
view=new QGraphicsView(this); view->repaint(); scene=new
QGraphicsScene(); OGRRegisterAll(); scaleFactor=0.01; //poDS
= OGRSFDriverRegistrar::Open( "admin.shp"); //poLayer =
poDS->GetLayerByName( "admin"); poDS = OGRSFDriverRegistrar::Open(
"india_st.shp"); poLayer = poDS->*GetLayerByName*( "india_st");
poDS1=OGRSFDriverRegistrar::Open("india_ds.shx");
poLayer1=poDS1->*GetLayerByName*("india_ds");
poLayer->*ResetReading*(); poLayer1->*ResetReading*();
view->setRenderHint(QPainter::Antialiasing);
while(((poFeature=poLayer->*GetNextFeature*())!=NULL) &&
((poFeature1=poLayer1->*GetNextFeature*())!=NULL)) {
poGeometry=poFeature->GetGeometryRef();
poGeometry1=poFeature1->GetGeometryRef(); OGREnvelope *
psEnvelope1 = new OGREnvelope(); OGREnvelope * psEnvelope2 =
new OGREnvelope(); poGeometry->*getEnvelope*(psEnvelope1);
poGeometry1->*getEnvelope*(psEnvelope2); qDebug() <<
psEnvelope1->MinX << psEnvelope1->MinY << psEnvelope1->MaxX <<
psEnvelope1->MaxY; qDebug() << psEnvelope2->MinX <<
psEnvelope2->MinY << psEnvelope2->MaxX << psEnvelope2->MaxY;
if (this->Intersects1(poGeometry,poGeometry1)==1) {
QPainter painter2;
poGeometry2=poGeometry->*Intersection*(poGeometry1);
if(poGeometry2 !=NULL &&
wkbFlatten(poGeometry2->*getGeometryType*()) == wkbPolygon)
{ qDebug()<<"1yes"; polyShape2 =
(OGRPolygon*) poGeometry2;
drawPolyShape(painter2,polyShape2,MBR,scaleFactor); }
} this->setCentralWidget(view); }//end of while loop
QString filename3,projString; filename3="india_st_out.shp";
OGRwkbGeometryType type; ShapeFileWriter( filename3,type,
projString);}//end of main
OGRBoolean mywindow::Intersects1(OGRGeometry *poGeom, OGRGeometry
*poOtherGeom ) const{ // this is for obtaining a region for
interscetion}
void mywindow::drawPolyShape(QPainter& painter,OGRPolygon*
polyShape,const QRectF& MBR, const double& scaleFactor){ // for
drawing a shape file
}
void mywindow::drawClosedLinearRing(QPainterPath&
painter,OGRLinearRing* linearRing,
const QRectF& MBR, const double& scaleFactor){ // for
drawing a shape file
}
void mywindow::ShapeFileWriter(QString filename,OGRwkbGeometryType
type,QString projString)// program to write to shape file {
QFileInfo info(filename); QString baseFileName =
info.absolutePath(); //+ QDir::separator(); baseFileName +=
info.baseName(); QFile fileshx(baseFileName + ".shx");
QFile fileshp(baseFileName + ".shp"); QFile
filedbf(baseFileName + ".dbf"); QFile fileprj(baseFileName +
".prj"); if(fileshp.exists()) {
fileshp.remove(); } if(fileshx.exists()) {
fileshx.remove(); } if(filedbf.exists()) {
filedbf.remove(); } if(fileprj.exists())
{ fileprj.remove(); } QString fileName =
filename; QStringList list; const char *pszDriverName =
"ESRI Shapefile"; OGRSFDriver *poDriver;
OGRRegisterAll(); poDriver =
OGRSFDriverRegistrar::GetRegistrar()->GetDriverByName(
pszDriverName ); if( poDriver == NULL ) {
return; } poDS =
poDriver->*CreateDataSource*(filename.toLatin1().data(), NULL );
if( poDS == NULL ) { return; }
filename.truncate(fileName.length()-4); poLayer =
poDS->*CreateLayer*(filename.toLatin1().data(), NULL, type, NULL);
if( poLayer == NULL ) { return; }
if(!projString.isEmpty() && !projString.isNull()) {
filename = filename + ".prj"; QFile data(filename);
if (data.*open*(QFile::WriteOnly | QFile::Truncate)) {
QTextStream out(&data);
out<<projString<<"\n"; list.append(projString);
} data.*close*(); }
int rec;//
rec=identifyRecord(psEnvelope3->MinX,psEnvelope3->MinY,psEnvelope3->MaxX
); int x; x=addRecord(poGeometry2,list); int y;
y=getNumofRecords(); qDebug()<<y; }int
mywindow::addRecord(OGRGeometry* geometry,QStringList list) {
int fID; OGRFeature *poFeature; poFeature =
OGRFeature::CreateFeature( poLayer->*GetLayerDefn*() );
poFeature->SetField( "ID", (int)poFeature->GetFID());
poFeature->SetGeometry( geometry ); for(int i = 0; i <
list.size(); i++) { qDebug()<<"ADDED";
poFeature->SetField( i, list.at(i).toDouble()); } if(
poLayer->*CreateFeature*( poFeature ) != OGRERR_NONE ) {
return -1; } fID = (int)poFeature->GetFID();
OGRFeature::DestroyFeature( poFeature ); return fID; }
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://lists.osgeo.org/pipermail/gdal-dev/attachments/20120608/1fd054ae/attachment-0001.html>
More information about the gdal-dev
mailing list