Monday, November 29, 2010

Video Games for Christmas - Buy Them Or Build Them and a Few Gadgets

I am a GIS web programmer and today I am taking a break. I went virtual Christmas shopping for fun. If you don't like video games and you have someone that likes video games on your list, Christmas shopping can be a nightmare, especially if you look at all the items for sale; a blank look comes across your face and you suddenly feel light headed. Well, I hope what I have to share will ease your shopping pain. If you get lost, just ask a question in the comments and I will help you out. This is one item on your list I will help you out with.

For My GIS Friends, Conference Attendees, and My Sanity

Lately, I have been on the go with no breaks and tech talk; let me break away from that by adding some ridiculously priced products which is part of my personality where I enjoy taking what I do light no matter how complex.

If you ever said you wanted to give tanks(you should check out the reviews on this); now you can. I have also added a Segway and a trailor(really?!), yes really. I call it an i-Haul. If you have to present at the User Conference or any place you have to get around quite a bit. It might be ideal.



Don't Need To Be Mobile, But Need Mobile Capabilities?

At any event, you want to be able to communicate. And if you are a developer like me; if your mobile tool of choice doesn't have what you need, you may want to program it. Here are some options for you in one spot.




Motion Sensing XBox 360 Got You Froze - Here is What I Thaw Right Down To The Batteries





Maybe Your Shopping List Is A Wii Different; Let Mii Take Care Of You



Video Games And Consoles Making You A Grinch? Get These Items And It Could Ease That Financial Pinch



Merry Christmas! I wish I could share more, but all I want for Christmas is a screen door.
To make this wish come true; it will depend on purchases by you. And I will be watching. :-)

Chris

Saturday, November 27, 2010

Write XML in MS Access 2010 Using VBA

There is an XML theme for everything that I am writing and this post is no different, except this time, I want to meet you, so I am going to give you the opportunity to meet me. While you're waiting, you can try a game below or just wait for my twitter/LinkedIn notifcation. You can go right to my twitter page by clicking on my name right below the sympathy donation button.

Instead of a cartoon before the show, I will use video games. The channel that I will be broadcasting on is just below the games.





Preparing to go live now.

Create XML in VBA

'Author: Chris Sergent


'Date Created: November 27, 2010

'Purpose: To provide a basic example on how to create an XML document with VBA in MS Access.



Private Sub btnCreateXml_Click()

'Open the vbSkeleton.xml document if it exists and replace all contents. If

'the file does not exsist create it.

Open "c:\Templates\vbaSkeleton.xml" For Output As 1

'Declares the document as an XML document. This element is required at the top of the XML document.

'The declaration is required and must go at the top and only needs to be referenced at the top of the

'document. After the root element, XML documents require beginning and ending elements.

'That is to say that if we have an element or a tag such as , it must have a corresponding

'tag of
even if there is no content.

Print #1, "

Print #1, ""

Print #1, ""

'An XML document requires one and only one root or parent node is allowed and it must be empty.

'spatialDeveloperKnowledge is our parent node.

Print #1, ""

'Although this is like a parent node, you could add an attribute to the Developer node such as id.

'I don't recommend it. We'll use XPATH to navigate the XML DOM in later examples.

'There are no rules for this data, but that will be taken care of in a future XSD example.

Print #1, vbTab & ""

Print #1, vbTab & ""

'Set the focus on the txtFirstName control to obtain information for writing

txtFirstName.SetFocus

Print #1, vbTab & vbTab & "" & Me.txtFirstName.Text & ""



'Set the focus on the txtLastName control to obtain information for writing

txtLastName.SetFocus

Print #1, vbTab & vbTab & "" & Me.txtLastName.Text & " "



'Set the focus on the txtCity control to write the city data to file

txtCity.SetFocus

Print #1, vbTab & vbTab & ""

Print #1, vbTab & vbTab & "" & Me.txtCity.Text & " "



