Wednesday, October 19, 2011

Ensure Correct Ring Orientation for SqlGeography

SqlGeography is very picky about the order of points when reading Well Known Text (WKT): if your WKT points aren’t just so, the de-serialization will throw something like this:

A .NET Framework error occurred during execution of user-defined routine or aggregate "geography":
Microsoft.SqlServer.Types.GLArgumentException: 24205: The specified input does not represent a valid geography instance because it exceeds a single hemisphere. Each geography instance must fit inside a single hemisphere. A common reason for this error is that a polygon has the wrong ring orientation.

Luckily, there are smarter people in the world than me.  Here’s a post on how to use T-SQL to ensure correct ring orientation.  Here’s how I do it using Microsoft.SqlServer.Types in C#:

// a helper SqlGeometry, which isn't picky about ring orientation
var geometryHelper =
SqlGeometry.STGeomFromText(wkt.ToSqlChars(), srid).MakeValid();

// STUnion will automagically correct any bad ring orientation
var validGeom =
geometryHelper.STUnion(geometryHelper.STStartPoint());

// use the validGeom with correct ring orientation to
// create a SqlGeography.
var validGeography =
SqlGeography.STGeomFromText(validGeom.STAsText(), srid);

Tuesday, October 18, 2011

Switch/Flag Parameters in PowerShell

I wanted to create a function with a ‘-force’ option.  First, I tried to add a [boolean] parameter, but that doesn’t work because it needs a value with the argument; I want it to be invoked with just the presence of the switch/flag.  Here’s how I finally did it:

function Update-Something {
param(
[switch] $force)

if ($force) {
#do something here
}
}


# use like:
$> Update-Something -force

Friday, October 14, 2011

DotSpatial Accepted Pull Request!

Tooting my own horn: the DotSpatial project accepted a pull request of mine for nuspec and build helper files -- woot!

Thursday, October 13, 2011

Rounding In PowerShell

Another simple one (from John D. Cook):

$> [int] 1.1
1

$> [int] 1.5
2

$> [int] 1.8
2

PowerShell: Parse Date from String

Incredibly simple (checkout this PowerShell Cookbook):
$> (get-date 2000/01/01)

Saturday, January 01, 2000 12:00:00 AM

Thursday, October 6, 2011

NuGet Without Committing Packages

I like NuGet.  I’m using it to distribute the #foo REST project.  I had this idea that it would be nice NOT to check in package binaries to source control.  As usual, I’m not the first one with that idea.  God bless all the people that are smarter than me:
PM> Enable-PackageRestore
Attempting to resolve dependency 'NuGet.CommandLine (≥ 1.4)'.
Successfully installed 'NuGet.CommandLine 1.4.20615.182'.
Successfully installed 'NuGet.Build 0.16'.

Copying nuget.exe and msbuild scripts to D:\Code\StarterApps\Mvc3Application\.nuget
Successfully uninstalled 'NuGet.Build 0.16'.
Successfully uninstalled 'NuGet.CommandLine 1.4.20615.182'.

Don't forget to commit the .nuget folder
Updated 'Mvc3Application' to use 'NuGet.targets'
Enabled package restore for Mvc3Application

Wednesday, October 5, 2011

CodePlex and Windows Live Writer

I’m working on a simple POCO message based service layer to sit on top of ASP.NET MVC.  The project (not yet published) is/will be hosted at CodePlex.  As part of this blog, I discovered Windows Live Writer and have been happy with it.  I am even more happy that it works with CodePlex wiki pages.  And I’m giddy that you can use the Insert Code plugin to add code snippets without leaving Live Writer (don’t be freaked out that the last release was in 2006…).

As an aside, I use the more robust SyntaxHighlighter with the PreCode Live Writer plugin for this blog.  I use version 2.x so that the toolbar buttons still show.  There are publicly hosted source files for SyntaxHighlighter, if you’re interested.  I basically then followed the install instructions, substituting the 2.x hosted file URLs where needed.

Tuesday, October 4, 2011

PowerShell Format-List (fl)

Staying with the late to the party theme on my PowerShell adventure, today I discovered Format-List (fl alias).  You can pipe results into it and get more detailed output.

Without Format-List:
PS> [Array]

IsPublic IsSerial Name BaseType
-------- -------- ---- --------
True True Array System.Object
With Format-List:
PS> [Array] | fl

Module : CommonLanguageRuntimeLibrary
Assembly : mscorlib, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b7
7a5c561934e089
TypeHandle : System.RuntimeTypeHandle
DeclaringMethod :
BaseType : System.Object
UnderlyingSystemType : System.Array
FullName : System.Array
AssemblyQualifiedName : System.Array, mscorlib, Version=4.0.0.0, Culture=neutral, Pub
licKeyToken=b77a5c561934e089
Namespace : System
GUID : 200fb91c-815d-39e0-9e07-0e1bdb2ed47b
IsEnum : False
GenericParameterAttributes :
IsSecurityCritical : False
IsSecuritySafeCritical : False
IsSecurityTransparent : True

Monday, October 3, 2011

PowerShell RegEx

Trying to learn PowerShell by using it to create a NuGet build scripts. In order to parse some code files, I needed to filter a list of strings using a regular expression and capture a match group from the first string matched:
$line = 
    $assembly_info | 
    where {$_ -match 'AssemblyDescription\("(?<1>[^"]*)"\)'} | 
    select -first 1

$matches[1]
Looks almost like a functional language.  Better late to the party than never…