Existing code to subdivide an envelope?

classic Classic list List threaded Threaded
9 messages Options
Reply | Threaded
Open this post in threaded view
|  
Report Content as Inappropriate

Existing code to subdivide an envelope?

Sunburned Surveyor
Does anyone know of (or have) code that will take an envelope and
break it up into smaller envelopes of equal size? (I didn't see a
method in the Envelope class that performed this operation.)

Here is my use case:

I've got some shapefiles that are too large to work with in OpenJUMP.
I want to split one of these large shapefiles into a smaller shapefile
that I can work with. To do this I thought I could access the envelope
that covers all features in the Shapefile. I'd split this into a
number of smaller envelopes of equal size. I'll then use code from
deegree/Geotools to iteratively access each feature in the Shapefile
on disk. I'll determine which envelope each feature fits in, and I'll
stick that feature in a new Shapefile.

Thanks for any thoughts. I'll whip up some new code if no one has
existing code to do the trick.

The Sunburned Surveyor
_______________________________________________
jts-devel mailing list
[hidden email]
http://lists.refractions.net/mailman/listinfo/jts-devel
Reply | Threaded
Open this post in threaded view
|  
Report Content as Inappropriate

Re: Existing code to subdivide an envelope?

mbedward
Don't know if this is useful, but here is a nice little paper about
square-ish partitioning.
http://www.win.tue.nl/~vanwijk/stm.pdf

Although the applications are complex the algorithm is very simple.

Michael
_______________________________________________
jts-devel mailing list
[hidden email]
http://lists.refractions.net/mailman/listinfo/jts-devel
Reply | Threaded
Open this post in threaded view
|  
Report Content as Inappropriate

Re: Existing code to subdivide an envelope?

