So I have a huge directory structure full of shape files and I wanted to get them all into my SQL Server 2014 instance (I'm sure many earlier versions of SQL Server will work just as well with this procedure). I got tired of all the software I found to do this for one reason or another. Anyhow, I created a .bat file with the following contents:
for /R %%f in (*.shp) do C:\OSGeo4W64\bin\ogr2ogr -f "MSSQLSPATIAL" "MSSQL:server=localhost;database=MyGISDB;trusted_connection=yes" -skipfailures "%%f"
Note: -skipfailures is optional here. I also had to get ogr2ogr by installing OSGeo4W, but it may not be necessary to install all of this just to get this one executable.
Then I put this batch file in the root of the folder that contains all of my shape files. Started this .bat file in a command prompt (see another of my posts for an easy way to switch directories with the cmd window). Remember to change your working directory to the folder that contains your shape files and batch file. I typed in cmd /C MyBatchFile.bat and hit enter. The /C command causes the cmd to continue executing even if the batch file hits an error while running. You can use the /k command if you would like the cmd window to remain open when all is complete. However, for some reason I was not able to use both the /C and /k at the same time. I'm sure this is not a problem for a command line guru, which I clearly am not. Feel free to comment if you know how to use both at the same time. Hopefully this helps someone save some time.
Bob's Occasionally Sane Thoughts
Tuesday, September 2, 2014
Monday, May 7, 2012
Installing Sandcastle to Create MSDN Style Documentation
I tried doing this installation on my own and could not seem to make it work, so I found this tutorial which works quite well as far as getting things installed properly. However, I am adding details and issues that I encountered that didn’t seem to be mentioned in this tutorial. Basically, I only followed step #1 in this tutorial and I fixed and reused the sample class Guy.cs.
Sandcastle Tutorial
Here is the version of Sandcastle you will need, or whatever the most recent version happens to be when you read this:
SandCastle version 2.7 download
When you download the correct version it should look something like this (as of now anyhow):

and then you will need to install:
Sandcastle Help File Builder
Step #1
- It is important that you check installed programs and make sure you uninstall any version previous to 2.7. Apparently the installer will not update things correctly.
- The guided installer has a step that will install the Sandcastle Help File Builder for you, so you don’t have to download that yourself.
- Now run the Sandcastle installer and follow the steps. This is such a great installer I really have nothing to add at this point.
Step #2
- Step #2 involves working with the command line and I could not make it work with the instructions given in the tutorial. I continuously got an error stating that no APIs could be found. At first that was a legitimate error because the Guy class was not public, then the ToString method was not commented. However, after fixing those items I still received that error for no obvious reason. So at that point I explain how to do this through Visual Studio 2010.
This is how I did it:
1. Open Visual Studio 2010 and Create a new Class Library project called GuyProject.
2. Rename Class1 to Guy and paste in the following code:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
namespace GuyProject
{
using System;
/// <summary>
/// A guy with a name, age and a wallet full of bucks
/// </summary>
public class Guy {
/// <summary>
/// Read-only backing field for the Name property
/// </summary>
private readonly string name;
/// <summary>
/// The name of the guy
/// </summary>
public string Name { get { return name; } }
/// <summary>
/// Read-only backing field for the Name property
/// </summary>
private readonly int age;
/// <summary>
/// The guy's age
/// </summary>
public int Age { get { return age; } }
/// <summary>
/// The number of bucks the guy has
/// </summary>
public int Cash { get; private set; }
/// <summary>
/// The constructor sets the name, age and cash
/// </summary>
/// <param name="name">The name of the guy</param>
/// <param name="age">The guy's age</param>
/// <param name="cash">The amount of cash the guy starts with</param>
public Guy(string name, int age, int cash) {
this.name = name;
this.age = age;
Cash = cash;
}
/// <summary>
/// Override of the object class ToString method.
/// </summary>
public override string ToString() {
return String.Format("{0} is {1} years old and has {2} bucks", Name, Age, Cash);
}
/// <summary>
/// Give cash from my wallet
/// </summary>
/// <param name="amount">The amount of cash to give</param>
/// <returns>The amount of cash I gave, or 0 if I don't have enough cash</returns>
public int GiveCash(int amount) {
if (amount <= Cash && amount > 0) {
Cash -= amount;
return amount;
} else {
return 0;
}
}
/// <summary>
/// Receive some cash into my wallet
/// </summary>
/// <param name="amount">Amount to receive</param>
/// <returns>The amount of cash received, or 0 if no cash was received</returns>
public int ReceiveCash(int amount) {
if (amount > 0) {
if (amount > 0) {
Cash += amount;
return amount;
}
Console.WriteLine("{0} says: {1} isn't an amount I'll take", Name, amount);
}
return 0;
}
}
}
This is basically the same code that was given in the tutorial except the Guy class wasn’t public and the public ToString member was not commented. Which are both fixed in the above version.
3. Right-click on the GuyProject and select Properties and make sure the XML Documentation File check box is checked as shown in the following screenshot.

4. Build the GuyProject project.
5. Add a new Documentation Project as shown here and name it GuyDocumentation:

6. Right-Click the Documentation folder and select “Add Documentation Sources.”
7. Navigate to the Debug/bin folder where we output our Guy.dll and Guy.xml files on the project build.
8. Select the Guy.dll file and add it. This should automatically bring along the Guy.xml file along with it. If not, then add it as well.
9. Rebuild the solution and when that has completed there should be a chm file in the new Help folder in the GuyDocumentation folder called GuyDocumentation.chm.
10. If you try to open but no content is shown then right-click and select Properties and make sure there isn’t a button showing called “Unblock.” If there is this button then click it and try to open the chm file again.
11. If all is well you should see something like this:

Later on I plan how to blog on integrating this into a TFS build, since there probably isn’t much need for each developer to continuously generate chm files on their local machine.
Have fun documenting!
Sandcastle Tutorial
Here is the version of Sandcastle you will need, or whatever the most recent version happens to be when you read this:
SandCastle version 2.7 download
When you download the correct version it should look something like this (as of now anyhow):
and then you will need to install:
Sandcastle Help File Builder
Step #1
- It is important that you check installed programs and make sure you uninstall any version previous to 2.7. Apparently the installer will not update things correctly.
- The guided installer has a step that will install the Sandcastle Help File Builder for you, so you don’t have to download that yourself.
- Now run the Sandcastle installer and follow the steps. This is such a great installer I really have nothing to add at this point.
Step #2
- Step #2 involves working with the command line and I could not make it work with the instructions given in the tutorial. I continuously got an error stating that no APIs could be found. At first that was a legitimate error because the Guy class was not public, then the ToString method was not commented. However, after fixing those items I still received that error for no obvious reason. So at that point I explain how to do this through Visual Studio 2010.
This is how I did it:
1. Open Visual Studio 2010 and Create a new Class Library project called GuyProject.
2. Rename Class1 to Guy and paste in the following code:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
namespace GuyProject
{
using System;
/// <summary>
/// A guy with a name, age and a wallet full of bucks
/// </summary>
public class Guy {
/// <summary>
/// Read-only backing field for the Name property
/// </summary>
private readonly string name;
/// <summary>
/// The name of the guy
/// </summary>
public string Name { get { return name; } }
/// <summary>
/// Read-only backing field for the Name property
/// </summary>
private readonly int age;
/// <summary>
/// The guy's age
/// </summary>
public int Age { get { return age; } }
/// <summary>
/// The number of bucks the guy has
/// </summary>
public int Cash { get; private set; }
/// <summary>
/// The constructor sets the name, age and cash
/// </summary>
/// <param name="name">The name of the guy</param>
/// <param name="age">The guy's age</param>
/// <param name="cash">The amount of cash the guy starts with</param>
public Guy(string name, int age, int cash) {
this.name = name;
this.age = age;
Cash = cash;
}
/// <summary>
/// Override of the object class ToString method.
/// </summary>
public override string ToString() {
return String.Format("{0} is {1} years old and has {2} bucks", Name, Age, Cash);
}
/// <summary>
/// Give cash from my wallet
/// </summary>
/// <param name="amount">The amount of cash to give</param>
/// <returns>The amount of cash I gave, or 0 if I don't have enough cash</returns>
public int GiveCash(int amount) {
if (amount <= Cash && amount > 0) {
Cash -= amount;
return amount;
} else {
return 0;
}
}
/// <summary>
/// Receive some cash into my wallet
/// </summary>
/// <param name="amount">Amount to receive</param>
/// <returns>The amount of cash received, or 0 if no cash was received</returns>
public int ReceiveCash(int amount) {
if (amount > 0) {
if (amount > 0) {
Cash += amount;
return amount;
}
Console.WriteLine("{0} says: {1} isn't an amount I'll take", Name, amount);
}
return 0;
}
}
}
This is basically the same code that was given in the tutorial except the Guy class wasn’t public and the public ToString member was not commented. Which are both fixed in the above version.
3. Right-click on the GuyProject and select Properties and make sure the XML Documentation File check box is checked as shown in the following screenshot.
4. Build the GuyProject project.
5. Add a new Documentation Project as shown here and name it GuyDocumentation:
6. Right-Click the Documentation folder and select “Add Documentation Sources.”
7. Navigate to the Debug/bin folder where we output our Guy.dll and Guy.xml files on the project build.
8. Select the Guy.dll file and add it. This should automatically bring along the Guy.xml file along with it. If not, then add it as well.
9. Rebuild the solution and when that has completed there should be a chm file in the new Help folder in the GuyDocumentation folder called GuyDocumentation.chm.
10. If you try to open but no content is shown then right-click and select Properties and make sure there isn’t a button showing called “Unblock.” If there is this button then click it and try to open the chm file again.
11. If all is well you should see something like this:
Later on I plan how to blog on integrating this into a TFS build, since there probably isn’t much need for each developer to continuously generate chm files on their local machine.
Have fun documenting!
Friday, April 27, 2012
How to Convert an ISO to a VHD
Okay, for starters I don’t know if you can actually convert an iso directly to a vhd, however I am going to show you the process to create a vhd and install your iso onto that vhd all at once. I should also note that this is what I had to do on Windows 7, and I imagine it's a much easier task on Windows Server.
First you will need to download WIM2VHD converter from:
WIM2VHD Converter
Then follow these steps:
1. Open an elevated command prompt.
2. Then enter:
cscript wim2vhd.wsf /wim:X:\sources\install.wim /sku:anynameyoulike
/vhd:c:\AnyFolderThatAlreadyExists\AnyNameYouLike.vhd
Note: If you run this and you get an error stating that wim2vhd.wdf cannot be found in such and such a place, you can just move this file to where the command prompt is looking for it. Of course, you could use the trick in another one of my blog posts that opens a command prompt in the folder where your wim2vhd.wdf file already exists. Your choice.
3. You will need to install Virtual Clone Drive (which is free) from here:
Virtual Clone Drive Download Page
4. Double-click on your iso file which should then show something like this:

As you can see our iso was mounted on drive E:, so in the command above you will need to replace X: with whatever drive your iso was mounted to.
5. When you hit enter you should see something like this:

6. Once the process is complete, you can attach the VHD to a virtual machine and boot, or you can boot natively if your system has a Windows 7/Server 2008 R2 boot manager.
7. Have fun!
First you will need to download WIM2VHD converter from:
WIM2VHD Converter
Then follow these steps:
1. Open an elevated command prompt.
2. Then enter:
cscript wim2vhd.wsf /wim:X:\sources\install.wim /sku:anynameyoulike
/vhd:c:\AnyFolderThatAlreadyExists\AnyNameYouLike.vhd
Note: If you run this and you get an error stating that wim2vhd.wdf cannot be found in such and such a place, you can just move this file to where the command prompt is looking for it. Of course, you could use the trick in another one of my blog posts that opens a command prompt in the folder where your wim2vhd.wdf file already exists. Your choice.
3. You will need to install Virtual Clone Drive (which is free) from here:
Virtual Clone Drive Download Page
4. Double-click on your iso file which should then show something like this:
As you can see our iso was mounted on drive E:, so in the command above you will need to replace X: with whatever drive your iso was mounted to.
5. When you hit enter you should see something like this:
6. Once the process is complete, you can attach the VHD to a virtual machine and boot, or you can boot natively if your system has a Windows 7/Server 2008 R2 boot manager.
7. Have fun!
Monday, March 12, 2012
Exposing the Internals of an Assembly to Another Unit Testing Assembly
I have two projects one is production code (called Main), the other is a unit test project to test the real production code (called Main.Test). Both Main and Main.Test are signed with a strong name key. To expose the internal stuff to the Main.Test project we have to add an item at the bottom of the AssemblyInfo.cs class (in Main, not Main.Test) which looks similar to this:
(Note: Names and Public Keys have been changed to protect the innocent, and semi-innocent)
[assembly: System.Runtime.CompilerServices.InternalsVisibleTo("Main.Test, PublicKey=1028000004800030940000100602000000240000525341310004
0000010001001312297cb7c20cfb40f4efef6e5e96dcf84fa46db61b43b2bf65cf
49d547d6a5f14ef07f54d6313b39b10ae631e158748b39e2ab4c816b0753f2a
432160dddf06a1a7170d7ab6e4df081784da7a08b37a3a8a47b722ed6cf6c1c
f6896bc709f805a8087a758156171b7deb74a2ded23ce55688f9b2a67bfeedf1
081496da76f4")]
Now you may be thinking to yourself: "That's great bob, but how in the hell did you get this value for the public key in the first place?" Good question! Here is the answer:
1. Open a visual studio command prompt and enter this command:
(This is using Visual Studio 2008)
C:\Program Files\Microsoft Visual Studio 9.0\VC>sn -p "[RestOfPath]\Main.Test\MainProjectStrongNameKey.snk" "[RestOfPath]\Main.Test\MainProjectStrongNameKey.publickey"
2. You should then get a message that says "Public key written blah blah blah".
3. Now in the same command window, enter this command:
C:\Program Files\Microsoft Visual Studio 9.0\VC>sn -tp "[RestOfPath]\Main.Test\MainProjectStrongNameKey.publickey"
Now the command line should spit out the long public key, which you can then use like I did above. Now you can test your internal members in the Main project from the Main.Test project
Happy testing!
On the Utility of Visual Basic and Haikus
I was working with some sample code which unfortunately was in Visual Basic. After working on translating the Visual Basic to a language that doesn't suck (anything else is better) for about 20 minutes I started to try to think of ways to increase the rate I translated the VB code to C#. My first thought was to write a Haiku about it. Here it goes:
I hate Visual Basic
The Bubonic Plague
is much more preferable
After writing this I realized that writing Haikus is like writing Visual Basic, both are a waste of time. ;)
I hate Visual Basic
The Bubonic Plague
is much more preferable
After writing this I realized that writing Haikus is like writing Visual Basic, both are a waste of time. ;)
Fast way to Change the Directory Path in a Console Window
I have been aware of this trick for over a year now, but I haven't shared it until now because of my legendary procrastination when it comes to blogging. If you open a command prompt the usual way this is what you get:
Notice the path defaults to my users folder? So in order to change it I have to enter a command like this: cd C:\ which will change the path to the root C:\ folder. I have learned a better way than doing the cd command to get to arbitrary folders.
If you click on your arbitrary folder, then hold down the Shift key and right click you will see this menu come up:
Notice there is a choice to “Open a Command Window Here?”
Clicking this menu item will give you this:
Woo hoo! No cd command nonsense!
One other thing, you may need to have your command window open with administrator privileges. here is a link that explains how you can make that happen automatically:
Setting Command Window to Open in Administrator Mode
Notice the path defaults to my users folder? So in order to change it I have to enter a command like this: cd C:\ which will change the path to the root C:\ folder. I have learned a better way than doing the cd command to get to arbitrary folders.
If you click on your arbitrary folder, then hold down the Shift key and right click you will see this menu come up:
Notice there is a choice to “Open a Command Window Here?”
Clicking this menu item will give you this:
Woo hoo! No cd command nonsense!
One other thing, you may need to have your command window open with administrator privileges. here is a link that explains how you can make that happen automatically:
Setting Command Window to Open in Administrator Mode
Subscribe to:
Comments (Atom)