I'm given:

  • Extend of WMS Layer
  • SRS

Now I want to, in standard 2.6 Python (no OGR or C lib dependencies), to generate a 100x100m random bounding box within that extent (assuming it is more than 100x100 itself).

I'm allowed to make the following HTTP requests of the following type (caching the results for reuse):

But since I basically don't understand the contents of such files (e.g. proj4 etc), I have no idea whether it's even helpful in my computation.

To repeat what I want:

  • A random 100x100m bounding box within the layer extend, with coordinates in the SRS

The code could look like this, when calling:

random_bbox = random_bbox(extent=[minx,miny,maxx,maxy], srs='epsg:25832', width_meters=100, height_meters=100)

So, how to do this, given this information, in "pure" python? I'm of course willing to write a heap of code, so just need some fairly precise hints.

Sorry if I missed some details, I'm making pancakes for my kids while writing this :-)

  • No dependencies at all? Not even pyproj? It's not impossible, but it's a fairly tall order. Mar 3, 2012 at 11:36
  • I want few deps because I'm writing a very minimal monitoring app for geospatial services (starting with WMS). Would you recommend having proj4 as a required lib dependency? Your answer is a little scary :-) Mar 3, 2012 at 14:33
  • As you say in your answer. I can tell the unit from the downloaded proj4 file. If it's in meters it's easy. If not (e.g. geograph. coordinates), I find the UTM zone, transform geogr. extent to this projection, make a 100x100 box, transform back to the orig. srs. That leaves one question: how do I find the utm zone? Found an answer for that: stackoverflow.com/questions/9186496/… Mar 3, 2012 at 14:50
  • I think proj.4 is a small price to pay for simplicity, especially as there are Python bindings for it (pyproj). There are other projection libraries out there, but Proj.4 seems to be the de-facto standard. It also has no other dependencies. Mar 5, 2012 at 10:36

1 Answer 1


If your maps are always going to be in a projected coordinate system, that is locations are expressed in linear units, then you just need to know which units it's in and multiply by the relevant conversion factor. For instance, read in the epsg file that comes with proj.4, and parse it for the +units parameter or the +to_meters parameter.

If, however, you're going to get unprojected data, that is in a geographic CS, you're going to have to learn the ins and outs of datum shifts and projections. I'd be inclined to download the source for proj.4 and study it carefully, then find a couple of good books on the mathematics of map projections. If you want to cover every coordinate system, you're going to have to essentially rewrite significant chunks of proj.4.

Because you don't know where in the world your map is likely to be, the basic steps would be to shift the datum to a common standard such as WGS84, then I'd determine which UTM zone your rectangle is in and project it to that zone. From there you can calculate your random box, not forgetting that a rectangular region in a geographic CS is unlikely to be rectangular after it's projected, so you'll have to account for a skewed, warped, and curved initial bounding box (or if you're using UTM and your data is near the poles, you're going to have to choose another projection) to make sure your random box doesn't extend outside it.

Then if you want to get the extents of your random box back into the original CS, you'll need to reverse the process, which of course means it almost certainly won't be quite rectangular, although 100m isn't too big a deal.

  • I see. The use of the bbox is for a Nagios plugin that monitors WMS services (github.com/skipperkongen/GeoNagios). Precision is not a big deal. Basically I want a rough 100x100m to be more gentle to the WMS service I'm monitoring. Thank you for your answer. Mar 3, 2012 at 14:22

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.