$.geo.WKT object

There are two commonly used formats to display a geometry object as text.

The first is GeoJSON which is a pure JavaScript Object Notation (JSON) encoding of objects following a specified format. jQuery Geo exclusivly uses GeoJSON objects throughout the library. You can use the native HTML5 JSON object to convert between GeoJSON text and the JavaScript objects themselves.

The second is well-known text (WKT). It considerably predates GeoJSON and can look a lot nicer when viewed in a database (well, databases that aren't JSON based to begin with (CouchDB, MongoDB, etc.) or databases that have more suppport for JSON such as PostgreSQL 9.2). The point is that at some point in web mapping, you may come across WKT and jQuery Geo has you covered.

The $.geo.WKT object has two functions: parse & stringify. They are the same names as the HTML5 JSON object by design to give you the option of writing serialization functions that can take either the native JSON object or $.geo.WKT. The $.geo.WKT.parse function parses as WKT string and constructs a GeoJSON object that's ready to be used elsewhere in the library such as the geomap widget's append function. The stringify function accepts a JavaScript object that follows the GeoJSON specification and returns its WKT equivalent.

parsing WKT strings

To convert a WKT string into a GeoJSON formatted JavaScript object, you can pass the WKT string into the $.geo.WKT.parse( ) function:

var geojsonObject = $.geo.WKT.parse( 'POINT (-71.070554128125 42.36859979580157)' );

jQuery Geo supports parsing on all geometry types from Point to GeometryCollection.

converting objects into WKT

To convert a GeoJSON formatted object into a WKT string, pass the object to the $.geo.WKT.stringify( ) function:

var wkt = $.geo.WKT.stringify( {
  type: 'Point',
  coordinates: [ -71.070554128125, 42.36859979580157 ]
} );

Since all geometry-based arguments in jQuery Geo are GeoJSON formatted JavaScript objects, you can convert them directly in a callback function. For example, if you are handling the click event:

$( '#map' ).geomap( {
  click: function( e, geo ) {
    alert( $.geo.WKT.stringify( geo ) );
} );

patterned after JSON

The $.geo.WKT object's function names are the same as the native JSON object you find in modern web browsers. Therefore, you can write a geometry object serialization function that takes either:

function geoToString( geo, serializer ) {
  return serializer.stringify( geo );

// log JSON
console.log( geoToString( geo, JSON ) );

// log WKT
console.log( geoToString( geo, $.geo.WKT ) );

This limited example is not terribly useful but hints at what can be done.