Friday, March 29, 2013

c#: Get all the files in a folder

Get all the files in a folder
private static List files = new List();
static void FullDirList(DirectoryInfo dir, string searchPattern)
{
 // Console.WriteLine("Directory {0}", dir.FullName);
 // list the files
 try
 {
  foreach (FileInfo f in dir.GetFiles(searchPattern))
  {
   //Console.WriteLine("File {0}", f.FullName);
   files.Add(f);
  }
 }
 catch
 {
  Console.WriteLine("Directory {0}  \n could not be accessed!!!!", dir.FullName);
  return;  // We alredy got an error trying to access dir so dont try to access it again
 }

 // process each directory
 // If I have been able to see the files in the directory I should also be able 
 // to look at its directories so I dont think I should place this in a try catch block
 foreach (DirectoryInfo d in dir.GetDirectories())
 {                
  FullDirList(d, searchPattern);
 }

}

USGS DEM trivials


  • Get the correct folder name

     
string foldername = (lat > 0 ? "n" + Math.Ceiling(lat).ToString("00") : "s" + Math.Ceiling(lat * -1).ToString("00")) + (lon > 0 ? "e" + Math.Ceiling(lon).ToString("000") : "w" + Math.Ceiling(lon * -1).ToString("000"));

  • Does each DEM data has the same cell size? No. The scanning result shows the following possibilities
              9.2592592593E-05
              9.2592592599959E-05
              9.2592592599958E-05

A good example demonstrate the accessor usage in C# (from MSDN)

In this example, two classes, Cube and Square, implement an abstract class, Shape, and override its abstract Area property. Note the use of the override modifier on the properties. The program accepts the side as an input and calculates the areas for the square and cube. It also accepts the area as an input and calculates the corresponding side for the square and cube.
// overridding_properties.cs
// Overriding properties
using System;
abstract class Shape 
{
   public abstract double Area 
   {
      get;
      set;
   }
}

class Square: Shape 
{
      public double side;

      // Constructor:
      public Square(double s) 
      {
         side = s;
      }

   // The Area property
   public override double Area 
   {
      get 
      {
         return side*side ; 
      }
      set 
      {
         // Given the area, compute the side
         side = Math.Sqrt(value); 
      }
   }
}

class Cube: Shape 
{
      public double side;    

      // Constructor:
      public Cube(double s) 
      {
         side = s;
      }

      // The Area property
      public override double Area 
      {
         get 
         {
            return 6*side*side;
         }
         set 
         {
            // Given the area, compute the side
            side = Math.Sqrt(value/6);
         }
      }
}

public class MainClass 
{
   public static void Main()  
   {
      // Input the side:
      Console.Write("Enter the side: ");
      string sideString = Console.ReadLine();   
      double side = double.Parse(sideString);

      // Compute areas:
      Square s = new Square(side);
      Cube c = new Cube(side);

      // Display results:
      Console.WriteLine("Area of a square = {0:F2}",s.Area);
      Console.WriteLine("Area of a cube = {0:F2}", c.Area); 

      // Input the area:
      Console.Write("Enter the area: ");
      string areaString = Console.ReadLine();   
      double area = double.Parse(areaString);

      // Compute sides:
      s.Area = area;
      c.Area = area;

      // Display results:
      Console.WriteLine("Side of a square = {0:F2}", s.side);
      Console.WriteLine("Side of a cube = {0:F2}", c.side);
   }
}

Tuesday, March 5, 2013

Sqlite trivials


  • It is safest to operate only on SQLite files on a physical disk of the local system
  • SQLite is a Transactional DBMS; while many INSERT statements are executed in close sequence, BEGIN TRANSACTION and COMMIT TRANSACTION statements have to be invoked appropriately in order to get optimal performance.
  • The default OGR behavior is to COMMIT a transaction every 200 inserted rows. This value is surely too low for SQLite; and closing too much frequently the current transaction causes severe performance degradation. The -gt argument allows to explicitly set the number of rows for each transaction. Explicitly defining -gt 1024 usually ensures a noticeable performance boost; defining an even bigger -gt 65536 ensures optimal performance while populating some table containing many hundredth thousand or million rows.
  •  Explicitly setting a much more generously dimensioned internal Page Cache may often help to get a noticeably better performance. Starting since GDAL 1.9.0 you can explicitly set the internal Page Cache size using the configuration option OGR_SQLITE_CACHE value [value being measured in MB]; if your HW has enough available RAM, defining a Cache size as big as 512MB (or even 1024MB) may sometimes help a lot in order to get better performance.
  • Setting the OGR_SQLITE_SYNCHRONOUS configuration option to OFF might also increase performance when creating SQLite databases (altough at the expense of integrity in case of interruption/crash ).

Friday, March 1, 2013

Ways to identify beach/parking parcels

Criteria 1. Land cover type. I downloaded USGS national land cover data, and overlapped the parcel boundaries on them. A typical beach/parking parcel has the following look:

It can be found that large area inside the parcel is class 31-barren land.

 

Criteria 2. Area and shape of the parcel. Area is simple to calculate. Shape identification has been seen and desired in a few problems. The most simple way to characterize the shape is the length and width. A good definition of any parcel’s length and width should be like this:

First, start with the envelope of a polygon, it would look like this:

Obviously, the envelope doesn’t consider the orientation of the parcel. Therefore, we can rotate the polygon, and at every angle a new envelope is generated. If we calculate the ratio of the area of the polygon and the area of the envelope, it will have a maximum value when we rotate to a certain degree, looking like this:

Now the length and width of the envelope are defined the length and width of the polygon correspondingly.

A normal residential parcel’s length-width-ratio should not be too big. But not necessary, so it is a little vague.

Just a few thoughts, let me know if you have better idea.