Thursday, September 18, 2008

Google Maps API and Microsoft Virtual Earth SDK

After thinking about what would be worth to learn, I decided to choose the Google Maps API and Microsoft Virtual Earth SDK. I just got started, you can find my "Hello, World" example here. I will keep on posting examples (more interesting than this one, I promess!)

Tuesday, September 9, 2008

Algorithm to buffer a polygon

I came across the need to buffer a polygon. Of course, ESRI provides tool in ArcMap to achieve this, but I needed an algorithm to use in an application rather than a tool. I could not find the algorithm, but I found the code to achieve this. Still, it should be fairly easy to figure out the algorithm by reading the code. You can find by clicking here. It's in VB, so I had to convert it to C++.

In case the link is broken, I am including the source code:

Private Type XY
p_X As Double
p_Y As Double
End Type
Dim MyPoly() As XY
Dim MyPoly2() As XY
Dim MyMidLines() As XY
Dim MyLine() As XY

Private Sub OUTLINE_IT()
'MyPoly is a matrix from 0 to N, containing the X and Y values of the
'points of the original polygon
'MyPoly2 is the Output Poly

Dim I As Integer
Dim PI As Integer
Dim NI As Integer '
Dim p_1 As XY
Dim p_2 As XY
Dim t1 As Double
Dim t2 As Double
Dim MidP As XY
Dim MySign As Integer

ReDim MyMidLines(UBound(MyPoly))
ReDim MyPoly2(UBound(MyPoly))
ReDim MyLine(UBound(MyPoly))
Dim MyD As Long
MyD = Val(txtSize.Text)
MySign = Sgn(MyD)
If MySign <> 0 Then
For I = 0 To UBound(MyPoly)
PI = ((I - 1) + UBound(MyPoly) + 1) Mod (UBound(MyPoly) + 1)
NI = ((I + 1) + UBound(MyPoly) + 1) Mod (UBound(MyPoly) + 1)
Picture1.Line (MyPoly(I).p_X, MyPoly(I).p_Y)-(MyPoly(NI).p_X, MyPoly(NI).p_Y), vbBlack
MyLine(I).p_X = MyPoly(NI).p_X - MyPoly(I).p_X
MyLine(I).p_Y = MyPoly(NI).p_Y - MyPoly(I).p_Y
t1 = Sqr(((MyPoly(PI).p_X - MyPoly(I).p_X) ^ 2 + (MyPoly(PI).p_Y - MyPoly(I).p_Y) ^ 2) / _
((MyPoly(NI).p_X - MyPoly(I).p_X) ^ 2 + (MyPoly(NI).p_Y - MyPoly(I).p_Y) ^ 2))
MidP.p_X = (MyPoly(PI).p_X + (MyPoly(NI).p_X - MyPoly(I).p_X) * t1 + MyPoly(I).p_X) / 2
MidP.p_Y = (MyPoly(PI).p_Y + (MyPoly(NI).p_Y - MyPoly(I).p_Y) * t1 + MyPoly(I).p_Y) / 2

MyMidLines(I).p_X = (MidP.p_X - MyPoly(I).p_X)
MyMidLines(I).p_Y = (MidP.p_Y - MyPoly(I).p_Y)

Next I

t1 = MySign * Sqr((MyD ^ 2) / (MyMidLines(0).p_X ^ 2 + MyMidLines(0).p_Y ^ 2))

MyPoly2(0).p_X = t1 * MyMidLines(0).p_X + MyPoly(0).p_X
MyPoly2(0).p_Y = t1 * MyMidLines(0).p_Y + MyPoly(0).p_Y

For I = 0 To UBound(MyPoly)
PI = ((I - 1) + UBound(MyPoly) + 1) Mod (UBound(MyPoly) + 1)
NI = ((I + 1) + UBound(MyPoly) + 1) Mod (UBound(MyPoly) + 1)
t2 = (MyLine(I).p_Y * MyPoly2(I).p_X - MyLine(I).p_X * MyPoly2(I).p_Y + MyLine(I).p_X * MyPoly(NI).p_Y - _
MyLine(I).p_Y * MyPoly(NI).p_X) / (MyLine(I).p_Y * MyMidLines(NI).p_X - MyLine(I).p_X * MyMidLines(NI).p_Y)
MyPoly2(NI).p_X = MyMidLines(NI).p_X * t2 + MyPoly(NI).p_X
MyPoly2(NI).p_Y = MyMidLines(NI).p_Y * t2 + MyPoly(NI).p_Y
Picture1.Line (MyPoly2(I).p_X, MyPoly2(I).p_Y)-(MyPoly2(NI).p_X, MyPoly2(NI).p_Y), vbBlack
Next I
End If

