GDAL / Programování / Vektorová data

Z FreeGIS portál
Přejít na: navigace, hledání

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 GDAL.

Vypsání atributů

C++

 1 #include <iostream>
 2 #include "ogrsf_frmts.h"
 3 
 4 using std::cout;
 5 using std::cerr;
 6 using std::endl;
 7 
 8 int main(int argc, char **argv)
 9 {
10     const char *filename;
11     
12     OGRDataSource  *poDS;
13     OGRLayer       *poLayer;
14     OGRFeatureDefn *poFDefn;
15 
16     if (argc != 2) {
17 	cerr << "Pouziti: " << argv[0] << " shapefile" << endl;
18 	return 1;
19     }
20     
21     filename = argv[1];
22     
23     // registrovat dostupne OGR ovladace
24     OGRRegisterAll();
25 
26     // otevrit ShapeFile pro cteni
27     poDS = OGRSFDriverRegistrar::Open(filename, FALSE);
28     if (poDS == NULL) {
29         cerr << "Otevreni '" << filename << "' selhalo." << endl;
30         return 1;
31     }
32     
33     // nacist prvni OGR vrstvu (tj. Shapefile)
34     poLayer = poDS->GetLayer(0);
35     if (poLayer == NULL) {
36         cerr << "Nelze nacist OGR vrstvu." << endl;
37         return 1;
38     }
39 
40     // ziskat informace o vrstve
41     poFDefn = poLayer->GetLayerDefn();
42 
43     cout << "Detekovana OGR vrstva '" << poFDefn->GetName() << "'.\n\n";
44 
45     for(int iField = 0; iField < poFDefn->GetFieldCount(); iField++) {
46 	// ziskat informace o atributovem sloupci
47 	OGRFieldDefn *poFieldDefn = poFDefn->GetFieldDefn(iField);
48 	cout << poFieldDefn->GetNameRef() << endl;
49     }
50 
51     return 0;
52 }

Python

 1 import sys
 2 from osgeo import ogr
 3 
 4 def error(msg):
 5     sys.exit(msg)
 6 
 7 def main(filename):
 8     # otevrit ShapeFile pro cteni
 9     ds = ogr.Open(filename)
10     if ds is None:
11         error("Otevreni '{}' selhalo.".format(filename))
12     
13     # nacist prvni OGR vrstvu (tj. Shapefile)
14     lyr = ds.GetLayer(0)
15     if lyr is None:
16         error("Nelze nacist OGR vrstvu.")
17     
18     # ziskat informace o vrstve
19     feat_defn = lyr.GetLayerDefn()
20     
21     print("OGR vrstva '{}' obsaguje {} geoprvku".format(feat_defn.GetName(), lyr.GetFeatureCount()))
22     print("Seznam atributu:")
23     # ziskat informace o atributovem sloupci    
24     for i in range(feat_defn.GetFieldCount()):
25         field_defn = feat_defn.GetFieldDefn(i)
26         print("\t {}".format(field_defn.GetNameRef()))
27     
28     return 0
29 
30 if __name__ == "__main__":
31     if len(sys.argv) != 2:
32         sys.stderr.write("Pouziti: %s shapefile\n" % sys.argv[0])
33         sys.exit(0)
34     
35     sys.exit(main(sys.argv[1]))

Prostorové predikáty

Pro PostGIS je vhodné nastavit

export PGCLIENTENCODING=WIN1250
export PG_USE_COPY=YES

