Using a polygon to cut into another polygon

169 views
Skip to first unread message

Bob

unread,
Mar 29, 2010, 6:27:13 PM3/29/10
to openjump-users
Hello all,

I have a shapefile which seems to have a small polygon digitized on
top of a larger polygon. I would like to cut a hole in the larger
polygon the same shape (and location) as the smaller polygon. How can
I do that? Do I use the "Create Cookie Cut" tool? If so, where can I
find documentation on this tool?

Thanks,
Bob

Stefan Steiniger

unread,
Mar 29, 2010, 8:37:51 PM3/29/10
to openjum...@googlegroups.com
Hei Bob,

the cookie cut tool works the way that you select a polygon and then
draw the cookie-cut region. So you can not use it with an existing
polygon, except you would put the existing small polygon in a different
layer and digitize it vertex by vertex.

on option to create cookie cuts for a whole layer is to use: Tools>Edit
Geometry>Convert>Planar Graph
and then restrict the output to faces with transfered attributes. If you
would have the small polygon in a different layer you could also use
Tools>Analysis>Two Layers>Intersect Polygon Layers

stefan

> To unsubscribe from this group, send email to openjump-users+unsubscribegooglegroups.com or reply to this email with the words "REMOVE ME" as the subject.
>
>

Jukka Rahkonen

unread,
Mar 30, 2010, 12:24:52 AM3/30/10
to openjum...@googlegroups.com
Hi,

Could it be possible to show some magical Java beans code that drives the
coocie cutter on the selected polygon and takes the cutting polygon as
WKT? It could be useful sometimes.

-Jukka Rahkonen-

Michaël Michaud

unread,
Apr 1, 2010, 3:10:20 AM4/1/10
to openjum...@googlegroups.com
Hi,

I was just on the way of achieving a script to process a whole layer of
plain polygons in order to "recognize" polygons representing holes and
to substract those polygons from others.
I attach the script so that you can examine and test it.
Be careful, it will modify data without any way to cancel.
A possible evolution would be to work on a selection instead of the
whole layer.
You can use it from the script editor or as a beantool

Any feedback is welcome

Micha�l

Bob a �crit :

CreateHoles.bsh

Sunburned Surveyor

unread,
Apr 6, 2010, 4:28:36 PM4/6/10
to openjum...@googlegroups.com
Thanks for the script Michael.

The Sunburned Surveyor

2010/4/1 Michaël Michaud <michael...@free.fr>:


> Hi,
>
> I was just on the way of achieving a script to process a whole layer of
> plain polygons in order to "recognize" polygons representing holes and to
> substract those polygons from others.
> I attach the script so that you can examine and test it.
> Be careful, it will modify data without any way to cancel.
> A possible evolution would be to work on a selection instead of the whole
> layer.
> You can use it from the script editor or as a beantool
>
> Any feedback is welcome
>

> Michaël
>
> Bob a écrit :


>>
>> Hello all,
>>
>> I have a shapefile which seems to have a small polygon digitized on
>> top of a larger polygon.  I would like to cut a hole in the larger
>> polygon the same shape (and location) as the smaller polygon.  How can
>> I do that?  Do I use the "Create Cookie Cut" tool?  If so, where can I
>> find documentation on this tool?
>>
>> Thanks,
>> Bob
>>
>> To unsubscribe from this group, send email to
>> openjump-users+unsubscribegooglegroups.com or reply to this email with the
>> words "REMOVE ME" as the subject.
>>
>>
>>
>
>

