Programování s knihovnou OGR: Porovnání verzí
mBez shrnutí editace |
|||
Řádek 2: | Řádek 2: | ||
__TOC__ | __TOC__ | ||
== C++ == | |||
== Vypsání atributů == | |||
=== C++ === | |||
<source lang="cpp"> | <source lang="cpp"> | ||
Řádek 59: | Řádek 62: | ||
</source> | </source> | ||
== Python == | === Python === | ||
<source lang="python"> | <source lang="python"> | ||
Řádek 98: | Řádek 101: | ||
sys.exit(main()) | sys.exit(main()) | ||
</source> | </source> | ||
== Prostorové predikaty == | |||
=== C++ === | |||
== Související články == | == Související články == |
Verze z 10. 11. 2010, 16:44
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 "ogrsf_frmts.h"
using std::cout;
using std::cerr;
using std::endl;
int main(int argc, char **argv)
{
const char *filename;
OGRDataSource *poDS;
OGRLayer *poLayer;
OGRFeatureDefn *poFDefn;
if (argc != 2) {
cerr << "Pouziti: " << argv[0] << " shapefile" << endl;
return 1;
}
filename = argv[1];
// registrovat dostupne OGR ovladace
OGRRegisterAll();
// otevrit ShapeFile pro cteni
poDS = OGRSFDriverRegistrar::Open(filename, FALSE);
if (poDS == NULL) {
cerr << "Otevreni '" << filename << "' selhalo." << endl;
return 1;
}
// nacist prvni OGR vrstvu (tj. Shapefile)
poLayer = poDS->GetLayer(0);
if (poLayer == NULL) {
cerr << "Nelze nacist OGR vrstvu." << endl;
return 1;
}
// ziskat informace o vrstve
poFDefn = poLayer->GetLayerDefn();
cout << "Detekovana OGR vrstva '" << poFDefn->GetName() << "'.\n\n";
for(int iField = 0; iField < poFDefn->GetFieldCount(); iField++) {
// ziskat informace o atributovem sloupci
OGRFieldDefn *poFieldDefn = poFDefn->GetFieldDefn(iField);
cout << poFieldDefn->GetNameRef() << 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())