C++

  1 #include <iostream>
  2 #include <ctime>
  3 #include "ogrsf_frmts.h"
  4  
  5 using std::cerr;
  6 using std::endl;
  7 using std::cout;
  8  
  9 OGRDataSource *open_dsn(const char *dsn)
 10 {
 11     OGRDataSource *poDs;
 12  
 13     poDs = OGRSFDriverRegistrar::Open(dsn, FALSE);
 14     if (!poDs)
 15         cerr << "Otevreni " << dsn << " selhalo." << std::endl;
 16  
 17     return poDs;
 18 }
 19  
 20 int compute_number(OGRLayer *poLayer1, OGRLayer *poLayer2)
 21 {
 22     int count, i;
 23     int nfeat;
 24     bool found;
 25  
 26     OGRFeature *poFeat1, *poFeat2;
 27     OGRGeometry *poGeom1, *poGeom2;
 28  
 29     count = 0;
 30  
 31     nfeat = poLayer1->GetFeatureCount();
 32  
 33     /* sekvencne cti prvky z vsrtvy 1 (body) */
 34     poLayer1->ResetReading();
 35     i = 0;
 36     while(true) {
 37         cerr << "\r" << i << "/" << nfeat;
 38         poFeat1 = poLayer1->GetNextFeature();
 39         if (!poFeat1)
 40             break;
 41         poGeom1 = poFeat1->GetGeometryRef();
 42         if (!poGeom1 || poGeom1->getGeometryType() != wkbPoint)
 43             continue;
 44  
 45         /* sekvencne cti prvky z vrstvy 2 (polygony) */
 46         found = false;
 47         poLayer2->ResetReading();
 48 	poLayer2->SetSpatialFilter(poGeom1);
 49         while (!found) {
 50             poFeat2 = poLayer2->GetNextFeature();
 51             if (!poFeat2)
 52                 break;
 53             poGeom2 = poFeat2->GetGeometryRef();
 54             if (!poGeom2 || poGeom2->getGeometryType() != wkbPolygon)
 55                 continue;
 56  
 57             if (poGeom1->Within(poGeom2)) {
 58                 found = true;
 59             }
 60  
 61             OGRFeature::DestroyFeature(poFeat2);
 62         }
 63         if (found)
 64             count++;
 65  
 66         OGRFeature::DestroyFeature(poFeat1);
 67         i++;
 68     }
 69     cerr << "\r";
 70  
 71     return count;
 72 }
 73  
 74 int main(int argc, char **argv)
 75 {
 76     const char *dsn1, *dsn2, *relation;
 77     // const char *pszDriverName = "SQLite";
 78     const char *pszDriverName = "PostgreSQL";
 79     char      **papszLCO;
 80     time_t      start;
 81     OGRSFDriver    *poDriver;
 82     OGRDataSource  *poDs1, *poDs2, *poDsSL;
 83     OGRLayer       *poLayer1, *poLayer2;
 84  
 85     if (argc != 3) {
 86         cerr << "Pouziti: " << argv[0]
 87              << " <bodova vrstva> <polygonova vrstva>" << endl;
 88         return 1;
 89     }
 90  
 91     dsn1     = argv[1];
 92     dsn2     = argv[2];
 93     relation = argv[3];
 94  
 95     // registrovat dostupne OGR ovladace                                                 
 96     OGRRegisterAll();
 97  
 98     cout << "Nacitam data..." << endl;
 99     // otevrit dsn1, dsn2 pro cteni                                                                           
100     poDs1 = open_dsn(dsn1);
101     poDs2 = open_dsn(dsn2);
102     if (!poDs1 || !poDs2) {
103         return 1;
104     }
105  
106     // nacist prvni OGR vrstvu
107     poLayer1 = poDs1->GetLayer(0);
108     poLayer2 = poDs2->GetLayer(0);
109     if (!poLayer1 || !poLayer2 ) {
110         cerr << "Nelze nacist OGR vrstvu." << endl;
111         return 1;
112     }
113  
114     // konverze dat do databaze SpatiaLite
115     cout << "Ukladam data do databaze PostGIS..." << endl;
116     poDriver = OGRSFDriverRegistrar::GetRegistrar()->GetDriverByName(pszDriverName);
117     if (!poDriver) {
118 	cerr << "Driver " << pszDriverName << " neni dostupny" << endl;
119 	return 1;
120     }
121     // poDsSL = poDriver->Open("tmp.sqlite", TRUE);
122     poDsSL = poDriver->Open("PG:dbname=pgis_test", TRUE);
123     if (!poDsSL) {
124 	char **papszDSCO = NULL;
125 	// papszDSCO = CSLSetNameValue(papszDSCO, "SPATIALITE", "YES");
126 	// poDsSL = poDriver->CreateDataSource("tmp.sqlite", papszDSCO);
127     }
128     papszLCO = NULL;
129     papszLCO = CSLSetNameValue(papszLCO, "OVERWRITE", "YES");
130     poLayer1 = poDsSL->CopyLayer(poLayer1, "points", papszLCO);
131     poLayer2 = poDsSL->CopyLayer(poLayer2, "polygons", papszLCO);
132  
133     cout << "Analyzuji data..." << endl;
134     start = time(NULL);
135     cout << "Pocet bodu:  " << compute_number(poLayer1, poLayer2)
136          << "/" << poLayer1->GetFeatureCount() << endl;
137     cout << "Cas vypoctu: " << time(NULL) - start << " sec" << endl;
138  
139     OGRDataSource::DestroyDataSource(poDsSL);
140     OGRDataSource::DestroyDataSource(poDs1);
141     OGRDataSource::DestroyDataSource(poDs2);
142  
143     return 0;
144 }

Související články

Externí odkazy