'Set the focus on the txtState control to write the name of the state

txtState.SetFocus

Print #1, vbTab & vbTab & "" & Me.txtState.Text & " "



'Set the focus on the txtCountry control to write out the name of the country

txtCountry.SetFocus

Print #1, vbTab & vbTab & "" & Me.txtCountry.Text & " "



Print #1, vbTab & vbTab & ""

'Set the focus on txtX1 to obtain the X coordinate

txtX1.SetFocus

Print #1, vbTab & vbTab & "" & Me.txtX1.Text & ""



'Set the focus on txtY1 to obtain the Y coordinate

txtY1.SetFocus

Print #1, vbTab & vbTab & "" & Me.txtY1.Text & ""



'Add measuring value

Print #1, vbTab & vbTab & ""

Print #1, vbTab & vbTab & "NA"



'Add elevation coordinate

Print #1, vbTab & vbTab & ""

Print #1, vbTab & vbTab & "0"





'Set the focus on the txtSkills control to write out the list of skills the developer has

txtSkills.SetFocus

'I added this with an array of skills, because I want to see, not just a developer meetup;

'but a meetup mashup. A developer that limits their access to knowledge, limits their potential

'to provide a solutions and skill dilutes the potential skills in creativity and problem solving.

'Even for the most skilled, a novice can still teach the elite in our field how to communicate with a

'novice. Knowing how to apply what you have learned is not as important as being able to enable others

'with the knowledge and the ability to apply it without help.

Print #1, vbTab & vbTab & "" & Me.txtSkills.Text & " "



Print #1, vbTab & "
"

Print #1, "
"

'Closes file. If you don't do this, you might receive a message that this file is in use. Don't leave it out.

'If you open a file in code; close a file in code. The same thing holds true with database connections.

Close #1



'This displays once the click event completes. There is no error checking for this example

'If you receceive an error and, ensure you have a folder named Templates in your root folder or "C:\" drive

'or modify the code to so that there is a folder to write to

MsgBox "vbaSkeleton.xml file has been created.", vbOKOnly, "File Written"





End Sub

 
Sample of the Output Document
 
Sample of a Resume with Inline CSS

Wednesday, November 24, 2010

ESRI Print to PDF Task Sample for ArcGIS Server 9.3.1 - Part of the Thanksgiving Code Buffet

]If you have been following along, then you know this weekend I am hosting a Thanksgiving code buffet. Perhaps you don't want to miss out on these knowledge dishes when they are fresh because you have to shop. Don't worry, I posted Black Friday Specials and gift cards right here for you. And I had to shop around for ones that I thought you may like. But, if you don't want anything to do with that online shopping stuff; I took care of that too. Content is at the very top, bottom and on the sides. There are a few gadgets I will share with you in my posts. They are expensive, so I don't expect you to buy them, but if you do; I hope you share. We can jsut have fun talking about them.

Now for the other PrintTask for the Web ADF. I have referred to this as the main course; it is our turkey. But, I will do the best I can to make it tasty.

I read this post http://www.arcgisserverblog.com/2008/12/print-map-with-arcgis-server.html and decided to do the same thing that I do when I buy a new pc; take it apart and change it. In this example, we are going to export the image of the map, obtain the name of the new image file and then perform a page redirect from there just like in the ESRI JSAPI 2.1 Print to PDF example.

 The screen print I planned to share with you is not available right now, but I do have a screen print of the MapResourceManager. With the main course like this, what does that say about my side dishes?

To define your map, we don't use the REST API in this example, so set you MapResource Manager as displayed in the image below.


It is the same resource. We are just not using the REST API in this one. Now for the one thing that separates these two examples; the code.