> // Process a Polygonal layer made of simple Polygons, and create holes
> // in larger polygons using inner Polygons.
> // The rules used to determine if a Polygon represents a Shell or a Hole are
> as
> // follows :
> // - A Polygon A which is not contained in the Exterior Envelope of any
> other
> // Polygon is a Shell
> // - A Polygon A which is strictly included in a Shell B is a Hole of B,
> except
> // if there is another Polygon C included in the Shell B and including A
> // - A Polygon A included in a "Hole" of Polygon B (A is strictly included
> in
> // the Exterior Envelope of Polygon B but not in the Area of Polygon B) is
> // considered as a Hole of Polygon B if it "fill" the hole (this last test
> is
> // just done by comparing lengths of A.intersection(B) and A exterior Ring.
> // Michael Michaud 2010-03-30
>
> import com.vividsolutions.jump.feature.IndexedFeatureCollection;
> import com.vividsolutions.jts.index.strtree.STRtree;
> import com.vividsolutions.jump.workbench.ui.OKCancelDialog;
>
> /**
>  * Determine if g1 can be considered as a hole of g2.
>  */
> boolean isIn(Geometry g1, Geometry g2) {
>  if (!(g1 instanceof Polygon)) return false;
>  if (!(g2 instanceof Polygon)) return false;
>  if (!g1.envelopeInternal.intersects(g2.envelopeInternal)) return false;
>  if (g1.envelopeInternal.contains(g2.envelopeInternal)) return false;
>  // g2 envelope contains g1 envelope
>  if (g2.envelopeInternal.contains(g1.envelopeInternal) &&
>    // g1 "fill" a hole of g2
>    Math.abs(1.0 -
> g1.intersection(g2).getLength()/g1.exteriorRing.getLength()) < 0.01 &&
>    // g1 is "strictly" contained in g2
>    g1.relate(g2.factory.createPolygon(g2.exteriorRing, new LinearRing[0]),
> "TFFTFF***")) {
>      return true;
>  }
>  return false;
> }
>
> /**
>  * Main loop over pairs of Features
>  */
> List processFeatures(FeatureCollection fc) {
>  // A map containing all the Polygons "enclosing" a given key-Polygon
>  enclosing = new HashMap();
>  // A map containing all the Polygons strictly included in a given
> key-Polygon
>  enclosed = new HashMap();
>  // For each feature
>  int count = 0;
>  for (f1: fc.features) {
>    // Ajoute une liste des contours circonscrits
>    //enclosing.put(f1, new ArrayList());
>    if (count++%100==99) print("   phase 1 : " + count + " objets traités");
>    for (f2 : fc.query(f1.geometry.envelopeInternal)) {
>      if (f1!=f2 && isIn(f1.geometry, f2.geometry)) {
>        outers = enclosing.get(f1);
>        outers = outers==null?new ArrayList():outers;
>        outers.add(f2);
>        enclosing.put(f1, outers);
>        inners = enclosed.get(f2);
>        inners = inners==null?new ArrayList():inners;
>        inners.add(f1);
>        enclosed.put(f2, inners);
>      }
>    }
>  }
>  print("   phase 1 : " + count + " objets traités");
>  count = 0;
>  holes = new ArrayList();
>  for (f1 : fc.features) {
>    count++;
>    inners = enclosed.get(f1);
>    if (inners == null) continue;
>    for (f2 : inners) {
>      enclosingf1 = enclosing.get(f1);
>      nbenclosingf1 = enclosingf1 == null ? 0 : enclosingf1.size();
>      enclosingf2 = enclosing.get(f2);
>      if (enclosingf2 != null &&
>          enclosingf2.size() == nbenclosingf1+1
>          && nbenclosingf1%2==0) {
>        f1.setGeometry(f1.geometry.difference(f2.geometry));
>        holes.add(f2);
>      }
>    }
>  }
>  print("   phase 2 : " + count + " objets traités");
>  return holes;
> }
>
> class Main extends Thread {
>    // Sélection des layers
>    layers = wc.layerNamePanel.selectedLayers;
>    public void run() {
>        dialog = new OKCancelDialog(wc.workbench.frame, "Create holes", true,
> new JLabel("<html>This action will try to create holes <br>in all the
> selected layers.<br>Do you want to continue ?</html>"), null);
>        dialog.show();
>        if (dialog.wasOKPressed()) {
>            for (layer : layers) {
>                print("Traitement de la couche \"" + layer.name + "\"");
>                holes = processFeatures(new
> IndexedFeatureCollection(layer.featureCollectionWrapper, new STRtree()));
>                layer.featureCollectionWrapper.removeAll(holes);
>                wc.layerViewPanel.repaint();
>            }
>          print("Traitement terminé");
>        }
>    }
> }
> new Main().start();
>
>
> /* Test case
> POLYGON ((829.7 541.9, 829.7 542.3, 832.5 542.3, 832.5 541.9, 829.7 541.9))
>
> POLYGON ((829.5 542.6, 829.5 542.9, 829.9 542.9, 829.9 542.6, 829.5 542.6))
>
> POLYGON ((831.4 541.3, 831.4 541.6, 832.5 541.6, 832.5 541.3, 831.4 541.3))
>
> POLYGON ((831.7 541.4, 831.7 541.5, 832.2 541.5, 832.2 541.4, 831.7 541.4))
>
> POLYGON ((829 539.5, 829 542.3, 829.7 542.3, 829.7 539.5, 829 539.5),
>  (829.2 539.8, 829.5 539.8, 829.5 542, 829.2 542, 829.2 539.8))
>
> POLYGON ((830.3 541.6, 830.3 541.9, 832.5 541.9, 832.5 541.6, 830.3 541.6))
>
> POLYGON ((830.9 540.5, 830.9 540.6, 831 540.6, 831 540.5, 830.9 540.5))
>
> POLYGON ((829 542.3, 829 543.2, 832.5 543.2, 832.5 542.3, 829 542.3),
>  (829.2 542.4, 830.4 542.4, 830.4 543.1, 829.2 543.1, 829.2 542.4))
>
> POLYGON ((829.9 540.2, 829.9 541.3, 830.2 541.3, 830.2 540.2, 829.9 540.2))
>
> POLYGON ((830.3 541.3, 830.3 541.6, 831.4 541.6, 831.4 541.3, 830.3 541.3))
>
> POLYGON ((829.3 540.1, 829.3 540.7, 829.4 540.7, 829.4 540.1, 829.3 540.1))
>
> POLYGON ((830.6 540.1, 830.6 541, 831.5 541, 831.5 540.1, 830.6 540.1))
>
> POLYGON ((830.8 540.4, 830.8 540.8, 831.2 540.8, 831.2 540.4, 830.8 540.4))
>
> POLYGON ((829.7 539.5, 829.7 541.9, 830.3 541.9, 830.3 539.5, 829.7 539.5))
>
> POLYGON ((829.3 541.1, 829.3 541.8, 829.4 541.8, 829.4 541.1, 829.3 541.1))
>
> POLYGON ((829.2 539.8, 829.2 542, 829.5 542, 829.5 539.8, 829.2 539.8),
>  (829.3 541.1, 829.4 541.1, 829.4 541.8, 829.3 541.8, 829.3 541.1))
>
> POLYGON ((830.3 539.5, 830.3 541.3, 832.5 541.3, 832.5 539.5, 830.3 539.5))
> */
>
>

Jukka Rahkonen

unread,
Apr 19, 2010, 4:24:51 AM4/19/10
to openjum...@googlegroups.com
Hi,

Has anybody made a tool for creating automatically a grid as a line or
polygon layer? It might be used somehow like

- select the origin
- select grid y-size in map units
- select grid x-size in map units
- select the width of the grid as mesh count
- select the height of the grid as mesh count
- select if the grid will be a line or polygon layer

If the result is a polygon layer it migh also contain attribute fields
"x-index" and "y-indes" with automatically filled values.

-Jukka Rahkonen-




--
Subscription settings: http://groups.google.com/group/openjump-users/subscribe?hl=en

Michaël Michaud

unread,
Apr 19, 2010, 5:09:14 AM4/19/10
to openjum...@googlegroups.com
Hi Jukka,

Here is the beanshell-script, polygon version of what you want.
Options are a bit different with what you suggest.
There is no option for extent. It takes the maximum extent of all layers
Can be put in beantools directory.
It can easily be renamed/translated or modified to take only one
selected layer as extent.

Would not be too difficult to transform into a plugin. Needs an
agreement about what options are most useful.
Could be nice to write a feature request to remember.

Micha�l


Jukka Rahkonen a �crit :
CreerGrille.bsh

Jukka Rahkonen

unread,
Apr 19, 2010, 5:57:20 AM4/19/10
to openjum...@googlegroups.com
Hi Micha�l,

Pretty nice. I volunteer to make a feature request but I will wait till
tomorrow if somebody else has opinions about the options.

It seems that OpenJUMP can be made to do very useful things with scripting.

-Jukka-

Stefan Steiniger

unread,
Apr 19, 2010, 12:23:09 PM4/19/10
to openjum...@googlegroups.com
Hei Jukka,

there is a grid toolbox from Pirol that allows to create grids. It is
pretty advanced (I think)
I plan already for some time now to integrate it - now that the Pirol
project basically finished, but never found the time.

I'll attach the library files.

stefan
pgridTools.jar
pbaseClasses.jar

Martin Weis

unread,
Apr 19, 2010, 3:15:03 PM4/19/10
to openjum...@googlegroups.com
Hi!

Jukka Rahkonen a écrit :
>>>> Has anybody made a tool for creating automatically a grid as a line or
>>>> polygon layer? It might be used somehow like
>>>>
>>>> - select the origin
>>>> - select grid y-size in map units
>>>> - select grid x-size in map units
>>>> - select the width of the grid as mesh count
>>>> - select the height of the grid as mesh count

Geostaf Graticule Creator plugin does this:
http://digilander.libero.it/valruggero/download.html

>>>> - select if the grid will be a line or polygon layer

Maybe you can convert it to lines afterwards (not in the tool yet)?

Greetings,
Martin

Jukka Rahkonen

unread,
Apr 20, 2010, 3:08:07 AM4/20/10
to openjum...@googlegroups.com
Martin wrote:

> Hi!
>
> Jukka Rahkonen a écrit :
>>>>> Has anybody made a tool for creating automatically a grid as a line
>>>>> or
>>>>> polygon layer? It might be used somehow like
>>>>>
>>>>> - select the origin
>>>>> - select grid y-size in map units
>>>>> - select grid x-size in map units
>>>>> - select the width of the grid as mesh count
>>>>> - select the height of the grid as mesh count
>
> Geostaf Graticule Creator plugin does this:
> http://digilander.libero.it/valruggero/download.html

You are right, it works almost exactly as I was sketching. A couple of
changes might be considered. It might be better to build a separate
cell-ID for x and y directions, and the lower left corner of active map
could be used as a default origin, or then the user might have a
possibility to click on a map and show it.

>>>>> - select if the grid will be a line or polygon layer
>
> Maybe you can convert it to lines afterwards (not in the tool yet)?

I do not know if it is actually so important feature. Myself I needed
regular sized polygons for splitting a large area to better manageable
pieces. Another use case could be to split a project to mapsheets for
printing. I will make a feature request about adding this or similar tool
into OpenJUMP core. There is a good place for it in the menu in Tools -
Generate.

-Jukka-

Nacho Uve

unread,
Apr 20, 2010, 4:50:14 AM4/20/10
to openjum...@googlegroups.com
Hi,

You can have a look to SEXTANTE->Vector Tools -> graticulebuilder [1]

Regards,
Nacho

[1]: http://forge.osor.eu/plugins/scmsvn/viewcvs.php/trunk/soft/sextante_lib/vectorTools/src/es/unex/sextante/vectorTools/graticuleBuilder/GraticuleBuilderAlgorithm.java?root=sextante&view=markup

2010/4/19 Jukka Rahkonen <jukka.r...@latuviitta.fi>



--
Juan Ignacio Varela García (Nacho Uve)
Coordinador Grupo de Desarrollo
Cartolab - Laboratorio de Ingeniería Cartográfica
http://www.cartolab.es

ETS Ingeniería de Caminos, Canales y Puertos
Universidade da Coruña
Campus de Elviña - 15071 A Coruña (España)
(34)981167000 ext. 5493

Jukka Rahkonen

unread,
Apr 20, 2010, 5:20:07 AM4/20/10
to openjum...@googlegroups.com
Hi,

I tested this one also. Yes, it is advanced. It took also some time with
try and error method before I learned how to use it. However, even this
tool creates easily even rotated grids it does not allow to set the x and
y dimensions for the mesh.

There is now three solutions for defining the total size of the grid:
- Micha�l: maximum extent of all layers (or one selected layer)
- GeoSTAF: origin + (grid count * grid dimensions)
- Pirol: selected feature

It is hard to say which grid tool I would select if I had to take just
one. GeoSTAF plugin and the script by Micha�l gives a possiblity to
separate mesh size for x and y directions which is a good feature and not
included in Pirol tools. What is best in the Pirol grid tools is the
possiblity to select a feature and cover it with a grid and widen it with
a given tolerance. Perfect for creating mapsheets or just a seach index
grid covering some city area, for example. And none of the tools create
separate x and y index columns which would help in labeling for example
the city search index. Perhaps none of these tools is ready to be
integrated into OJ without some modification.

-Jukka-
Reply all
Reply to author
Forward
0 new messages