Programování s knihovnou OGR: Porovnání verzí
m →C++ |
m →C++ |
||
Řádek 8: | Řádek 8: | ||
<source lang="cpp"> | <source lang="cpp"> | ||
#include <iostream> | #include <iostream> | ||
#include <ctime> | |||
#include "ogrsf_frmts.h" | #include "ogrsf_frmts.h" | ||
using std::cerr; | using std::cerr; | ||
using std::endl; | using std::endl; | ||
using std::cout; | |||
OGRDataSource *open_dsn(const char *dsn) | |||
{ | |||
OGRDataSource *poDs; | |||
poDs = OGRSFDriverRegistrar::Open(dsn, FALSE); | |||
if (!poDs) | |||
cerr << "Otevreni " << dsn << " selhalo." << std::endl; | |||
return poDs; | |||
} | |||
int compute_number(OGRLayer *poLayer1, OGRLayer *poLayer2) | |||
{ | |||
int count, i; | |||
int nfeat; | |||
bool found; | |||
OGRFeature *poFeat1, *poFeat2; | |||
OGRGeometry *poGeom1, *poGeom2; | |||
count = 0; | |||
nfeat = poLayer2->GetFeatureCount(); | |||
/* sekvencne cti prvky z vsrtvy 2 (polygony) */ | |||
poLayer2->ResetReading(); | |||
i = 0; | |||
while(true) { | |||
cerr << "\r" << i << "/" << nfeat; | |||
poFeat2 = poLayer2->GetNextFeature(); | |||
if (!poFeat2) | |||
break; | |||
poGeom2 = poFeat2->GetGeometryRef(); | |||
if (!poGeom2 || poGeom2->getGeometryType() != wkbPolygon) | |||
continue; | |||
/* sekvencne cti prvky z vrstvy 1 (body) */ | |||
found = false; | |||
poLayer1->ResetReading(); | |||
while (!found) { | |||
poFeat1 = poLayer1->GetNextFeature(); | |||
if (!poFeat1) | |||
break; | |||
poGeom1 = poFeat1->GetGeometryRef(); | |||
if (!poGeom1 || poGeom1->getGeometryType() != wkbPoint) | |||
continue; | |||
if (poGeom2->Within(poGeom1)) { | |||
found = true; | |||
} | |||
OGRFeature::DestroyFeature(poFeat1); | |||
} | |||
if (found) | |||
count++; | |||
OGRFeature::DestroyFeature(poFeat2); | |||
i++; | |||
} | |||
cerr << "\r"; | |||
return count; | |||
} | |||
int main(int argc, char **argv) | int main(int argc, char **argv) | ||
{ | { | ||
const char * | const char *dsn1, *dsn2, *relation; | ||
time_t start; | |||
OGRDataSource * | OGRDataSource *poDs1, *poDs2, *poSqlLite; | ||
OGRLayer * | OGRLayer *poLayer1, *poLayer2; | ||
if (argc != 3) { | |||
if (argc != | cerr << "Pouziti: " << argv[0] | ||
<< " <bodova vrstva> <polygonova vrstva>" << endl; | |||
return 1; | |||
} | } | ||
dsn1 = argv[1]; | |||
dsn2 = argv[2]; | |||
// registrovat dostupne OGR ovladace | relation = argv[3]; | ||
// registrovat dostupne OGR ovladace | |||
OGRRegisterAll(); | OGRRegisterAll(); | ||
// otevrit | cout << "Nacitam data..." << endl; | ||
// otevrit dsn1, dsn2 pro cteni | |||
if ( | poDs1 = open_dsn(dsn1); | ||
poDs2 = open_dsn(dsn2); | |||
if (!poDs1 || !poDs2) { | |||
return 1; | return 1; | ||
} | } | ||
// nacist prvni OGR vrstvu ( | // nacist prvni OGR vrstvu | ||
poLayer1 = poDs1->GetLayer(0); | |||
if ( | poLayer2 = poDs2->GetLayer(0); | ||
if (!poLayer1 || !poLayer2 ) { | |||
cerr << "Nelze nacist OGR vrstvu." << endl; | cerr << "Nelze nacist OGR vrstvu." << endl; | ||
return 1; | return 1; | ||
} | } | ||
cout << "Analyzuji data..." << endl; | |||
start = time(NULL); | |||
cout << "Pocet polygonu: " << compute_number(poLayer1, poLayer2) | |||
cout << " | << "/" << poLayer2->GetFeatureCount() << endl; | ||
cout << "Cas vypoctu: " << time(NULL) - start << " sec" << endl; | |||
return 0; | return 0; | ||
} | } | ||
</source> | </source> |
Verze z 30. 11. 2010, 13:37
Tato stránka obsahuje ukázku zdrojového textu demonstračního programu využívající pro přístup k vektorovým datům knihovnu OGR. Program je inspirován skriptem z cvičení GIS2.
Vypsání atributů
C++
#include <iostream>
#include <ctime>
#include "ogrsf_frmts.h"
using std::cerr;
using std::endl;
using std::cout;
OGRDataSource *open_dsn(const char *dsn)
{
OGRDataSource *poDs;
poDs = OGRSFDriverRegistrar::Open(dsn, FALSE);
if (!poDs)
cerr << "Otevreni " << dsn << " selhalo." << std::endl;
return poDs;
}
int compute_number(OGRLayer *poLayer1, OGRLayer *poLayer2)
{
int count, i;
int nfeat;
bool found;
OGRFeature *poFeat1, *poFeat2;
OGRGeometry *poGeom1, *poGeom2;
count = 0;
nfeat = poLayer2->GetFeatureCount();
/* sekvencne cti prvky z vsrtvy 2 (polygony) */
poLayer2->ResetReading();
i = 0;
while(true) {
cerr << "\r" << i << "/" << nfeat;
poFeat2 = poLayer2->GetNextFeature();
if (!poFeat2)
break;
poGeom2 = poFeat2->GetGeometryRef();
if (!poGeom2 || poGeom2->getGeometryType() != wkbPolygon)
continue;
/* sekvencne cti prvky z vrstvy 1 (body) */
found = false;
poLayer1->ResetReading();
while (!found) {
poFeat1 = poLayer1->GetNextFeature();
if (!poFeat1)
break;
poGeom1 = poFeat1->GetGeometryRef();
if (!poGeom1 || poGeom1->getGeometryType() != wkbPoint)
continue;
if (poGeom2->Within(poGeom1)) {
found = true;
}
OGRFeature::DestroyFeature(poFeat1);
}
if (found)
count++;
OGRFeature::DestroyFeature(poFeat2);
i++;
}
cerr << "\r";
return count;
}
int main(int argc, char **argv)
{
const char *dsn1, *dsn2, *relation;
time_t start;
OGRDataSource *poDs1, *poDs2, *poSqlLite;
OGRLayer *poLayer1, *poLayer2;
if (argc != 3) {
cerr << "Pouziti: " << argv[0]
<< " <bodova vrstva> <polygonova vrstva>" << endl;
return 1;
}
dsn1 = argv[1];
dsn2 = argv[2];
relation = argv[3];
// registrovat dostupne OGR ovladace
OGRRegisterAll();
cout << "Nacitam data..." << endl;
// otevrit dsn1, dsn2 pro cteni
poDs1 = open_dsn(dsn1);
poDs2 = open_dsn(dsn2);
if (!poDs1 || !poDs2) {
return 1;
}
// nacist prvni OGR vrstvu
poLayer1 = poDs1->GetLayer(0);
poLayer2 = poDs2->GetLayer(0);
if (!poLayer1 || !poLayer2 ) {
cerr << "Nelze nacist OGR vrstvu." << endl;
return 1;
}
cout << "Analyzuji data..." << endl;
start = time(NULL);
cout << "Pocet polygonu: " << compute_number(poLayer1, poLayer2)
<< "/" << poLayer2->GetFeatureCount() << endl;
cout << "Cas vypoctu: " << time(NULL) - start << " sec" << endl;
return 0;
}
Python
import sys
import osgeo.ogr as ogr
def main():
if len(sys.argv) != 2:
print >> sys.stderr, "Pouziti: %s shapefile" % sys.argv[0]
return 1
filename = sys.argv[1]
# otevrit ShapeFile pro cteni
ds = ogr.Open(filename)
if ds is None:
print >> sys.stderr, "Otevreni '%s' selhalo." % filename
return 1
# nacist prvni OGR vrstvu (tj. Shapefile)
lyr = ds.GetLayer(0)
if lyr is None:
print >> sys.stderr, "Nelze nacist OGR vrstvu."
# ziskat informace o vrstve
feat_defn = lyr.GetLayerDefn()
print "Detekovana OGR vrstva '%s'.\n" % feat_defn.GetName()
for i in range(feat_defn.GetFieldCount()):
# ziskat informace o atributovem sloupci
field_defn = feat_defn.GetFieldDefn(i)
print field_defn.GetNameRef()
return 0
if __name__ == "__main__":
sys.exit(main())
Prostorové predikáty
C++
#include <iostream>
#include "ogrsf_frmts.h"
using std::cerr;
using std::endl;
using std::cout;
OGRDataSource *open_dsn(const char *dsn)
{
OGRDataSource *poDs;
poDs = OGRSFDriverRegistrar::Open(dsn, FALSE);
if (!poDs)
cerr << "Otevreni " << dsn << " selhalo." << std::endl;
return poDs;
}
int compute_number(OGRLayer *poLayer1, OGRLayer *poLayer2)
{
int count, i;
int nfeat;
bool found;
OGRFeature *poFeat1, *poFeat2;
OGRGeometry *poGeom1, *poGeom2;
count = 0;
nfeat = poLayer2->GetFeatureCount();
/* sekvencne cti prvky z vsrtvy 2 (polygony) */
poLayer2->ResetReading();
i = 0;
while(true) {
cerr << "\r" << i << "/" << nfeat;
poFeat2 = poLayer2->GetNextFeature();
if (!poFeat2)
break;
poGeom2 = poFeat2->GetGeometryRef();
if (!poGeom2 || poGeom2->getGeometryType() != wkbPolygon)
continue;
/* sekvencne cti prvky z vrstvy 1 (body) */
found = false;
poLayer1->ResetReading();
while (found) {
poFeat1 = poLayer1->GetNextFeature();
if (!poFeat1)
break;
poGeom1 = poFeat1->GetGeometryRef();
if (!poGeom1 || poGeom1->getGeometryType() != wkbPoint)
continue;
if (poGeom2->Within(poGeom1)) {
found = true;
}
OGRFeature::DestroyFeature(poFeat1);
}
if (found)
count++;
OGRFeature::DestroyFeature(poFeat2);
i++;
}
return count;
}
int main(int argc, char **argv)
{
const char *dsn1, *dsn2, *relation;
OGRDataSource *poDs1, *poDs2;
OGRLayer *poLayer1, *poLayer2;
if (argc != 3) {
cerr << "Pouziti: " << argv[0]
<< " <bodova vrstva> <polygonova vrstva>" << endl;
return 1;
}
dsn1 = argv[1];
dsn2 = argv[2];
relation = argv[3];
// registrovat dostupne OGR ovladace
OGRRegisterAll();
cout << "Nacitam data..." << endl;
// otevrit dsn1, dsn2 pro cteni
poDs1 = open_dsn(dsn1);
poDs2 = open_dsn(dsn2);
if (!poDs1 || !poDs2) {
return 1;
}
// nacist prvni OGR vrstvu
poLayer1 = poDs1->GetLayer(0);
poLayer2 = poDs2->GetLayer(0);
if (!poLayer1 || !poLayer2 ) {
cerr << "Nelze nacist OGR vrstvu." << endl;
return 1;
}
cout << "Provadim vypocet..." << endl;
cout << "Pocet polygonu: " << compute_number(poLayer1, poLayer2)
<< "/" << poLayer2->GetFeatureCount() << endl;
return 0;
}