End Sub

Wednesday, August 27, 2008

Error showing "Run-Time Check Failure #0"

After compiling the source code of OSG 2.6.0 and MANUALLY copying the binaries to another directory, I got the error message:

Run-Time Check Failure #0 - The value of ESP was not properly saved across a function call. This is usually a result of calling a function declared with one calling convention with a function pointer declared with a different calling convention.

This error pops up when I run an OSG application in VS2005 in Debug mode. This error looks a little scary, but after looking for information about this error, I found which pointed me in the right direction. It was because I copied the dll's and include directory (*dll, *.h), but not the library files (*.lib) to a new directory. I guess that is why there is an INSTALL project in the OSG solution that copies the files automatically :)

Friday, June 27, 2008

Bug, Error or Defect?

A few days ago I came across this article:

It got me thinking... I usually use the word "bug" to referr to any unexpected behavior in the software. However, after reading this article, I started to say "defect" instead of "bug".

It also made me remember a Software Quality Assurance course I took during my Master's degree. In the course book the differences between defect, error, fault and some other words I don't really remember was explained. Back then -and still- I thought that there was no sense in using different words for different situations. In my opinion, problems during compilation, testing, deployment... all can be called defects. Using a word for each situation makes it confusing.

Finally, it is interesting how the name of the software changes our habits... for example, I used to say "bug" because I am used to using BugNET as a defect tracking system. Or when I say "googled" a word or "photoshop" an image.

Wednesday, June 18, 2008

VS2005 and wchar_t

While upgrading a VS2003 solution to VS2005, I received the following error:

Error 29 error LNK2028: unresolved token (0A001D22) "public: void __thiscall osgText::TextBase::setText(unsigned short const *)" (?setText@TextBase@osgText@@$$FQAEXPBG@Z) referenced in function "private: class osg::Node * __thiscall XXX::YY::ZZZ::FFF::WWWW(class XXX::YY::ZZZ::FFF::CCC &,class osg::Vec4f const &)" (?KKKKK@TTTTT@CCCCC@YY@XXX@@$$FAAEPAVNode@osg@@AAVGGGGG@234@ABVVec4f@6@@Z) RRRRRR.obj

My first thought was to make sure I had the correct version of the OSG libraries. They were OK, so then I googled some keywords of the error message and came across something interesting. VS has an option in Tools->Options that is "Treat wchar_t as built in type". My default value was false, so I set it to "Yes" and rebuilt the libraries. It worked!

Monday, April 28, 2008

Stack overflow at line: 0

While debugging an ASP.NET application I got the error: "Stack overflow at line: 0". This happens just after adding a new line of code to a javascript file. Then I thought this article could be related. However, the code indicated in the article was already implemented in the website.

The error appears in both IE7 and FireFox 2.0 in debug mode. However, in the deployed website the error was not appearing. Does it mean the error exist or not? Is it possible this issue is related to VS2005 and not to the javascript code I added?

Friday, February 8, 2008

Sergey Brin @ UC Berkeley

Check it out, interesting talk provided by

Using a ScaleDependentRenderer in a FeatureGraphicsLayer?

I was looking for a relationship between the classes FeatureGraphicsLayer and ScaleDependentRenderer. I could not find it in the EDN website. This is why I was looking for this: say you have a FeatureGraphicsLayer in which you want to draw features using a ValueMapRenderer. This is all OK since a ValueMapRenderer is "type of" IRenderer which is used by the FeatureGraphicsLayer.

However, what if you want to draw those features with a height/width that changes according to the scale? There is no IScaleDependentRenderer in the WebADF API. Philip Thompson posted a suggestion here:

Basically, we can extend his code snippet to set the Renderer on-the-fly everytime the ScaleChanged() method of the class Map is triggered. In this case, the Renderer will be set according to the scale. Thanks Philip!

