Skip to content

Commit

Permalink
Merge pull request #157 from akater320/performance_tweaks
Browse files Browse the repository at this point in the history
Performance tweaks
  • Loading branch information
matthewhanson committed Sep 14, 2018
2 parents 945858a + 175a0cf commit 2bce2ce
Show file tree
Hide file tree
Showing 7 changed files with 30 additions and 11 deletions.
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -13,3 +13,4 @@ gippy/gippy.py
gippy/gippy_wrap.cpp
gippy/algorithms.py
gippy/algorithms_wrap.cpp
/.vs
4 changes: 2 additions & 2 deletions GIP/GeoImage.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -50,7 +50,7 @@ namespace gip {
// Copy constructor
GeoImage::GeoImage(const GeoImage& image)
: GeoResource(image) {
for (uint i=0;i<image.nbands();i++)
for (unsigned int i=0;i<image.nbands();i++)
_RasterBands.push_back( image[i] );
_BandNames = image.bandnames();
}
Expand All @@ -61,7 +61,7 @@ namespace gip {
if (this == &image) return *this;
GeoResource::operator=(image);
_RasterBands.clear();
for (uint i=0;i<image.nbands();i++) _RasterBands.push_back( image[i] );
for (unsigned int i=0;i<image.nbands();i++) _RasterBands.push_back( image[i] );
_BandNames = image.bandnames();
return *this;
}
Expand Down
6 changes: 4 additions & 2 deletions GIP/GeoRaster.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -88,10 +88,11 @@ namespace gip {
vector<Chunk>::const_iterator iCh;
vector<Chunk> _chunks = chunks();

double noDataVal = nodata();
for (iCh=_chunks.begin(); iCh!=_chunks.end(); iCh++) {
cimg = read<double>(*iCh);
cimg_for(cimg,ptr,double) {
if (*ptr != nodata()) {
if (*ptr != noDataVal) {
total += *ptr;
count++;
if (*ptr > max) max = *ptr;
Expand All @@ -105,7 +106,7 @@ namespace gip {
for (iCh=_chunks.begin(); iCh!=_chunks.end(); iCh++) {
cimg = read<double>(*iCh);
cimg_for(cimg,ptr,double) {
if (*ptr != nodata()) {
if (*ptr != noDataVal) {
val = *ptr-mean;
total += (val*val);
total3 += (val*val*val);
Expand Down Expand Up @@ -280,6 +281,7 @@ namespace gip {
char* wkt;
site_t->exportToWkt(&wkt);
psWarpOptions->papszWarpOptions = CSLSetNameValue(psWarpOptions->papszWarpOptions,"CUTLINE", wkt);
CPLFree(wkt);
}

// set options
Expand Down
1 change: 1 addition & 0 deletions GIP/GeoResource.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -103,6 +103,7 @@ namespace gip {
bbox.y1(), 0.0, -std::abs(bbox.height() / (float)ysz)
);
set_affine(affine);
CSLDestroy(papszOptions);
}

GeoResource::GeoResource(const GeoResource& resource)
Expand Down
7 changes: 5 additions & 2 deletions GIP/GeoVectorResource.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@
#include <gip/GeoVectorResource.h>

#include <iostream>
#include <functional>

#include <cpl_error.h>

Expand Down Expand Up @@ -93,10 +94,12 @@ namespace gip {
return *_Layer->GetSpatialRef();
}*/

std::string GeoVectorResource::srs() const {
std::string GeoVectorResource::srs() const {
auto deleter = [](char* p) {CPLFree(p); };
char* wkt(NULL);
_Layer->GetSpatialRef()->exportToWkt(&wkt);
return std::string(wkt);
std::unique_ptr<char, decltype(deleter)> wktPtr(wkt, deleter); //make sure the char* is freed.
return std::string(wkt); //char* is copied
}

BoundingBox GeoVectorResource::extent() const {
Expand Down
19 changes: 15 additions & 4 deletions GIP/gip/GeoRaster.h
Original file line number Diff line number Diff line change
Expand Up @@ -494,7 +494,8 @@ namespace gip {
bool updatenodata = false;
// Apply gain and offset
if ((gain() != 1.0 || offset() != 0.0) && (!nogainoff)) {
img = gain() * img + offset();
img *= gain();
img += offset();
// Update NoData now so applied functions have proper NoData value set (?)
updatenodata = true;
}
Expand All @@ -514,9 +515,11 @@ namespace gip {

// If processing was applied update NoData values where needed
if (updatenodata) {
T noDataVal = static_cast<T>(nodata());
cimg_forXY(img,x,y) {
if (imgorig(x,y) == nodata() || std::isinf(imgorig(x,y)) || std::isnan(imgorig(x,y)))
img(x,y) = nodata();
T sample = imgorig(x, y);
if (sample == noDataVal || (std::is_floating_point<T>::value && (std::isinf(sample) || std::isnan(sample))))
img(x,y) = noDataVal;
}
}
auto elapsed = std::chrono::duration_cast<std::chrono::duration<float> >(std::chrono::system_clock::now()-start);
Expand Down Expand Up @@ -558,7 +561,15 @@ namespace gip {
//! Write a Cimg to the file
template<class T> GeoRaster& GeoRaster::write(CImg<T> img, Chunk chunk) {
if (gain() != 1.0 || offset() != 0.0) {
cimg_for(img,ptr,T) if (*ptr != nodata()) *ptr = (*ptr-offset())/gain();
double noDataVal = nodata(); //virtual call through pointer
double offsetVal = offset(); //virtual call through pointer
double invGainVal = 1.0 / gain(); //virtual call through pointer
cimg_for(img, ptr, T) {
double sample = static_cast<double>(*ptr);
if (sample != noDataVal) {
*ptr = static_cast<T>((sample - offsetVal) * invGainVal);
}
}
}
if (Options::verbose() > 3 && (chunk.p0()==iPoint(0,0)))
std::cout << basename() << ": Writing (" << gain() << "x + " << offset() << ")" << std::endl;
Expand Down
3 changes: 2 additions & 1 deletion GIP/gip/GeoResource.h
Original file line number Diff line number Diff line change
Expand Up @@ -106,6 +106,7 @@ namespace gip {
char* prj;
oSRS.exportToWkt(&prj);
_GDALDataset->SetProjection(prj);
CPLFree(prj);
//OGRSpatialReference::DestroySpatialReference(oSRS);
return *this;
}
Expand All @@ -121,7 +122,7 @@ namespace gip {
_GDALDataset->SetGeoTransform(affine.data());
return *this;
}
/* remove for now, add back in when required, with tests
/* remove for now, add back in when required, with tests
GeoResource& SetGCPs(CImg<double> gcps, std::string projection) {
int numgcps(gcps.height());
GDAL_GCP gdal_gcps[numgcps];
Expand Down

0 comments on commit 2bce2ce

Please sign in to comment.