Someone asked me at work how to find the number of vertices in each polygon in series of shape files. I spent a few minutes to write a python script to do the job. At first I just looped over each feature, got the geometry reference and then tried to use GetPointCount() to give me the number of points. Imagine my surprise when it returned zero. Here is my first try:

Read more →

Have you ever wondered what would be the easiest way to read a whole text file in a string? There are many ways to skin that cat, but here is one I find myself using the following code: #include <fstream> #include <string> int main() { std::string str; // Will hold the contents of the file std::ifstream fileStream("test.txt"); // Figure out how big is the file and reserve space fileStream.seekg(0, std::ios::end); str.

Read more →

What is a palindrome anyway? Wikipedia states: A palindrome is a word, phrase, number, or other sequence of units that may be read the same way in either direction, with general allowances for adjustments to punctuation and word dividers. There are many solutions of the question, but I thought I would give it a shot. I specifically didn’t want to use recursion. Here is what I came up with:

Read more →

Today I decided to show an example usage of std::find_if with std::bind. The task at hand is to find the first integer number in a vector that is greater than 10. The source code below shows three separate calls to find_if: First one is compatible with C++ 98. It is using the std::bind2nd() which is now deprecated after the C++ 11 standard. The second option uses a C++ 11 lambda function.

Read more →

As described in «Design Patterns: Elements of Reusable Object-Oriented Software» by Erich Gamma, et al. the Singleton pattern describes a solution to create a class that will have one and only one instance within an application and also provide a global access to it. Even though there are many arguments against using it, the Singleton is still widely used. So what can I use it for? Caching, logging, and any other shared resource access.

Read more →