The following is plain text, but this way you can copy it for your own application. You do need a license for ESRI ArcGIS Server to use this code. Nothing to worry about; there will be plenty of other map examples that will cost you nothing to use. But if you are unable to move away from the Web ADF right now, then this you may find this code helpful for two reasons. The first is that you will be able print to pdf and the second is that the page that you redirect to, that creates the pdf is the exact same page. So, you can migrate this PrintTask to the JSAPI 2.1 if that is your API of choice. If not, you may be interested in another such as Silverlight or Flex. We aren't serving those this weekend. But if you want to bring a code plate and serve one of those up, let me know. I will link to it as this will free up time for me to share additional code sooner.

Here is your export function for exporting your image:

Public Shared Function ExportMapImage(ByVal ADFMap As ESRI.ArcGIS.ADF.Web.UI.WebControls.Map, ByVal OutputPath As String) As String


Dim bitmaps As New System.Collections.Generic.List(Of System.Drawing.Image)()

For Each mf As ESRI.ArcGIS.ADF.Web.DataSources.IMapFunctionality In ADFMap.GetFunctionalities()

If mf IsNot Nothing Then

Dim mapImage As ESRI.ArcGIS.ADF.Web.MapImage = mf.DrawExtent(ADFMap.Extent)



mapImage = mf.DrawExtent(ADFMap.Extent)

If mapImage Is Nothing Then

Continue For

End If

If mapImage.GetImage() IsNot Nothing Then

bitmaps.Add(mapImage.GetImage())

End If

End If

Next



Dim newImg As New System.Drawing.Bitmap(Convert.ToInt16(ADFMap.Width.Value), Convert.ToInt16(ADFMap.Height.Value))



Dim imgGraphics As System.Drawing.Graphics = System.Drawing.Graphics.FromImage(newImg)

Dim i As Integer = bitmaps.Count - 1

While i >= 0

imgGraphics.DrawImage(bitmaps(i), 0, 0, Convert.ToInt16(ADFMap.Width.Value), Convert.ToInt16(ADFMap.Height.Value))

i += -1

End While

Dim Ticks As Long = DateTime.Now.Ticks

Dim ImagePath As String = OutputPath & "mapimage" & Ticks & ".png"



newImg.Save(ImagePath, System.Drawing.Imaging.ImageFormat.Png)



Return "mapimage" & Ticks & ".png"

End Function
 
 
Once you export your image, you need call the above function. We are going to use a button click event.
 
Protected Sub btnPrintMap_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles btnPrintMap.Click


'The following is how you should write out the path variables

'TextBox1.Text = "http://YourWebSite/Images/" & ExportMapImage(Map1, "http:///YourWebSite/Images/").ToString

Response.Redirect("PrintTaskResults.aspx?sender=" + TextBox1.Text.ToString)



End Sub
 
Although we are redirecting to another page, the embedded pdf page has something that you did not see in the last in the JSAPI 2.1. An image is created, but once the pdf is created, we have the code delete the image. So, here is the Page_Load event. 



In this line we create our pdf and add the image.



Since the image has been used for the pdf and it does not contain a unique name, we don't want our Images folder to constantly grow, so we check to see the image file used in the pdf still exists. If it does, it is deleted. In this way we have automated the clean up of our Images folder.


That concludes this sample, so now feel free to download the Print to PDF in ESRI ArcGIS Server 9.3.1 using the Web ADF in .NET now.

Tuesday, November 23, 2010

ESRI Web ADF Print Task - Up Next; It's About The Turkey

With the ESRI Web ADF on it's way out the door, I plan on posting a PrintTask just like the one I did for the JSAPI 2.1. So bookmark this page or get notified through RSS.

If you are looking forward to the XML, there is no reason to worry. I have already tested most of the PrintTask for the Web ADF. I am planning to do one for Silverlight and YahooMaps as well. They will be next.

Thanksgiving is coming up and so is Black Friday. If you are not from the US and possibly don't know what I mean. This Thursday Americans eat outside of their means and the next day we spend outside of our means. I am only kidding, but shopping the day after Thanksgiving can be quite wild.

