<div>Hi,</div>
<div>I also saw many times this kind of posts, and I think your&nbsp;comments of today are for the first time&nbsp;a positive contribution. As a matter of fact I&nbsp;never thought a raster in the way you describe here, but now I discover that in the past I would love to manage my&nbsp;scanned maps in this way!!. Also I recall &nbsp;that I used to get the number 2 point form your post but using ECW (Encapsulated Compressed Wavelet)&nbsp; and an API provided by ERMapper (I think it was free). Now I have to check but maybe ERMapper also have something for JPG2000. Is it bad idea to use ECW format?.
</div>
<div><br>Mario Servin<br>&nbsp;</div>
<div><span class="gmail_quote">On 10/24/06, <b class="gmail_sendername">Marshall, Steve</b> &lt;<a href="mailto:smarshall@wsi.com">smarshall@wsi.com</a>&gt; wrote:</span>
<blockquote class="gmail_quote" style="PADDING-LEFT: 1ex; MARGIN: 0px 0px 0px 0.8ex; BORDER-LEFT: #ccc 1px solid">
<div>
<div><font face="Arial" size="2"><span>Over the past several months, I've seen a number of postings regarding support in PostGIS for raster data.&nbsp; The email threads tend to look like this:</span></font></div>
<div><font face="Arial" size="2"><span>---</span></font></div>
<div><font face="Arial" size="2"><span>Poster: </span></font></div>
<div><font face="Arial" size="2"><span>I'd like to have support for raster data in PostGIS.</span></font></div>
<div><font face="Arial" size="2"><span></span></font>&nbsp;</div>
<div><font face="Arial" size="2"><span>Responder:&nbsp;&nbsp;&nbsp;</span></font></div>
<div><font face="Arial" size="2"><span>If you are just going to put the whole image in the database, then take the complete image back out, what's the point.&nbsp; Why do you think storing in the RDB would be better than managing raster data in files?
</span></font></div>
<div><font face="Arial" size="2"><span></span></font>&nbsp;</div>
<div><font face="Arial" size="2"><span>Poster:&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; </span></font></div>
<div><font face="Arial" size="2"><span>I would not have to manage the interactions between the RDB and a file-based database, the images would follow transactional semantics, and the raster data would be network accessible.&nbsp; Additionally, I could use PostGIS spatial queries&nbsp;to relate&nbsp;vector data to&nbsp;raster data, and maybe even write some additional raster manipulation functions, all of which would be built into the database.
</span></font></div>
<div><font face="Arial" size="2"><span></span></font>&nbsp;</div>
<div><font face="Arial" size="2"><span>Responder:</span></font></div>
<div><font face="Arial" size="2"><span>That stuff sounds neat, but the performance issues are unworkable.&nbsp; Performance with large images will be terrible if we use anything other than flat files.</span></font></div>
<div><font face="Arial" size="2"><span>---</span></font></div>
<div><font face="Arial" size="2"><span>Generally, the debate ends here.&nbsp; This time I'd like to see if the conversation could go in another direction.</span></font></div>
<div><font face="Arial" size="2"><span></span></font>&nbsp;</div>
<div><font face="Arial" size="2"><span>I think the &quot;what's the point&quot; response is basically valid for a naive implementation of images within a RDB.&nbsp; If you are going to treat an image as a blob that can be extracted only in the same form that it was inserted, there is really not much point in using relational storage.&nbsp; The raster data will need to be read into program memory buffers in one big chunk.&nbsp; For large images, the data&nbsp;will likely need to be&nbsp;written into a temporary file so it can be incrementally processed using file seeks.&nbsp; In this case, you'd be better off having the data in a file in the first place.
</span></font></div>
<div><font face="Arial" size="2"><span></span></font>&nbsp;</div>
<div><font face="Arial" size="2"><span>However, if you support multiple modes of extraction, then the relational model really starts to become compelling.&nbsp; For example, I could see extracting an image that is a subsector of&nbsp;the complete image (
i.e. a smaller image that&nbsp;covers a&nbsp;smaller geographic region).&nbsp; One could also imagine extracting a low-resolution version of the image for &quot;zoomed-out&quot; display.&nbsp; Low-res images would cover the same geographic area, but with fewer pixels, so less processing for applications.&nbsp; By extracting image subsets, large image processing could be done straight from the database, without bloating application memory or using an&nbsp;intermediate temporary file.
</span></font></div>
<div><font face="Arial" size="2"><span></span></font>&nbsp;</div>
<div><font face="Arial" size="2"><span>The ability to do both resolution and sector types of subsetting&nbsp;are well-supported by the JPEG2000 image format.&nbsp; In fact, these types of operations were some of the drivers behind the JPEG2000 design.&nbsp; Given that JPEG2000 compresses well and is an open standard, it seems like a good format for storing&nbsp;raster data.&nbsp; So is there some way to store JPEG2000 data in an intelligent way within a relational database?
</span></font></div>
<div><font face="Arial" size="2"><span></span></font>&nbsp;</div>
<div><font face="Arial" size="2"><span>Essentially, I think you could store JPEG2000 in a database by defining a new data type (e.g. pgraster) that could hold the image data in JPEG2000 format.&nbsp; The data type might also populate some derived data when the object is written by interpreting&nbsp;the JPEG2000, such as a PostGIS GEOMETRY object to represent the bounding box of the raster.&nbsp; This derived data would allow more optimal responses to spatial queries, but overall would not be mandatory to the implementation.&nbsp; However, it&nbsp;would&nbsp;be mandatory&nbsp;to define a set of SQL functions that take a pgraster argument&nbsp;to allow one to extract different subsets of&nbsp;the JPEG2000&nbsp;data.&nbsp; 
</span></font></div>
<div><font face="Arial" size="2"><span></span></font>&nbsp;</div>
<div><font face="Arial" size="2"><span>In terms of implementation, I think&nbsp;the pgraster implementation&nbsp;would require the following:</span></font></div>
<div><font face="Arial" size="2"><span></span></font>&nbsp;</div>
<div><font face="Arial" size="2"><span>1. The underlying database storage would allow random access&nbsp;into the image data.</span></font></div>
<div><font face="Arial" size="2"><span>2. A fairly sophisticated JPEG2000 codec would need to be linked into the database server, so that different subsets of the data could be accessed.</span></font></div>
<div><font face="Arial" size="2"><span>3. The JPEG2000 codec would need to be integrated with the RDB storage so that one could use standard codec functions with a RDB storage model.</span></font></div>
<div><font face="Arial" size="2"><span></span></font>&nbsp;</div>
<div><font face="Arial" size="2"><span>I think #1 could be achieved by making the pgraster type &quot;toastable&quot;, and using a PostgreSQL TOAST table for the underlying storage.&nbsp; The data would not use TOAST compression, since the image should already be well-compressed.&nbsp; We could use the internal PostgreSQL function heap_tuple_untoast_attr_slice() to extract subsets of the toasted data, so we do not need to detoast the entire image during processing.&nbsp; Toast does not provide an API for a similar kind of seeking during writes, but I think it's the seeking on reads that will be the most significant to performance.
</span></font></div>
<div><font face="Arial" size="2"><span></span></font>&nbsp;</div>
<div><font face="Arial" size="2"><span>I think #2 is potentially more problematic.&nbsp; The publicly available JPEG2000 codecs do not seem to have the interfaces needed for extraction of parts of a JPEG2000 image.&nbsp; The JasPer library only provides the encode and decode functions that produce or accept a jas_image_t type; it doesn't have any of partial extraction capabilities.&nbsp; The Open JPEG codec is a bit tougher to get a handle on, but it also appears to only allow translation between an image type and&nbsp;a codestream.
</span></font></div>
<div><font face="Arial" size="2"><span></span></font>&nbsp;</div>
<div><font face="Arial" size="2"><span>The code that really seems well-adapted to this problem is the Kakadu package written by David Taubman, one of the originators of JPEG 2000.&nbsp; Unfortunately, this library is not open source.&nbsp; Kakadu has been included as an optional component of other open source projects, like GDAL.&nbsp; However, I think&nbsp;Kakadu's license would come into conflict with the GPL used by PostGIS.&nbsp;&nbsp; I'm not a lawyer, but I&nbsp;think this conflict&nbsp;could be overcome if PostGIS could be release under dual licenses, such as GPL or LGPL.&nbsp; Whether or not its desirable to include of Kakadu in a PostGIS extension is another question.
</span></font></div>
<div><font face="Arial" size="2"><span></span></font>&nbsp;</div>
<div><font face="Arial" size="2"><span>If anyone has knowledge of other JPEG 2000 codecs that have these low-level access capabilities, I'd be very happy to hear about them.&nbsp;&nbsp; Also, if I've mischaracterized any of the codecs, I'd love to be corrected.
</span></font></div>
<div><font face="Arial" size="2"><span></span></font>&nbsp;</div>
<div><font face="Arial" size="2"><span>In any event, I'm curious to see if there is significant interest in an implementation of JPEG2000 raster data type&nbsp;within PostGIS.&nbsp; If so, I think I could dedicate a significant amount of my time over the next several months, as well as perhaps some funding from my employer, depending upon whether some of the issues I raised above can be resolved.
</span></font></div>
<div><font face="Arial" size="2"><span></span></font>&nbsp;</div>
<div><font face="Arial" size="2"><span>Steve Marshall</span></font></div>
<div><font face="Arial" size="2"><span></span></font>&nbsp;</div></div><br>_______________________________________________<br>postgis-users mailing list<br><a onclick="return top.js.OpenExtLink(window,event,this)" href="mailto:postgis-users@postgis.refractions.net">
postgis-users@postgis.refractions.net</a><br><a onclick="return top.js.OpenExtLink(window,event,this)" href="http://postgis.refractions.net/mailman/listinfo/postgis-users" target="_blank">http://postgis.refractions.net/mailman/listinfo/postgis-users
</a><br><br><br></blockquote></div><br>