PostGIS: Calculate shortest distance between polygon












1















I have many polygons stored in a table. I want to filter out polygons where they separate too far away (say 1km).



My approach is:
calculate the minimum distance between a particular polygon to other polygons, and store the value in a new column in the table.



Then filter out polygon with minimum distance larger than 1 km.



I have tired to use:



ALTER TABLE "int_20190124" ADD "nearestDistance" float8;
INSERT INTO "int_20190124"("nearestDistance")
VALUES
(
SELECT ST_Distance("int_20190124".geom, "int_20190124".geom)
FROM "int_20190124"
ORDER BY ST_Distance("int_20190124".geom, "int_20190124".geom)
LIMIT 1
)


I am very new to postgis. Thanks for your help!










share|improve this question



























    1















    I have many polygons stored in a table. I want to filter out polygons where they separate too far away (say 1km).



    My approach is:
    calculate the minimum distance between a particular polygon to other polygons, and store the value in a new column in the table.



    Then filter out polygon with minimum distance larger than 1 km.



    I have tired to use:



    ALTER TABLE "int_20190124" ADD "nearestDistance" float8;
    INSERT INTO "int_20190124"("nearestDistance")
    VALUES
    (
    SELECT ST_Distance("int_20190124".geom, "int_20190124".geom)
    FROM "int_20190124"
    ORDER BY ST_Distance("int_20190124".geom, "int_20190124".geom)
    LIMIT 1
    )


    I am very new to postgis. Thanks for your help!










    share|improve this question

























      1












      1








      1








      I have many polygons stored in a table. I want to filter out polygons where they separate too far away (say 1km).



      My approach is:
      calculate the minimum distance between a particular polygon to other polygons, and store the value in a new column in the table.



      Then filter out polygon with minimum distance larger than 1 km.



      I have tired to use:



      ALTER TABLE "int_20190124" ADD "nearestDistance" float8;
      INSERT INTO "int_20190124"("nearestDistance")
      VALUES
      (
      SELECT ST_Distance("int_20190124".geom, "int_20190124".geom)
      FROM "int_20190124"
      ORDER BY ST_Distance("int_20190124".geom, "int_20190124".geom)
      LIMIT 1
      )


      I am very new to postgis. Thanks for your help!










      share|improve this question














      I have many polygons stored in a table. I want to filter out polygons where they separate too far away (say 1km).



      My approach is:
      calculate the minimum distance between a particular polygon to other polygons, and store the value in a new column in the table.



      Then filter out polygon with minimum distance larger than 1 km.



      I have tired to use:



      ALTER TABLE "int_20190124" ADD "nearestDistance" float8;
      INSERT INTO "int_20190124"("nearestDistance")
      VALUES
      (
      SELECT ST_Distance("int_20190124".geom, "int_20190124".geom)
      FROM "int_20190124"
      ORDER BY ST_Distance("int_20190124".geom, "int_20190124".geom)
      LIMIT 1
      )


      I am very new to postgis. Thanks for your help!







      qgis postgis






      share|improve this question













      share|improve this question











      share|improve this question




      share|improve this question










      asked 4 hours ago









      JOHN JOHN

      14817




      14817






















          1 Answer
          1






          active

          oldest

          votes


















          3














          You can use ST_ClusterDBSCAN to group nearby geometries together and assigning them a cluster id. Cluster ID will be null for single geometries not within the specified distance of another.



          Image taken from the help section showing cluster IDs:



          enter image description here



          This query should return only the records within 1000 m of Another. minpoints := 2 is to prevent single points from getting a cluster ID:



          SELECT * FROM
          (SELECT *, ST_ClusterDBSCAN(geom, eps := 1000, minpoints := 2) OVER () clust
          FROM int_20190124) t1
          WHERE t1.clust IS NOT NULL;


          Example with points and 10000 m distance:
          enter image description here






          share|improve this answer


























          • Why minpoints := 2 and not minpoints := 1?

            – John Powell
            3 hours ago











          • With 1, single records with no other Points nearby will also get a cluster id. Dont you agree?

            – BERA
            3 hours ago








          • 2





            Yes, probably. I have always put array_agg(cluster_id) as cluster_ids in the inner select and then used WHERE array_length(cluster_ids, 1) > 1 for this kind of logic, but your approach is simpler. You should probably explain it in the answer, though, as ClusterDBScan is a bit non-obvious when you first see it +1 anyway, this really is one of my favourite functions, it has so many cool uses, that are really painful to do the old way with spatial self joins.

            – John Powell
            3 hours ago













          • @BERA Thank you! Although I still don't understand the query completely, it works! I will study more PostGIS later.

            – JOHN
            2 hours ago











          • The underlying theory for DBScan en.wikipedia.org/wiki/DBSCAN is worth a read. This is a somewhat tricky query as it also involves a window function, postgresql.org/docs/current/tutorial-window.html, so don't feel too bad if you don't get it immediately. The benefit of a window function is that you can return the actual geometry IDs in each cluster, which was not the case with the original Postgis clustering algorithms, such as, ST_ClusterIntersecting and ST_ClusterWithin.

            – John Powell
            2 hours ago











          Your Answer








          StackExchange.ready(function() {
          var channelOptions = {
          tags: "".split(" "),
          id: "79"
          };
          initTagRenderer("".split(" "), "".split(" "), channelOptions);

          StackExchange.using("externalEditor", function() {
          // Have to fire editor after snippets, if snippets enabled
          if (StackExchange.settings.snippets.snippetsEnabled) {
          StackExchange.using("snippets", function() {
          createEditor();
          });
          }
          else {
          createEditor();
          }
          });

          function createEditor() {
          StackExchange.prepareEditor({
          heartbeatType: 'answer',
          autoActivateHeartbeat: false,
          convertImagesToLinks: false,
          noModals: true,
          showLowRepImageUploadWarning: true,
          reputationToPostImages: null,
          bindNavPrevention: true,
          postfix: "",
          imageUploader: {
          brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
          contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
          allowUrls: true
          },
          onDemand: true,
          discardSelector: ".discard-answer"
          ,immediatelyShowMarkdownHelp:true
          });


          }
          });














          draft saved

          draft discarded


















          StackExchange.ready(
          function () {
          StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fgis.stackexchange.com%2fquestions%2f312167%2fpostgis-calculate-shortest-distance-between-polygon%23new-answer', 'question_page');
          }
          );

          Post as a guest















          Required, but never shown

























          1 Answer
          1






          active

          oldest

          votes








          1 Answer
          1






          active

          oldest

          votes









          active

          oldest

          votes






          active

          oldest

          votes









          3














          You can use ST_ClusterDBSCAN to group nearby geometries together and assigning them a cluster id. Cluster ID will be null for single geometries not within the specified distance of another.



          Image taken from the help section showing cluster IDs:



          enter image description here



          This query should return only the records within 1000 m of Another. minpoints := 2 is to prevent single points from getting a cluster ID:



          SELECT * FROM
          (SELECT *, ST_ClusterDBSCAN(geom, eps := 1000, minpoints := 2) OVER () clust
          FROM int_20190124) t1
          WHERE t1.clust IS NOT NULL;


          Example with points and 10000 m distance:
          enter image description here






          share|improve this answer


























          • Why minpoints := 2 and not minpoints := 1?

            – John Powell
            3 hours ago











          • With 1, single records with no other Points nearby will also get a cluster id. Dont you agree?

            – BERA
            3 hours ago








          • 2





            Yes, probably. I have always put array_agg(cluster_id) as cluster_ids in the inner select and then used WHERE array_length(cluster_ids, 1) > 1 for this kind of logic, but your approach is simpler. You should probably explain it in the answer, though, as ClusterDBScan is a bit non-obvious when you first see it +1 anyway, this really is one of my favourite functions, it has so many cool uses, that are really painful to do the old way with spatial self joins.

            – John Powell
            3 hours ago













          • @BERA Thank you! Although I still don't understand the query completely, it works! I will study more PostGIS later.

            – JOHN
            2 hours ago











          • The underlying theory for DBScan en.wikipedia.org/wiki/DBSCAN is worth a read. This is a somewhat tricky query as it also involves a window function, postgresql.org/docs/current/tutorial-window.html, so don't feel too bad if you don't get it immediately. The benefit of a window function is that you can return the actual geometry IDs in each cluster, which was not the case with the original Postgis clustering algorithms, such as, ST_ClusterIntersecting and ST_ClusterWithin.

            – John Powell
            2 hours ago
















          3














          You can use ST_ClusterDBSCAN to group nearby geometries together and assigning them a cluster id. Cluster ID will be null for single geometries not within the specified distance of another.



          Image taken from the help section showing cluster IDs:



          enter image description here



          This query should return only the records within 1000 m of Another. minpoints := 2 is to prevent single points from getting a cluster ID:



          SELECT * FROM
          (SELECT *, ST_ClusterDBSCAN(geom, eps := 1000, minpoints := 2) OVER () clust
          FROM int_20190124) t1
          WHERE t1.clust IS NOT NULL;


          Example with points and 10000 m distance:
          enter image description here






          share|improve this answer


























          • Why minpoints := 2 and not minpoints := 1?

            – John Powell
            3 hours ago











          • With 1, single records with no other Points nearby will also get a cluster id. Dont you agree?

            – BERA
            3 hours ago








          • 2





            Yes, probably. I have always put array_agg(cluster_id) as cluster_ids in the inner select and then used WHERE array_length(cluster_ids, 1) > 1 for this kind of logic, but your approach is simpler. You should probably explain it in the answer, though, as ClusterDBScan is a bit non-obvious when you first see it +1 anyway, this really is one of my favourite functions, it has so many cool uses, that are really painful to do the old way with spatial self joins.

            – John Powell
            3 hours ago













          • @BERA Thank you! Although I still don't understand the query completely, it works! I will study more PostGIS later.

            – JOHN
            2 hours ago











          • The underlying theory for DBScan en.wikipedia.org/wiki/DBSCAN is worth a read. This is a somewhat tricky query as it also involves a window function, postgresql.org/docs/current/tutorial-window.html, so don't feel too bad if you don't get it immediately. The benefit of a window function is that you can return the actual geometry IDs in each cluster, which was not the case with the original Postgis clustering algorithms, such as, ST_ClusterIntersecting and ST_ClusterWithin.

            – John Powell
            2 hours ago














          3












          3








          3







          You can use ST_ClusterDBSCAN to group nearby geometries together and assigning them a cluster id. Cluster ID will be null for single geometries not within the specified distance of another.



          Image taken from the help section showing cluster IDs:



          enter image description here



          This query should return only the records within 1000 m of Another. minpoints := 2 is to prevent single points from getting a cluster ID:



          SELECT * FROM
          (SELECT *, ST_ClusterDBSCAN(geom, eps := 1000, minpoints := 2) OVER () clust
          FROM int_20190124) t1
          WHERE t1.clust IS NOT NULL;


          Example with points and 10000 m distance:
          enter image description here






          share|improve this answer















          You can use ST_ClusterDBSCAN to group nearby geometries together and assigning them a cluster id. Cluster ID will be null for single geometries not within the specified distance of another.



          Image taken from the help section showing cluster IDs:



          enter image description here



          This query should return only the records within 1000 m of Another. minpoints := 2 is to prevent single points from getting a cluster ID:



          SELECT * FROM
          (SELECT *, ST_ClusterDBSCAN(geom, eps := 1000, minpoints := 2) OVER () clust
          FROM int_20190124) t1
          WHERE t1.clust IS NOT NULL;


          Example with points and 10000 m distance:
          enter image description here







          share|improve this answer














          share|improve this answer



          share|improve this answer








          edited 3 hours ago

























          answered 4 hours ago









          BERABERA

          15.8k52042




          15.8k52042













          • Why minpoints := 2 and not minpoints := 1?

            – John Powell
            3 hours ago











          • With 1, single records with no other Points nearby will also get a cluster id. Dont you agree?

            – BERA
            3 hours ago








          • 2





            Yes, probably. I have always put array_agg(cluster_id) as cluster_ids in the inner select and then used WHERE array_length(cluster_ids, 1) > 1 for this kind of logic, but your approach is simpler. You should probably explain it in the answer, though, as ClusterDBScan is a bit non-obvious when you first see it +1 anyway, this really is one of my favourite functions, it has so many cool uses, that are really painful to do the old way with spatial self joins.

            – John Powell
            3 hours ago













          • @BERA Thank you! Although I still don't understand the query completely, it works! I will study more PostGIS later.

            – JOHN
            2 hours ago











          • The underlying theory for DBScan en.wikipedia.org/wiki/DBSCAN is worth a read. This is a somewhat tricky query as it also involves a window function, postgresql.org/docs/current/tutorial-window.html, so don't feel too bad if you don't get it immediately. The benefit of a window function is that you can return the actual geometry IDs in each cluster, which was not the case with the original Postgis clustering algorithms, such as, ST_ClusterIntersecting and ST_ClusterWithin.

            – John Powell
            2 hours ago



















          • Why minpoints := 2 and not minpoints := 1?

            – John Powell
            3 hours ago











          • With 1, single records with no other Points nearby will also get a cluster id. Dont you agree?

            – BERA
            3 hours ago








          • 2





            Yes, probably. I have always put array_agg(cluster_id) as cluster_ids in the inner select and then used WHERE array_length(cluster_ids, 1) > 1 for this kind of logic, but your approach is simpler. You should probably explain it in the answer, though, as ClusterDBScan is a bit non-obvious when you first see it +1 anyway, this really is one of my favourite functions, it has so many cool uses, that are really painful to do the old way with spatial self joins.

            – John Powell
            3 hours ago













          • @BERA Thank you! Although I still don't understand the query completely, it works! I will study more PostGIS later.

            – JOHN
            2 hours ago











          • The underlying theory for DBScan en.wikipedia.org/wiki/DBSCAN is worth a read. This is a somewhat tricky query as it also involves a window function, postgresql.org/docs/current/tutorial-window.html, so don't feel too bad if you don't get it immediately. The benefit of a window function is that you can return the actual geometry IDs in each cluster, which was not the case with the original Postgis clustering algorithms, such as, ST_ClusterIntersecting and ST_ClusterWithin.

            – John Powell
            2 hours ago

















          Why minpoints := 2 and not minpoints := 1?

          – John Powell
          3 hours ago





          Why minpoints := 2 and not minpoints := 1?

          – John Powell
          3 hours ago













          With 1, single records with no other Points nearby will also get a cluster id. Dont you agree?

          – BERA
          3 hours ago







          With 1, single records with no other Points nearby will also get a cluster id. Dont you agree?

          – BERA
          3 hours ago






          2




          2





          Yes, probably. I have always put array_agg(cluster_id) as cluster_ids in the inner select and then used WHERE array_length(cluster_ids, 1) > 1 for this kind of logic, but your approach is simpler. You should probably explain it in the answer, though, as ClusterDBScan is a bit non-obvious when you first see it +1 anyway, this really is one of my favourite functions, it has so many cool uses, that are really painful to do the old way with spatial self joins.

          – John Powell
          3 hours ago







          Yes, probably. I have always put array_agg(cluster_id) as cluster_ids in the inner select and then used WHERE array_length(cluster_ids, 1) > 1 for this kind of logic, but your approach is simpler. You should probably explain it in the answer, though, as ClusterDBScan is a bit non-obvious when you first see it +1 anyway, this really is one of my favourite functions, it has so many cool uses, that are really painful to do the old way with spatial self joins.

          – John Powell
          3 hours ago















          @BERA Thank you! Although I still don't understand the query completely, it works! I will study more PostGIS later.

          – JOHN
          2 hours ago





          @BERA Thank you! Although I still don't understand the query completely, it works! I will study more PostGIS later.

          – JOHN
          2 hours ago













          The underlying theory for DBScan en.wikipedia.org/wiki/DBSCAN is worth a read. This is a somewhat tricky query as it also involves a window function, postgresql.org/docs/current/tutorial-window.html, so don't feel too bad if you don't get it immediately. The benefit of a window function is that you can return the actual geometry IDs in each cluster, which was not the case with the original Postgis clustering algorithms, such as, ST_ClusterIntersecting and ST_ClusterWithin.

          – John Powell
          2 hours ago





          The underlying theory for DBScan en.wikipedia.org/wiki/DBSCAN is worth a read. This is a somewhat tricky query as it also involves a window function, postgresql.org/docs/current/tutorial-window.html, so don't feel too bad if you don't get it immediately. The benefit of a window function is that you can return the actual geometry IDs in each cluster, which was not the case with the original Postgis clustering algorithms, such as, ST_ClusterIntersecting and ST_ClusterWithin.

          – John Powell
          2 hours ago


















          draft saved

          draft discarded




















































          Thanks for contributing an answer to Geographic Information Systems Stack Exchange!


          • Please be sure to answer the question. Provide details and share your research!

          But avoid



          • Asking for help, clarification, or responding to other answers.

          • Making statements based on opinion; back them up with references or personal experience.


          To learn more, see our tips on writing great answers.




          draft saved


          draft discarded














          StackExchange.ready(
          function () {
          StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fgis.stackexchange.com%2fquestions%2f312167%2fpostgis-calculate-shortest-distance-between-polygon%23new-answer', 'question_page');
          }
          );

          Post as a guest















          Required, but never shown





















































          Required, but never shown














          Required, but never shown












          Required, but never shown







          Required, but never shown

































          Required, but never shown














          Required, but never shown












          Required, but never shown







          Required, but never shown







          Popular posts from this blog

          ف. موراي أبراهام

          صرب

          كأس إنترتوتو