Commit 7234f4d5edd1d6c7048e2f1a0f6357f684034e21

Authored by Jordi Inglada
1 parent a41f6fde
Exists in master

Selection workflow works

Showing 1 changed file with 29 additions and 36 deletions   Show diff stats
src/select.cxx
... ... @@ -6,6 +6,8 @@
6 6 #include "otbGeometriesProjectionFilter.h"
7 7 #include "otbGeometriesSet.h"
8 8  
  9 +using namespace cbutils::operators;
  10 +
9 11 struct TileIntersection{
10 12 std::string tile_name;
11 13 double intersection_area;
... ... @@ -13,20 +15,21 @@ struct TileIntersection{
13 15 };
14 16 using DeptToTiles = std::unordered_map<std::string, std::vector<TileIntersection>>;
15 17  
16   -void print_fields_in_layer(otb::ogr::Layer& layer)
  18 +
  19 +void print_fields_in_layer(otb::ogr::Layer& layer, std::ostream& stream = std::cout)
17 20 {
18 21 auto& layer_def = layer.GetLayerDefn();
19 22 for(auto i=0; i< layer_def.GetFieldCount(); ++i)
20   - std::cout << layer_def.GetFieldDefn(i)->GetNameRef() <<
  23 + stream << layer_def.GetFieldDefn(i)->GetNameRef() <<
21 24 '\t' << layer_def.GetFieldDefn(i)->GetType() << '\n';
22   -
23 25 }
24 26  
25   -void print_fields(otb::ogr::DataSource::Pointer &source, int layer_id=0)
  27 +void print_fields(otb::ogr::DataSource::Pointer &source, int layer_id=0,
  28 + std::ostream& stream = std::cout)
26 29 {
27 30  
28 31 otb::ogr::Layer layer = source->GetLayer(layer_id);
29   - print_fields_in_layer(layer);
  32 + print_fields_in_layer(layer, stream);
30 33 }
31 34  
32 35 std::vector<otb::ogr::Feature> get_geometries(otb::ogr::DataSource::Pointer& source)
... ... @@ -96,31 +99,19 @@ DeptToTiles get_intersecting_tiles(otb::ogr::DataSource::Pointer&amp; polygon_source
96 99 return result;
97 100 }
98 101  
99   -void print_dept_tiles(DeptToTiles tile_list)
  102 +void print_dept_tiles(DeptToTiles tile_list, std::ostream& stream = std::cout)
100 103 {
101 104 for(const auto& d : tile_list)
102 105 {
103   - std::cout << "Departement " << d.first << '\n';
  106 + stream << "Departement " << d.first << '\n';
104 107 for(const auto& t : d.second)
105 108 {
106   - std::cout << "\t(" << t.tile_name << ", " << t.intersection_area << ")\n";
  109 + stream << "\t(" << t.tile_name << ", " << t.intersection_area << ")\n";
107 110 }
108   - std::cout << '\n';
  111 + stream << '\n';
109 112 }
110 113 }
111 114  
112   -bool matches_pattern(std::string s, std::vector<std::string> patterns)
113   -{
114   - bool result{false};
115   - for(const auto& pat : patterns)
116   - {
117   - if(s.find(pat) != std::string::npos)
118   - {
119   - return true;
120   - }
121   - }
122   - return result;
123   -}
124 115  
125 116 using AreaPerTile = std::unordered_map<std::string, double>;
126 117  
... ... @@ -129,12 +120,11 @@ AreaPerTile compute_area_per_tile(otb::ogr::DataSource::Pointer&amp; polygon_source,
129 120 std::vector<std::string> culture_patterns = {"_BLE_"})
130 121 {
131 122 auto polys = get_geometries(polygon_source);
132   -
133 123 AreaPerTile result;
134 124 for(const auto& parcelle : polys)
135 125 {
136 126 auto code_cult = parcelle["LIBCULTURE"].GetValue<std::string>();
137   - if(matches_pattern(code_cult, culture_patterns))
  127 + if(cbutils::string::contains(code_cult, culture_patterns))
138 128 {
139 129 auto parcelle_geom = parcelle.GetGeometry();
140 130 for(const auto& t : tiles)
... ... @@ -168,26 +158,29 @@ int main()
168 158 auto tile_list = get_intersecting_tiles(depts, s2_tiles);
169 159 print_dept_tiles(tile_list);
170 160  
171   - std::string shaphe_file{"/home/inglada/Dev/MAA-SSP/build/SURFACES-2017-PARCELLES-GRAPHIQUES-CONSTATEES_089_20180210.shp"};
172   - auto parcelles = otb::ogr::DataSource::New(shaphe_file);
173   - auto parc_geometries = otb::GeometriesSet::New(parcelles);
174   - auto parcs = reproject_geometries(parc_geometries,
175   - s2_tiles->GetLayer(0).GetProjectionRef());
176   -
177   - auto apt = compute_area_per_tile(parcs, tile_list["89"], {"TOURNESOL", "COLZA"});
178   - for(const auto& x : apt) std::cout << x.first << "\t" << x.second << '\n';
179   -
180   -/*
181 161 std::string shape_dir{"/home/inglada/stok/DATA/OSO/MAA_SSP/"};
182   - for(const auto& f : cbutils::file::list_files(shape_dir, ".*RPG_TERLAB_DEP88-89_2017.7z"))
  162 + for(const auto& f : cbutils::file::list_files(shape_dir,
  163 + ".*RPG_TERLAB_DEP88-89_2017.7z"))
183 164 {
184 165 std::cout << "Unzipping " << f << '\n';
185 166 cbutils::system::call("7z x "+f+" -aoa");
186   - for(const auto& shape : cbutils::file::list_files("/home/inglada/Dev/MAA-SSP/build/",".*shp"))
  167 + for(auto& shape : cbutils::file::list_files("/home/inglada/Dev/MAA-SSP/build/",
  168 + ".*shp"))
187 169 {
  170 + auto parcelles = otb::ogr::DataSource::New(shape);
  171 + auto parc_geometries = otb::GeometriesSet::New(parcelles);
  172 + auto parcs = reproject_geometries(parc_geometries,
  173 + s2_tiles->GetLayer(0).GetProjectionRef());
  174 +
  175 + auto code_dept = cbutils::string::split(shape, "_")[1].substr(1,2);
  176 + std::cout << "Departement " << code_dept << '\n';
  177 + auto apt = compute_area_per_tile(parcs, tile_list[code_dept],
  178 + {"TOURNESOL", "COLZA"});
  179 + for(const auto& x : apt) std::cout << x.first << "\t" << x.second << '\n';
188 180 }
189 181 }
190   -*/
  182 +
  183 +
191 184 return EXIT_SUCCESS;
192 185 }
193 186  
... ...