[GeoJSON] GeoJSON in OpenLayers

Tim Schaub noreply at geocartic.com
Sun Mar 25 15:22:24 PDT 2007


Hello-

I've put together a GeoJSON reader/writer for OpenLayers.  You can 
digitize some features and see the resulting GeoJSON (my own flavor) in 
this example:
http://dev.openlayers.org/sandbox/tschaub/geojson/examples/geojson.html

I'm not going to do anything with the reader until there is some 
agreement on the data structure.  Also, I think this is understood, but 
I would suggest that any service that accepts something like 
outputformat=json also takes a callback=somestring - where the result is 
  mime-type text/javascript and looks something like "somestring(json);" 
(allowing the somestring to be anything, including "NameSpace.property = 
" or "myFunction").

If you digitize some points, lines, and polygons, I think the data 
structure I've got in mind should be obvious.  It looks something like:

{
     "features": array,
     "crs": string
}

The features array is an array of features where each feature looks 
something like:

{
     "id": string,
     "type": string,
     "attributes: object,
     "data": array
}

The id string is any string. The type string is one of "point", 
"multipoint", "linestring", "multilinestring", "polygon", or 
"multipolygon" (and I'm entirely open to CamelCase).  The attributes 
object is any object and the data array depends on the type.

For type point, the data array is an array of coordinates like:
[number, number]

For type multipoint, the data array is an array of points as described 
above.

For type linestring, the data array is an array of points as described 
above.

For type multilinestring, the data array is an array of linestrings as 
described above.

For type polygon, the data array is an array of linestrings as described 
above - and the client assumes that those linestrings will be linear 
rings.  The first item in the array is the exterior ring and any 
subsequent items are interior rings.

For type multipolygon, the data array is an array of polygons as 
described above.

Finally, all features in the array have the same coordinate reference 
system, represented by the crs string.

Though I haven't read too carefully, I think this captures the 
alternatives discussed here.  Sean's Pleiades GeoJSON would use the 
attributes object for things like title, classname, and center.  Same 
with things like AREA, PERIMETER, and DESCRIPTION from Chris.

I'm happy to add this to the wiki somewhere if it looks sensible.  Or, 
if it looks like gibberish, let me know.

Tim

Related links -

The GeoJSON reader/writer
http://dev.openlayers.org/sandbox/tschaub/geojson/lib/OpenLayers/Format/GeoJSON.js

Which requires
http://dev.openlayers.org/sandbox/tschaub/geojson/lib/OpenLayers/Format/JSON.js

And if you want to make your own GeoJSON enabled OpenLayers application, 
point it to
http://dev.openlayers.org/sandbox/tschaub/geojson/lib/OpenLayers.js



More information about the GeoJSON mailing list