| 441 | } |
| 442 | |
| 443 | void MapTree::OnSpatialJoinCount(wxCommandEvent& event) |
| 444 | { |
| 445 | wxString map_name = map_titles[new_order[select_id]]; |
| 446 | BackgroundMapLayer* ml = GetMapLayer(map_name); |
| 447 | if (ml) { |
| 448 | // create rtree using points (normally, more points than polygons) |
| 449 | rtree_pt_2d_t rtree; |
| 450 | Shapefile::ShapeType shp_type = ml->GetShapeType(); |
| 451 | if (shp_type == Shapefile::POINT_TYP) { |
| 452 | int n = ml->shapes.size(); |
| 453 | double x, y; |
| 454 | for (int i=0; i<n; i++) { |
| 455 | x = ml->shapes[i]->center_o.x; |
| 456 | y = ml->shapes[i]->center_o.y; |
| 457 | rtree.insert(std::make_pair(pt_2d(x,y), i)); |
| 458 | } |
| 459 | } |
| 460 | |
| 461 | // for each polygon in map, query points |
| 462 | Shapefile::Main& main_data = canvas->GetGeometryData(); |
| 463 | OGRLayerProxy* ogr_layer = canvas->GetOGRLayerProxy(); |
| 464 | Shapefile::PolygonContents* pc; |
| 465 | int n_polygons = main_data.records.size(); |
| 466 | std::vector<wxInt64> spatial_counts(n_polygons, 0); |
| 467 | for (int i=0; i<n_polygons; i++) { |
| 468 | pc = (Shapefile::PolygonContents*)main_data.records[i].contents_p; |
| 469 | // create a box, tl, br |
| 470 | box_2d b(pt_2d(pc->box[0], pc->box[1]), |
| 471 | pt_2d(pc->box[2], pc->box[3])); |
| 472 | // query points in this box |
| 473 | std::vector<pt_2d_val> q; |
| 474 | rtree.query(bgi::within(b), std::back_inserter(q)); |
| 475 | OGRGeometry* ogr_poly = ogr_layer->GetGeometry(i); |
| 476 | for (int j=0; j<q.size(); j++) { |
| 477 | const pt_2d_val& v = q[j]; |
| 478 | double x = v.first.get<0>(); |
| 479 | double y = v.first.get<1>(); |
| 480 | OGRPoint ogr_pt(x, y); |
| 481 | if (ogr_pt.Within(ogr_poly)) { |
| 482 | spatial_counts[i] += 1; |
| 483 | } |
| 484 | } |
| 485 | } |
| 486 | |
| 487 | // save results |
| 488 | int new_col = 1; |
| 489 | std::vector<SaveToTableEntry> new_data(new_col); |
| 490 | std::vector<bool> undefs(n_polygons, false); |
| 491 | new_data[0].l_val = &spatial_counts; |
| 492 | new_data[0].label = "Spatial Counts"; |
| 493 | new_data[0].field_default = "SC"; |
| 494 | new_data[0].type = GdaConst::long64_type; |
| 495 | new_data[0].undefined = &undefs; |
| 496 | SaveToTableDlg dlg(canvas->GetProject(), this, new_data, |
| 497 | _("Save Results: Spatial Counts"), |
| 498 | wxDefaultPosition, wxSize(400,400)); |
| 499 | dlg.ShowModal(); |
| 500 | } |
nothing calls this directly
no test coverage detected