protected void Map1_ScaleChanged(object sender, ESRI.ArcGIS.ADF.Web.UI.WebControls.ScaleEventArgs args)
if (args.NewScale > 4000)
foreach (MapResourceItem item1 in this.MapResourceManager1.ResourceItems)
if (item1.Definition.DataSourceType == "GraphicsLayer")
item1.DisplaySettings.Visible = false;
if (args.NewScale < 4000)
foreach (MapResourceItem item1 in this.MapResourceManager1.ResourceItems)
if (item1.Definition.DataSourceType == "GraphicsLayer")
item1.DisplaySettings.Visible = true;

Tuesday, February 5, 2008

IServiceProvider error: ambiguous symbol message

I have been working on porting code from VS2003 to VS2005. The last error message I received before being able to compile it was:

error C2872: 'IServiceProvider' : ambiguous symbol

Lucky me, this error is well documented. The solution was moving the #include to the top of the file. It works for me and I did not look further on the cause of this error. However, it seems this symbol may be declared in more than one file.

This is one area where Microsoft could follow ESRI's approach. ESRI does not declare repeated names, it adds a number to the end of the class that can be incremented, i.e. ISpatialReference, ISpatialReference2, ISpatialReference3. Note that ESRI is actually extending a class with each increment and Microsoft may not be doing that.

Saturday, February 2, 2008

OSG scene viewed inside a .NET control

This is an interesting challenge. This is what I did: I created two threads, one for the main form and one for the OSG loop. This second thread shall run a method that receives a handle as a parameter. This handle can be either a handle to the form or a handle to a control (i.e. Panel) and is assigned as follows:

new osgViewer::GraphicsWindowWin32::WindowData(hwnd);

The method that is run by the second thread contains the usual instructions to render a scene in OSG. Of course, this is done in C++ and called from C#.

I run into a problem while doing this: it is not possible to access a control "from other than the thread it was created on" (Cross-thread operation error). After doing some investigation I found this can be fixed using:

CheckForIllegalCrossThreadCalls = false;

Still, this is a quick fix and I do not think is the solution for a commercial app. Visual Studio 2005 has more checking than Visual Studio 2003, therefore if you are running 2003 you might not receive this error.

Tuesday, January 22, 2008

Microsoft Popfly

Check out Microsoft Popfly. Is website development becoming more user-friendly? It even has a plugin for Mac users, although it has some issues when opened in Firefox.

It basically has two capabilities: creating mashups and creating websites. For the mashups, it seems it only links to a set predefined set of websites, which definitely limits what the tool can do. Still, it is an interesting concept! I will stick to DotNetNuke for the moment.

External tools to support VS2005

There are two tools I have been using lately along with Visual Studio 2005. One of them is .NET Reflector which you can download here.

The second tool is FileMon. Find it here. It is a great tool that can tell you the folder paths where Visual Studio is looking for a file and whether it finds it or not. Both of these applications are free.

Finally, another tool is TestDriven.NET. It can help you debug your unit tests if your are using TDD. It works great with NUnit, but there is a small fee to pay.

Monday, January 14, 2008

VS 2008 release

I can not wait to use VS 2008! I downloaded the evaluation version from I
read about the new features, I am really interested in the capability of using VS 2008 with
any version of the .NET framework. This will allow companies to support existing projects
before porting them to .NET 3.5

I was planning to get a book to learn LINQ and .NET 3.5, but Microsoft Learning offers 3
free books on the website This will get me started for the next
couple of weeks.

Sunday, January 13, 2008

Featuring a new website

Lately I heard about DotNetNuke. I decided to use it for my website and these are the results.

Right now I am just using the installers, but I am planning to download the source code and
customize it as necessary. Too bad the code is in VB.NET!Still, it is going to be a good
opportunity to keep my VB.NET skills sharp.

Sunday, January 6, 2008

Image Server 9.2 - Imagery resolution

I then changed the parameter to PNG24 and then compared the size of the file that is
created and then sent to the client. To my surprise, the results really affect the
performance of the application. For example, with PNG8 the file size is ~150KB, but
with PNG24 it can be up to ~750KB.

When you include the fact that it might be loading more than one image by more than one
user, performance can be even more critical! There are other factors, like the
available bandwidth.

One last test was about using JPEG as the image format. JPEG is really similar to JPG8
when talking about the image size. Therefore, you should be careful when choosing the
Image Format.