But on Thanksgiving, it's a buffet of food. And for the code hungry, I am going to post a code buffet. Since it's a buffet, your food for thought will not be in one programming language. I plan on feeding you some basic knowledge for commenting syntax and structure. Here's the knowledge menu.

Now the Web ADF has been a thorn in my side in programming right at the start. I thought it was me, but ESRI is dropping the Web ADF.

For our main course and the knowledge turkey will be the ESRI Web ADF Print Task. It will be just like the PrintTask I wrote about for the ESRI JSAPI 2.1.

Now with our main course out of the way. We are going to lay out the table with a full spread of XML writing through code. Each will result in the exact same XML file, but will be processed differently. and the dishes are;

- VBA in MS Access topped with a GUI
- VBScript served plain
- HTA stuffed with VBScript
- Perl served plain
- Visual Basic.NET and C# topped with a GUI and peppered with parameters for input
- Python sauteed with silly comments providing we have a wind blowing in a direction that is not opposite of the direction we had hoped for, unless it is a Tuesday or are making a left turn (if you're not familiar with it, Python related to Monty Python; so humor or an attempt at it is encouraged)
- Java caffeine free
- Sybase PowerScript for PowerBuilder but the GUI has been filleted

and for dessert, a dynamic web based resume presented with a splash of CSS topped with a Twitter and LinkedIn widget

XML appetizers will be provided before, during and after the knowledge feast.

And if you are worried about missing all of this free code and missing Black Friday specials, I will share specials with you too. But if you must shop, by all means shop. But if you are riveted to the spot enjoying one code course after another, I will do my best to share some things right here with you. I have also been having fun with these Amazon ads because, guess what; they are widgets. And I think web gadgets are pretty cool. Yes, I like that word.

I look forward to sharing with you. If you would like to bring a knowledge plat of your own, please feel free to cook some code up and share it with us.

So, Happy Thanksgiving. Enjoy.

Functional Code is Nice, But Nice Code is Better

The ability to print my map to pdf in ESRI ArcGIS Server is something I have wanted for a while. There are several examples available on ESRI's website from ESRI and others developers have shared their examples as well. But I haven't found the print examples easy to follow or to apply. So I made one "Chris" simple. Feel free to read about my PrintTask if you haven't. It uses iTextSharp, but one thing I did not mention is that you can use the same code to print any image that iTextSharp supports and everything I did can be done at no cost to you. Having a job in GIS isn't even required.

Now for the topic at hand; clean code. I thought it was more important for my fellow developers to have the knowledge of how to print a map to pdf. If I were in their shoes, I would not want to wait to learn the functionality that could be added to my application, so I did not want you to have to wait either.

And now you have a PrintTask in which you can integrate into your ESRI ArcGIS Server applications using the JavaScript API 2.1. However, if you know how to export the map in Flex or Silverlight, you can apply this same example in those API's as well as the page that displays the pdf uses no GIS API's of any kind; it just references the location of the image.

But why am I still talking about the PrintTask? I did not separate presentation, functionality and content. If you are not familiar with this, you are not alone. Now you need to know, I am not "the authority" on website design, but I do the best I can to apply what I learn from other professionals and programming books.

I listed three books that I have bought below from SitePoint. They are easy to read books and a good starting point. You can check your local library first. The first one I mention is quite possibly available there and the best one of the three. Build Your Own Website the Right Way will help with basic CSS, but I would skip the chapter on HTML forms. I did. I want to build .NET pages, so HTML forms is not an area I was too concerned about. Concepts of this book are applied to my examples, but where the book takes you on a learning tour of creating well-formed websites with HTML; I will extend the information and show you how to apply those same well-formed techniques in .NET. That's not in the book. Simply JavaScript is a lot to take in, but it does drive home the importance of separation of content, display and functionality. jQuery is there because I just like it. However, if you don't want or need a book, don't worry. I plan on taking you through the steps that these books use and plan on showing you how to take that information further, by integrating  the topics with .NET, JavaScript Framework, and mapping API's. But, you do need to be aware that I will be starting with .NET, not HTML. It will be easier than you think, just follow along and if you get lost, just speak up and I will give you directions.




Before sharing how to build a well-formed site template, I want to share with you some basics that I think are important, commenting and documenting your work by showing you how to write code that creates an XML file for you.


If you want to become more involved with the developer community collaborating, live, online and even in person, I would like to invite you to join the Web Dev Network on LinkedIn. Just click the link below and request to join. If you do not already have a LinkedIn account, you will need to create it first. But it's free and we have live online meetings. There is no video yet, but you can talk, watch, and even present.
Join

Saturday, November 13, 2010

ESRI Print Task Sample for the JSAPI 2.1


I failed at giving myself deadlines, so I thought I would challenge myself. There are examples of creating a PrintTask, but they seem to be hard to follow, at least for me. If you have found this to be the case, then this utility and post is for you. This example will be very basic as will show you how to print a map to pdf using the ESRI ArcGIS Server JSAPI 2.1; but just the map. To simplify the code, I have not included code to format the pdf itself. This is a how to, because any solution is better than no solution when a solution is needed. There are only two blocks of code in the entire application; one block in JavaScript and one in VB.NET so don't let the details of this post overwhelm you.

Well I just finished writing the PrintTask for the ESRI ArcGIS Server JSAPI 2.1. It took me 45 minutes to build the site from scratch. Unfortunately, I missed a parameter that I was passing and it slowed down my development time. It then took me the additional time to write this blog, not to mention adding the images manually after writing the blog in MS Word. But now the blog and the utility are ready.

I used ESRI's web service in the REST API at http://sampleserver1.arcgisonline.com/ArcGIS/rest/services/Demographics/ESRI_Population_World/MapServer which displays below in the exportMap.aspx. Yes, a .NET page using the JSAPI.


What happened? We used the .NET control and had it run some JavaScript on the ClientClick event and then passed the variable to a label .NET control. If you have had problems in the past with using .NET controls with the ESRI JSAPI, you may need to lay out the button like the following image.


With that in place, we can click on our .NET button with worrying about the page refreshing. Be careful! Not including the ContentTemplate tag within the UpdatePanel and surrounding the Button will make your application crash. I have been there a few times.
So, we click our button and then just for you information, a JavaScript message comes up. Everything is going smooth so far if you see this.


A new browser window opens, but before we look at that. Check out what displayed on our page of the world right above the Print Map button. Where did that come from?


Before we can print the image, we need to use the export function. You can read more about exportImage in ESRI online documentation at http://resources.esri.com/help/9.3/arcgisserver/apis/rest/index.html?export.html.
But, what about the code? If you have a hard time following it, let me take you through it.
Remember our .NET control? Take a look below and you will see that the OnClientClick event calls up our JavaScript that resides at the top of the page. The function we call is exportimg();



Before we even called that function, there were a few things handled behind the scenes.
We first put in a block of code telling our application that we want to use the ESRI ArcGIS Server JavaScript API 2.1.



Next, we start a new script block and tell our application we will be using esri's dojo tools. Now the question to ask ourselves, even before writing code is; what do we want? The application wants to know the same thing. Just like a restaurant, you place your order and what you requested is prepared for you. With this information in hand, let's place our order. We need three things total and that is it. At our sit down web service, the application uses parameters, the variables that will be used to plug into our code to make it work. It's just like, if you don't order steak, you don't get steak. So, if you don't ask for a map, you won't get a map. To do this we add three declarations.






The first is our web service, displayed as dynamicMapServiceLayer. The second is the variable params which we need for the purpose of defining the output of our map image that we export. The final variable is the printMapURL. Remember the old saying about the most important thing is "location, location, location". This holds true for the printMapURL. It serves one purpose. When the map is exported, it will tell you where it went. And we need that for our PrintTask. Below is the entire script block. Don't worry, we will go through it, but take a look at the line function init() and right at the bottom of the script where you see dojo.addOnLoad(init); dojo.addOnLoad(init); will run as it does not reside in a function. Time to load the code; we define our map in the first line. In the next line, here is one of our three parameters; params. Here it defines the dpi or dots per inch for printing, the width and height of the map and the format to be exported. Define the web service in the dynamicMapServiceLayer and then add it to the map with map.addLayer. Everything is ready to enable exporting our map. We haven't talked about the printMapURL. Do I really need to say; but wait there's more? Jump down below the next block of code and keep reading.



There was a lot to talk about once we clicked that button, but I wanted you to know how everything got wired so this next piece would make more sense.
Now that we have clicked the button to call the exportimg function, it's nice to have information for our variables as we use them immediately.
The exportimg function uses the parameters that we already have and then we use the expmapimagfunc(mapimg) function to return our third variable. The printMapURL.





We use the getElementById and then set the lblMapLocation text to the URL and the image file that was exported. And finally we open the CreatePdf.aspx and append the printMapURL as a variable to pass into our new browser window. From here we leave JavaScript and move to VB.NET. Now if you want the project written in C#; you can use a free converter to do so at Developer Fusion.








Our code runs and our new browser window opens and there you have it; a pdf embedded in our web page. And we are finally at that awkward question; where do embedded pdf's come from? That's not a pdf down below, but it plays a real one on print sample site I will be sharing with you.




Our web page has already defined a pdf in this line of code below. You can use it if you just want to embed a pdf; no printing required. It just references our pdf in the pdfs folder named "Image.pdf".




But that did not create the pdf. I used a tool called iTextSharp which you can download. Many examples are written in C#, so converted C# code to VB.NET. If you have would like a C# example, just ask. Or if you have try to create a C# example and have problems, just post your question about it too.




In our Page_Load event, we define the location of the pdfs folder and variable that we want to use to print our pdf. We first defined the URL of the image as printMapURL. Once we had the value, we assigned it to sender. At the top of our web page is where that image went to.
You will notice that within the address bar after the current web page, sender was added it was set to the value of the location of the image and the image itself.
CreatePdf.aspx?sender=http://sampleserver1b.arcgisonline.com/arcgisoutput/_ags_map687a6f058c52422f9503a216b51da659.png
Finally, we create a new document and then attempt to write our our pdf in the PdWriter by creating a pdf in the pdfs folder named "Image.pdf" . Our pdf is created, but there is no image yet, so we open the new document to write on it or is this case, to add the image. To do that we look at the sender value, which is the URL of our map image; and then we add it to the pdf. Once it is written, we close the pdf and then for informational purposes only, I add the text PDF has been created. You do not need this.
And there you have it, the PrintTask for the ESRI JavaScript API 2.1 using Visual Basic .NET and the .NET Framework 4.0. Try this tool out and tell me what you think. I will be integrating, but for now; I wanted to keep it simple.
Or share a tool or make this one better.

And here is the PrintTask for the ESRI JSAPI 2.1 written in both VB.NET and C# - http://www.arcgis.com/home/item.html?id=56bc091f27134b6b9296db9ad52261a9

Other GIS Resources

For a few other examples of GIS pages in .NET, you can take a look at samples of just basic .NET Framework 3.5 maps that I created with no feature as of yet in the following API's: Microsoft Bing version 6.3,Yahoo AJAX version 3, and Google version 3. An example of Google maps in the previous version 2 as just basic html pages are at: http://sergentsolutions.net/maps.html which includes images from Flickr, an example on how GeoRSS can be applied and another at http://sergentsolutions.net/maps/testmap.html which provides you with an example on how to create tabbed balloons like below. That's all I have to share for now. What would you like to share? Your feedback is important to me. Talk to you soon.

Chris

GeoBloggers Posts You May Like