Inkscape.org
Beyond the Basics How to only keep only path intersections?
  1. #1
    Sledge92 Sledge92 @Sledge92

    Good morning, everyone.

    I've been using inkscape for .svg manipulation, in my side project.

    I was wondering if there would by anything in inkscape that would allow me to remove borders from an .svg path, keeping only what's inside.
    I need to be putting objects inside other objects, so I'm converting them to paths so I can be filling them, after I put them over a base, which I already have.
    How I'm achieving this is by getting all the intersections between the paths and after getting all these intersections , but I'm getting the nagging feeling that such work is unnecessary.
    What do you think? Do you think that there's an easier way to achieve this? Thank you for your replies and for your time!

    P.S. I'm attaching some files for you to understand what I mean.
    I want to go from file "borders" to "no_borders", to then be putting it on "base" and filling it to get all the areas.

    Base
    No Borders
    Borders
  2. #2
    Tyler Durden Tyler Durden @TylerDurden

    Something like this?

     

  3. #3
    brynn brynn @brynn

    Note that if you just want to fill the shapes with solid colors, you would not use clipping.  You would use the borders or outlines, so that you can see what you're doing.  Then when you have all the shapes filled, you can remove the stroke (the black outline).

    I wrote a tutorial that explains how to make the paths.  I'm not clear if you already have all the closed paths that you need.  But if you do, all you need to do is fill them with color.  Once they are filled with color, you can remove the stroke, the black border.  It's just easier to do (at least it is for me) with the black outline.  Then I would just remove them in the end.

    Let me know if you want to see the tutorial.  It's about making the paths.  (Geez, your map looks a lot like the map in my tutorial!  I think it might be the same country.)

  4. #4
    Sledge92 Sledge92 @Sledge92
    *

    Hey! First of all, thank you very much for the replies!

    @TylerDurden  What I actually want to do is to go from photo 3(the one with the perimeter) to photo 2(without perimeter) to be able to fit it in the base that I have and use the bucket to fill, because the thing is, the base and the objects that I already have have different edges, and I don't want to have to align the nodes manually. Basically, the border from "Base"(photo1) and "Borders" (photo3) is not the same, so I can't do what you showed me...
    Additionally, how did you put "No borders" (photo2) in "base"(photo1)? Is there some easier way than copy/paste + scale?

    @brynn I want to basically remove the outline of a path, so, going from photo3 -> photo2. (Interesting, I'm curious about the tutorial, to check if indeed the country is the same...)

  5. #5
    Tyler Durden Tyler Durden @TylerDurden
    *

    I just put the base in on layer1, imported the no borders file and scaled it. Anyplace that had a gap, I added a small segment so the regions appear closed.

    The two layers overlap, they only have to appear closed for the bucket tool to work.

  6. #6
    Sledge92 Sledge92 @Sledge92
    *

    I see, thank you for informing me!
    I'm still having a problem. I don't have an answer to what I asked. Perhaps I didn't explain myself correctly.
    What I would like would be to know if there's any fast way to get all the intersections of all paths.

    E.g.: I've uploaded 2 files, Ex1 before any transformation, and Ex2 is the result I want.
    In the example files we have 4 paths. A, B, C and D. The total number of intersections to check would be:

    A - B, A - C, A - D
    B - C, B - D
    C - D

    In this case, only A - B, A - D and B - C are available.
    Currently I'm doing this by hand but is there any faster way of doing this, instead of by hand?
    Thank you!

    Ex1
    Ex2
  7. #7
    Tyler Durden Tyler Durden @TylerDurden

    Here's one way to do that.

    2020 02 23 10 18 25
  8. #8
    Sledge92 Sledge92 @Sledge92
    *

    Hmmm..I see. And can this be done with the example files on my original post, meaning "Borders" -> "No Borders"? Because the files I provided initially are the ones I'm using. The latest example was just to help you understand. I'm not sure how exactly I can use the same method to quickly get all the various intersections in the "Borders" object I provided..
    Thank you very much for all the help, so far!

  9. #9
    brynn brynn @brynn

    I think it might be better if you could share your SVG file with us.  I think I understand what you want to do.  But I can't tell exactly what you have as the starting place.  So it's hard to suggest the best way to proceed.  (Looking at the screenshots doesn't let us know what kind of objects you are using.)

    In the end, you might already be doing it properly.  Maps can be very labor intensive!

    Here's how I would get from your first screenshot (the map with black borders) to the 3rd screenshot (pale pink-filled shapes on a black background).  I would select the entire map in the first screenshot, and Group it (all in one group).  Then I would fill the group with the pale pink color.  Then I would draw a large rectangle, fill it with black, and put it behind the map.  Then I would adjust the stroke widths of the group, if you want the borders wider.

    If the individual shapes need to be different colors, that won't work.  Or if you have some other reason that the individual shapes need to be separate units, it won't work.  But as long as the shapes are all the same fill color, and you make the background rectangle the same color as the strokes, it will give the appearance in your 3rd screenshot.

    (No it turned out not to be the same map.  Just similar shapes.)

  10. #10
    Sledge92 Sledge92 @Sledge92
    *

    @brynn Okay, I can share the file. So, the main file is an .svg file of Portugal, and I'm adding the 3rd subdivision of administrative territory. In English it goes something like "District"(1st level), "Municipality"(2nd) and "Parish"(3). So far, someone has done levels 1 and two but I want to do all 3 levels, which means that every object of lvl 2 will get subdivided into several of lvl3.
    I'm already 1/3 of the way done. What do you think?
    P.S.: The objects have no styling because CSS is taking care of that for me. And in that 2nd photo, I converted all objects to paths, which is why there's no background. The base color is already there, either way, so there's no need for filling. And if you open the map file in Inkscape, or with a text editor, you'll see that there's a ton of objects there.

  11. #11
    brynn brynn @brynn
    *

    Ok, in some places, I see some tiny black shapes on top of larger black shapes, and those are on top of even larger black shapes.  In other places, just the medium shape on top of the larger shape.  So you must be making the smallest black shapes?

    Ooohh ok, I finally located the overall shape in the Amares.svg and Barcelos.svg files, in the larger map file.  So how do you fit those into the larger map?  Because they are much larger than their actual place in the map.

    Oh, I see I had your screenshots backwards in your first message.  I think I'm starting to get the idea.

    Is literally all you need to do is remove the outer border?  Or is there more to it than that?  I guess you need to scale it too?

    Ok, I think I finally have the before and after, and you want to know if there is a faster process than what you've been doing, right?

    I think I might have a plan.  But first, I'm still not clear what you've been doing so far.  Can you explain?  You mentioned about intersections, but I'm not sure if you meant that you're using the boolean Intersection, or if you meant something else.

  12. #12
    Sledge92 Sledge92 @Sledge92
    *

    @brynn Yes, exactly. Those 3 different sizes correspond to the "levels". Perhaps I could even add them as layers, but that will only be when I've added all I want, to not slow the process. And yes, I'm making the small shapes. Should you want to see the shapes in inkscape, you just need to add:
    style="fill:#ffefd5;fill-opacity:1;fill-rule:evenodd;stroke:#000000;stroke-width:0.2;stroke-opacity:1"
    on any object you want to see in Inkscape, in the code. There are already examples of this in the file.(Basically, all the shapes with that light yellowish color, visible on inkscape)

    I fit those bigger ones by scaling them, but before I import them to the map.svg I need to prepare them for importing. I basically have to delete anything in it that's not the area that I want. To achieve this I edit the map file in a text editor, removing the "sodipodi:insensitive="true"" so that I can select and delete anything I want in Inkscape(I can either delete in inkscape or in the text editor). After removing the "insensitive" part, I delete everything that's not the areas, and when I'm done deleting what I don't want I convert the objects to paths. By now you'll have the Borders file I provided in the original post.
    After all this process, I'm ready to start removing the perimeter of the file, so, he outernmost border. This is where I start making all the intersections between every area and the others. When I'm done, I'll have the "No Borders" file in my original post, and the file is ready to be imported to the map file.

    Yes, all I need to do is remove the borders of the objects to import, because the premade objects in map.svg have different borders than the objects to import(you can compare the borders of the Braga file I sent with the border of Braga already in map.svg. You'll come to the conclusion they're different, so you can't just copy/paste).

    Yes, the intersections I'm talking about are the boolean intersections, I believe. It's basically just "Path->Intersection", hence me converting the objects to paths, in the preparation. 

    I'll provide you an example of a completely unedited file(Braga_freguesias...) as well, should you want to try it yourself. The Braga object in map.svg is also visible and colored so you could even try doing it to see how your methos works!

    I hope this helps you understanding! Thanks a lot for helping me out, and I'm sorry for the bigger post.

  13. #13
    Tyler Durden Tyler Durden @TylerDurden

    I would probably work the other direction. It means starting over, but the work will go faster and be much more precise.

    I'd start with one map of parishes and add each neighboring map of parishes on a new layer. 

    The maps will be different scale, but there is an extension Menu: Extensions>Scale>RealScale which can be used to match the scale of each added map to the document.

     

    TD

  14. #14
    brynn brynn @brynn

    Ok, I have a couple of questions, before I can go on.

    First, I hear that you want to remove the outer border.  But when I look at your map file with all the shapes, they all have all their strokes.  You can't see them because the fill and stroke are the same color.  But they are still there.  So I'm still missing the reason for removing the stroke on the outer paths.

    Oohh....  You said the reason you want to remove them is because they don't perfectly match the border of the larger shape which they fill.  So you must be using duplicates of the larger shapes when you're doing the Intersections, to create the correct border for the outer shapes?

    When you say you convert the shapes to paths, to me, that means converting a raster file to vector, probably using an auto trace engine, such as Inkscape's Trace Bitmap.  But it sounds like you describe using Path menu > Intersection as converting to paths?

    In your Amares file, that does look like it's been auto-traced, because they are not single, stroked paths.  Instead, they are 2 closed paths, with a black fill in between.  But since the Barcelos file contains the single, stroked paths, I'll set aside the Amares file for the moment.

    First, a lot of the paths I've looked at, both in the Barcelos and the main map files, have tons and tons and tons of nodes - wayyy more nodes than needed.  You will run into performance issues, if you haven't already.  You can reduce the number of nodes easily, without distorting the paths, by using Path menu > Simplify.  If you use it only once, you won't notice any distortion.  If you use it twice or more, you might notice.  However, you can change the threshold for simplification, to make it more aggressive, if you like.  The setting is in Edit menu > Preferences > Behavior.  (click on Behavior, then look on the right side of the window).

    The large amount of nodes in the main map file might be coming from all the Intersection operations.  But it looks like they might be starting out with a lot already too.  So you might need to simplify both before and after you add them to the main map.  And if you do run into performance issues, you can simplify the ones you've already finished in the main map.  Note that it's better to do it one path at a time.  For example, if you select 10 paths, but one of them does not need simplifying, it will badly distort them.  I don't know why that is.  But if you have several paths with relatively equally excessive number of nodes, simplifying them together might work ok.

    Ok, I have a couple of ideas about this, and you'll have to decide whether they would be faster or easier than your current process.  I would think using Intersection to create all the small shapes must take some time, and probably a good bit of concentration, to avoid getting too confused.

    The easiest one to describe is just use node editing.  Use the Node tool

    • select each shape along the outside of the larger area, one at a time
    • break the path at the 2 nodes in each corner (the button on the Node tool control bar "Break path at selected nodes" or else I'm sure there's a key shortcut for it)
    • Path menu > Break Apart
    • deselect all and then select and delete the unwanted path

    But even doing that, I guess you would still have to do all the Intersection operations.  Right?  Maybe I have another idea that would let you do a lot of the work all in one go.

    What I have in mind would put approx a 0.2 pixel space between the shapes.  In your main map file, that won't work.  Because even the largest of your tiny shapes is less than 5 px.  However, if you do this procedure first, before you scale it down, it could work.  You do already have a little space between all the shapes.  (Before you mentioned adding the styles to the tiny shapes to see them better, I was selecting the larger shape below them, and I giving them white or yellow fill.  Then the space between them is like a yellow or white line.  So there already is some space there.)

    What I don't yet understand is how you get the outer border.  I'm guessing you use the larger shapes with the Intersection, to get them, but I'm not sure when you do that - before or after you scale.  But anyway, here's a general approach, and you could modify it to your needs, depending on how you get the outer border.

    • Select all the paths (such as in the Barcelos file)
    • Object menu > Fill and Stroke > Stroke Style tab > Width, set for 0.2 px or wider (I tried 0.1, but it leaves a lot of garbage behind) (or the equivalent for whatever unit you want)
    • Path menu > Combine
    • Draw a filled rectangle that's larger than the whole thing
    • Move it behind the combined shapes
    • Select both the combined paths and the rectangle
    • Path menu > Difference
    • Set both the fill and stroke to Unset (because that's how they are on the main map)
    • Optional:  Path menu > Break Apart.  This breaks them up so you can move them individually.  They would be easier to scale and move without breaking apart, but just so you can see they are separate pieces.

    Now you have all the individual shapes, in one go!  You'd have to modify it, to handle the outer border.  But if the space between the shapes is acceptable, it would save a lot of Intersection operations.

    One last note.  If you run into performance issues, which you almost certainly will, using layers can help to allow you to keep working.  Then you can disable layers that you don't need at the moment.  You could start putting new shapes into a new layer, and than some time in the future, move the ones you've already finished into new layers.  I'd probably make a layer for each of the largest shapes, but of course it's entirely up to you!

    Well, I hope some of that will be helpful.  Let us know how it goes.

     

  15. #15
    Sledge92 Sledge92 @Sledge92
    *

    @brynn Yes, I have to do duplicates.

    When I say convert to path I literally mean the option Path -> Convert stroke to path.

    Yes, I'd still have to do the intersections, which I want to avoid.

    Regarding performance, I'm not as concerned about it yet, since this is a personal side-project. However, it is something worth noting. If I am able to make the process faster, I might redo the whole country, with objects with much less nodes.

    You told me you don't understand how I get the outer border. Could you point it to me, specifically? With so many objects I lose track of which outer border you're talking about, so if you could post an image here with the outer border you mean it'd be easier for me to tell you how I get it.

    Indeed doing all the intersections is a grueling process, I even set up a macro to automatize the repetitive process.

    I was trying the general approach you meticulously mentioned but I'm afraid I'm not getting the result I want.(fail)
    Do you think you could try it and then show me how it turns out?
    I'm using Braga as the example.

    Thank you so much for the continued support!

     

    Braga
    Fail
  16. #16
    Tyler Durden Tyler Durden @TylerDurden

    It would take me too long to type this out.

     

  17. #17
    Sledge92 Sledge92 @Sledge92

    @TylerDurden Thank you for your reply. I changed my approach to deleting the segment (after choosing all the nodes). It would be really cool if I could choose the beginning node and end node and then delete all in between, but I don't think Inkscape allows for such a functionality yet.
    Also, regarding your way. That's definitely interesting! But I think I can't start over from scratch..but thank you for showing me that! It saves a lot of time and I didn't know you could do that indeed.
    (I hope I'm not bringing back a dead forum entry by now)

  18. #18
    Tyler Durden Tyler Durden @TylerDurden
    *

    I'd look at how many areas that still need to be completed and how long each method takes. It could be faster/easier to do the entire project using the scale method than to continue using the original method.

    Inkscape has Path>Simplify, but it can create unwanted distortion. 

    Inkscape also has Select>Invert, but that may also create unwanted distortion. 

     

  19. #19
    Sledge92 Sledge92 @Sledge92
    *

    I'd say it's about 56% done(Out of 18 districts 9 are done, one halfway done and 8 are eft to do).

    Then if I'd want to create a shape that is of the combined shapes that were joined(using the methd you were showing), how would I do that? I could do union, but there's distortion...

  20. #20
    Tyler Durden Tyler Durden @TylerDurden

    I'm not getting distortion when I Union the small areas together.

    e.g. I Union Barcelos, and I get the correct outline and rubbish nodes inside that I can delete.

  21. #21
    Sledge92 Sledge92 @Sledge92
    *

    Hey everyone! I wanted to say that I was able to modify the Multiple Boolean  Operations extension to suit my needs. Basically, it performs whatever operation between every object and every other object in the selection, so if I have 5 objects and I choose Intersection, it will do all the intersections between all 5 different objects, which was exactly what I wanted. The only downsides are 2:

    First one is that I can no longer use that extension for single operations. Perhaps I will duplicate the extension, so that one is for singles and the other one is for multiple ones.

    Second is that I can only have a maximum of 27 objets before it tells me that the filename is too long(since how it computes the intersections is through actions, that are appended to the command run in the command line, and any additional permutation "breaks the system"). I've already also figured out how to circumvent the problem. Unfortunately I get some few repeated intersections, which makes me wish inkscape had a functionality to remove redundant paths(paths that are duplicates) for inkscape v1.0, which I'm using, since it exists for version 0.9, if I'm not mistaken.

    But nonetheless, I'm quite happy about it! It saves so many hours of work! Should you guys want me to share it let me know.

    Since it's a modification of an extension, I believe I should notify the maker of this extension, right?

    And answering @TylerDurden, I also am not getting distortion now, for some reason...
    Either way, I think my problem is fixed, so I'd like to thank you for helping me with this, even if it took me longer to get to where I wanted! 😊

Inkscape Inkscape.org Inkscape Forum Beyond the Basics How to only keep only path intersections?