Martin Davis
In reply to this post by Sunburned Surveyor
It sounds like you want to create a grid of specified extent and size (=
# of horizontal and vertical cells).  It's pretty straightforward to
code this up as a nested loop which creates Envelopes.

I don't know of any Java code that's packaged up and ready to use.    
You can do this fairly easily with JEQL, however.

Sunburned Surveyor wrote:

> Does anyone know of (or have) code that will take an envelope and
> break it up into smaller envelopes of equal size? (I didn't see a
> method in the Envelope class that performed this operation.)
>
> Here is my use case:
>
> I've got some shapefiles that are too large to work with in OpenJUMP.
> I want to split one of these large shapefiles into a smaller shapefile
> that I can work with. To do this I thought I could access the envelope
> that covers all features in the Shapefile. I'd split this into a
> number of smaller envelopes of equal size. I'll then use code from
> deegree/Geotools to iteratively access each feature in the Shapefile
> on disk. I'll determine which envelope each feature fits in, and I'll
> stick that feature in a new Shapefile.
>
> Thanks for any thoughts. I'll whip up some new code if no one has
> existing code to do the trick.
>
> The Sunburned Surveyor
> _______________________________________________
> jts-devel mailing list
> [hidden email]
> http://lists.refractions.net/mailman/listinfo/jts-devel
>
>  

--
Martin Davis
Senior Technical Architect
Refractions Research, Inc.
(250) 383-3022

_______________________________________________
jts-devel mailing list
[hidden email]
http://lists.refractions.net/mailman/listinfo/jts-devel
Reply | Threaded
Open this post in threaded view
|  
Report Content as Inappropriate

Re: Existing code to subdivide an envelope?

Sunburned Surveyor
Thanks MD.

I don't know when I'll get to this, but when I get it done I'll make
the code available to others. Would you be willing to add a method to
the Envelope class whose signature looks something like this?:

public List<Envelope> subdivideEnvelope(int rows, int columns)

If you don't think this is a good addition, then I'll park it in my
own utility class.

SS

On Tue, Jan 27, 2009 at 5:21 PM, Martin Davis <[hidden email]> wrote:

> It sounds like you want to create a grid of specified extent and size (= #
> of horizontal and vertical cells).  It's pretty straightforward to code this
> up as a nested loop which creates Envelopes.
>
> I don't know of any Java code that's packaged up and ready to use.    You
> can do this fairly easily with JEQL, however.
>
> Sunburned Surveyor wrote:
>>
>> Does anyone know of (or have) code that will take an envelope and
>> break it up into smaller envelopes of equal size? (I didn't see a
>> method in the Envelope class that performed this operation.)
>>
>> Here is my use case:
>>
>> I've got some shapefiles that are too large to work with in OpenJUMP.
>> I want to split one of these large shapefiles into a smaller shapefile
>> that I can work with. To do this I thought I could access the envelope
>> that covers all features in the Shapefile. I'd split this into a
>> number of smaller envelopes of equal size. I'll then use code from
>> deegree/Geotools to iteratively access each feature in the Shapefile
>> on disk. I'll determine which envelope each feature fits in, and I'll
>> stick that feature in a new Shapefile.
>>
>> Thanks for any thoughts. I'll whip up some new code if no one has
>> existing code to do the trick.
>>
>> The Sunburned Surveyor
>> _______________________________________________
>> jts-devel mailing list
>> [hidden email]
>> http://lists.refractions.net/mailman/listinfo/jts-devel
>>
>>
>
> --
> Martin Davis
> Senior Technical Architect
> Refractions Research, Inc.
> (250) 383-3022
>
> _______________________________________________
> jts-devel mailing list
> [hidden email]
> http://lists.refractions.net/mailman/listinfo/jts-devel
>
_______________________________________________
jts-devel mailing list
[hidden email]
http://lists.refractions.net/mailman/listinfo/jts-devel
Reply | Threaded
Open this post in threaded view
|  
Report Content as Inappropriate

Re: Existing code to subdivide an envelope?

mbedward
In reply to this post by Martin Davis
2009/1/28 Martin Davis <[hidden email]>:
> It sounds like you want to create a grid of specified extent and size (= #
> of horizontal and vertical cells).  It's pretty straightforward to code this
> up as a nested loop which creates Envelopes.

That reminded me of something in my code cellar...

SS: if you hack out the Geotools bits from this, add a line or two to
process your envelope, and it might do what you want...

Feel free to do whatever with it.

Michael


    public FeatureCollection makeRegularLattice(
            Rectangle2D bounds, double cellWidth,
CoordinateReferenceSystem crs) {

        int rows = (int)Math.round((bounds.getHeight()) / cellWidth);
        int cols = (int)Math.round((bounds.getWidth()) / cellWidth);

        double[] xvertices = new double[cols+1];
        for (int i = 0; i <= cols; i++) {
            xvertices[i] = bounds.getMinX() + i*cellWidth;
        }

        double[] yvertices = new double[rows+1];
        for (int i = 0; i < rows; i++) {
            yvertices[i] = bounds.getMinY() + i*cellWidth;
        }

        SimpleFeatureTypeBuilder typeBuilder = new SimpleFeatureTypeBuilder();
        typeBuilder.setName("lattice");

        typeBuilder.setCRS(crs);
        typeBuilder.add("shape", Polygon.class,
(CoordinateReferenceSystem) null);
        typeBuilder.add("id", Integer.class);
        typeBuilder.add("somevalue", Double.class);
        SimpleFeatureType type = typeBuilder.buildFeatureType();

        SimpleFeatureBuilder builder = new SimpleFeatureBuilder(type);
        GeometryFactory gf = new GeometryFactory();

        Coordinate[] cellVertices = new Coordinate[5];
        int[] xind = {0, 1, 1, 0};
        int[] yind = {0, 0, 1, 1};

        FeatureCollection features = FeatureCollections.newCollection();

        int id = 1;
        double valuePlaceHolder = 0;
        for (int i = 0; i < rows; i++) {
            for (int j = 0; j < cols; j++) {
                for (int v = 0; v < 4; v++) {
                    cellVertices[v] = new Coordinate(xvertices[j +
xind[v]], yvertices[i + yind[v]]);
                }
                cellVertices[4] = new Coordinate(cellVertices[0]);

                Polygon poly =
gf.createPolygon(gf.createLinearRing(cellVertices), null);

                builder.add(poly);
                builder.add(id++);
                builder.add(valuePlaceHolder);

                features.add(builder.buildFeature(null));
            }
        }

        return features;
    }
_______________________________________________
jts-devel mailing list
[hidden email]
http://lists.refractions.net/mailman/listinfo/jts-devel
Reply | Threaded
Open this post in threaded view
|  
Report Content as Inappropriate

Re: Existing code to subdivide an envelope?

jericks
In reply to this post by Sunburned Surveyor
Here is some code that I use to subdivide envelopes.

  /**
     * Create a grid of Envelopes for an Envelope with a number of rows and columns
     * @param envelope The Envelope
     * @param numberOfRows The number of rows
     * @param numberOfColumns The number of columns
     * @return An array of Envelopes
     */
    public Envelope[] createGrid(Envelope envelope, int numberOfRows, int numberOfColumns) {

        // Extract the origin, width, and height
        double minX = envelope.getMinX();
        double minY = envelope.getMinY();
        double width = envelope.getWidth();
        double height = envelope.getHeight();

        // Calculate the cell width and height
        double cellWidth = width / numberOfColumns;
        double cellHeight = height / numberOfRows;

        // Calculate the number of cells (Envelopes)
        int numberOfCells = numberOfRows * numberOfColumns;

        // Create the Envelope array
        Envelope[] envs = new Envelope[numberOfCells];

        // Fill the Envelope array
        int i = 0;
        for (int r = 0; r < numberOfRows; r++) {
            for (int c = 0; c < numberOfColumns; c++) {
                double x = minX + (r * cellWidth);
                double y = minY + (c * cellHeight);
                envs[i] = new Envelope(x, x + cellWidth, y, y + cellHeight);
                i++;
            }
        }

        return envs;
    }

Jared Erickson

>>> Sunburned Surveyor <[hidden email]> 1/27/2009 4:49 PM >>>
Does anyone know of (or have) code that will take an envelope and
break it up into smaller envelopes of equal size? (I didn't see a
method in the Envelope class that performed this operation.)

Here is my use case:

I've got some shapefiles that are too large to work with in OpenJUMP.
I want to split one of these large shapefiles into a smaller shapefile
that I can work with. To do this I thought I could access the envelope
that covers all features in the Shapefile. I'd split this into a
number of smaller envelopes of equal size. I'll then use code from
deegree/Geotools to iteratively access each feature in the Shapefile
on disk. I'll determine which envelope each feature fits in, and I'll
stick that feature in a new Shapefile.

Thanks for any thoughts. I'll whip up some new code if no one has
existing code to do the trick.

The Sunburned Surveyor
_______________________________________________
jts-devel mailing list
[hidden email]
http://lists.refractions.net/mailman/listinfo/jts-devel

_______________________________________________
jts-devel mailing list
[hidden email]
http://lists.refractions.net/mailman/listinfo/jts-devel
Reply | Threaded
Open this post in threaded view
|  
Report Content as Inappropriate

Re: Existing code to subdivide an envelope?

Sunburned Surveyor
Thanks Jared and Michael for your sample code. I'll make use of it.

The Sunburned Surveyor

On Wed, Jan 28, 2009 at 7:12 AM, Jared Erickson <[hidden email]> wrote:

> Here is some code that I use to subdivide envelopes.
>
>  /**
>     * Create a grid of Envelopes for an Envelope with a number of rows and columns
>     * @param envelope The Envelope
>     * @param numberOfRows The number of rows
>     * @param numberOfColumns The number of columns
>     * @return An array of Envelopes
>     */
>    public Envelope[] createGrid(Envelope envelope, int numberOfRows, int numberOfColumns) {
>
>        // Extract the origin, width, and height
>        double minX = envelope.getMinX();
>        double minY = envelope.getMinY();
>        double width = envelope.getWidth();
>        double height = envelope.getHeight();
>
>        // Calculate the cell width and height
>        double cellWidth = width / numberOfColumns;
>        double cellHeight = height / numberOfRows;
>
>        // Calculate the number of cells (Envelopes)
>        int numberOfCells = numberOfRows * numberOfColumns;
>
>        // Create the Envelope array
>        Envelope[] envs = new Envelope[numberOfCells];
>
>        // Fill the Envelope array
>        int i = 0;
>        for (int r = 0; r < numberOfRows; r++) {
>            for (int c = 0; c < numberOfColumns; c++) {
>                double x = minX + (r * cellWidth);
>                double y = minY + (c * cellHeight);
>                envs[i] = new Envelope(x, x + cellWidth, y, y + cellHeight);
>                i++;
>            }
>        }
>
>        return envs;
>    }
>
> Jared Erickson
>
>>>> Sunburned Surveyor <[hidden email]> 1/27/2009 4:49 PM >>>
> Does anyone know of (or have) code that will take an envelope and
> break it up into smaller envelopes of equal size? (I didn't see a
> method in the Envelope class that performed this operation.)
>
> Here is my use case:
>
> I've got some shapefiles that are too large to work with in OpenJUMP.
> I want to split one of these large shapefiles into a smaller shapefile
> that I can work with. To do this I thought I could access the envelope
> that covers all features in the Shapefile. I'd split this into a
> number of smaller envelopes of equal size. I'll then use code from
> deegree/Geotools to iteratively access each feature in the Shapefile
> on disk. I'll determine which envelope each feature fits in, and I'll
> stick that feature in a new Shapefile.
>
> Thanks for any thoughts. I'll whip up some new code if no one has
> existing code to do the trick.
>
> The Sunburned Surveyor
> _______________________________________________
> jts-devel mailing list
> [hidden email]
> http://lists.refractions.net/mailman/listinfo/jts-devel
>
> _______________________________________________
> jts-devel mailing list
> [hidden email]
> http://lists.refractions.net/mailman/listinfo/jts-devel
>
_______________________________________________
jts-devel mailing list
[hidden email]
http://lists.refractions.net/mailman/listinfo/jts-devel
Reply | Threaded
Open this post in threaded view
|  
Report Content as Inappropriate

Re: Existing code to subdivide an envelope?

Martin Davis
In reply to this post by Sunburned Surveyor
Hmmm... Not sure I like the idea of this going into Envelope, when it
could easily be placed in an external utility class.  I think it's a
good idea to keep the signature of Envelope as narrow as possible, to
reduce the possible of awkward dependencies going forward.

Sunburned Surveyor wrote:

> Thanks MD.
>
> I don't know when I'll get to this, but when I get it done I'll make
> the code available to others. Would you be willing to add a method to
> the Envelope class whose signature looks something like this?:
>
> public List<Envelope> subdivideEnvelope(int rows, int columns)
>
> If you don't think this is a good addition, then I'll park it in my
> own utility class.
>
> SS
>
> On Tue, Jan 27, 2009 at 5:21 PM, Martin Davis <[hidden email]> wrote:
>  
>> It sounds like you want to create a grid of specified extent and size (= #
>> of horizontal and vertical cells).  It's pretty straightforward to code this
>> up as a nested loop which creates Envelopes.
>>
>> I don't know of any Java code that's packaged up and ready to use.    You
>> can do this fairly easily with JEQL, however.
>>
>> Sunburned Surveyor wrote:
>>    
>>> Does anyone know of (or have) code that will take an envelope and
>>> break it up into smaller envelopes of equal size? (I didn't see a
>>> method in the Envelope class that performed this operation.)
>>>
>>> Here is my use case:
>>>
>>> I've got some shapefiles that are too large to work with in OpenJUMP.
>>> I want to split one of these large shapefiles into a smaller shapefile
>>> that I can work with. To do this I thought I could access the envelope
>>> that covers all features in the Shapefile. I'd split this into a
>>> number of smaller envelopes of equal size. I'll then use code from
>>> deegree/Geotools to iteratively access each feature in the Shapefile
>>> on disk. I'll determine which envelope each feature fits in, and I'll
>>> stick that feature in a new Shapefile.
>>>
>>> Thanks for any thoughts. I'll whip up some new code if no one has
>>> existing code to do the trick.
>>>
>>> The Sunburned Surveyor
>>> _______________________________________________
>>> jts-devel mailing list
>>> [hidden email]
>>> http://lists.refractions.net/mailman/listinfo/jts-devel
>>>
>>>
>>>      
>> --
>> Martin Davis
>> Senior Technical Architect
>> Refractions Research, Inc.
>> (250) 383-3022
>>
>> _______________________________________________
>> jts-devel mailing list
>> [hidden email]
>> http://lists.refractions.net/mailman/listinfo/jts-devel
>>
>>    
> _______________________________________________
> jts-devel mailing list
> [hidden email]
> http://lists.refractions.net/mailman/listinfo/jts-devel
>
>  

--
Martin Davis
Senior Technical Architect
Refractions Research, Inc.
(250) 383-3022

_______________________________________________
jts-devel mailing list
[hidden email]
http://lists.refractions.net/mailman/listinfo/jts-devel
Reply | Threaded
Open this post in threaded view
|  
Report Content as Inappropriate

Re: Existing code to subdivide an envelope?

mbedward
2009/1/29 Martin Davis <[hidden email]>:
> Hmmm... Not sure I like the idea of this going into Envelope, when it could
> easily be placed in an external utility class.  I think it's a good idea to
> keep the signature of Envelope as narrow as possible, to reduce the possible
> of awkward dependencies going forward.
>

I'd vote for that approach too.

Michael
_______________________________________________
jts-devel mailing list
[hidden email]
http://lists.refractions.net/mailman/listinfo/jts-devel
Loading...