I have an internet/web application sitting on top of OpenLayers, ExtJS, ColdFusion and PostGreSQL/PostGIS. I can query the geometry from PostGIS thru ColdFusion without any problems. If you are in ColdFusion 7, then you need a postgresql driver installed, but I do suggest you use ColdFusion 8 as the driver is built-in.

To display the geometry, I typically use AsGML output from PostGIS and supply that path to OpenLayers. Although it works as expected, I need to invest in two (2) http calls to the server:

  • 1st Request: Call search.cfm and get the attributes (name, address, tel_no, etc…). Afterwards, display the results on an Ext.Window.
  • 2nd Request: Display the geometries on OpenLayers using GML.

We have decided in our organisation not to write out the javascript statements on AJAX calls but instead retrieve them as Data Objects (either XML or GeoJSON). Putting in javascript statements on AJAX calls seems harder to debug. GeoJSON is highly preferred for internet mapping applications since it has a unique ability to see the javascript objects quickly on the browser, making debugging easier.

I wrote a simple cfc below to be able to parse the geometry from PostGIS using AsText(geometry). The function accepts a geometry as string and returns a structure containing the type and the coordinates.

<cfcomponent displayname="GeoJSON" hint="I am a GeoJSON Component. I parse a geometry and return it as JSON" output="false">
 
	<cffunction name="init" access="public" output="false">
		<cfreturn>
	</cfreturn>	
 
	</cffunction><cffunction name="format" returntype="Struct" output="false">
		<cfargument name="geomtext" type="String" required="true">
		<cfset>
		</cfset><cfset>
		</cfset><cfset geometry="StructNew()">
		</cfset><cfset>
		</cfset><cfset>
		</cfset><cfset>
		</cfset><cfset>
		</cfset><cfset>
 
		</cfset><cfset geometry.type="Mid(arguments.geomtext,">
		</cfset><cfset>
 
		</cfset><cfset geomreplacedbrackets="Replace(arguments.geomtext,">
		</cfset><cfset geomreplacedbrackets="Replace(geomReplacedBrackets,">
 
		<cfloop from="1" to="#Len(geomReplacedBrackets)#" index="i">
			<cfset curchar="Mid(geomReplacedBrackets,">
			<cfif>
				<cfset geometry.coordinates="geometry.coordinates">
	    		</cfset><cfset secchar="Mid(geomReplacedBrackets,">
				<cfif> <!--- Get the next character from the current position and see if its numeric --->
					<cfset posi="i"> <!--- Get the position of "["--->
					</cfset><cfset posj="find(']',">
					</cfset><cfset tempgeom="Mid(geomReplacedBrackets,">
					</cfset><cfset i="posJ"> <!--- reinitialize i --->
 
					</cfset><cfset>
 
					<cfloop list="#tempGeom#" index="tempGeomIndex" delimiters=",">
						<cfset tempvalue="[">
						</cfset><cfset geomreplaced="ListAppend(geomReplaced,">
					</cfset></cfloop></cfset>
 
					<cfset geometry.coordinates="geometry.coordinates">
				</cfset></cfif></cfset></cfif></cfset></cfloop>
			<cfelseif>
				<cfset geometry.coordinates="geometry.coordinates">
			</cfset></cfelseif></cfset>
 
		<cfset geometry.coordinates="Replace(geometry.coordinates,">
 
		<cfif>
			<cfset geometry.coordinates="Replace(geometry.coordinates,">
			</cfset><cfset geometry.coordinates="Replace(geometry.coordinates,">
		</cfset>
 
		<cfreturn>
	</cfreturn></cfif></cfset>
 
</cfargument></cffunction></cfcomponent>