title: PostGIS convert meters to degree using epsg 5186
body: <p>I have a few questions about geometry and geography in PostGIS.</p>
  <p>I am currently using PostGIS and Postgresql.</p>
  <p>Most of my spatial data is from Korea which is basically latitude and longitude.</p>
  <p>For testing, I have created two tables with the same latitude and longitude data but different data types, one for geography with SRID 4326 and the other for geometry with SRID 5186.</p>
  <pre><code>create table geometry_stores
  (
      id       serial primary key,
      location geometry(POINT, 5186) not null
  );
  
  create table geography_stores
  (
      id       serial primary key,
      location geography(POINT, 4326) not null
  );
  </code></pre>
  <p>You can find more details of EPSG 5186 on this link <a href="https://epsg.io/5186" rel="nofollow noreferrer">https://epsg.io/5186</a></p>
  <p>Here is a list of question I have got:</p>
  <ol>
  <li><p>PostGIS has this method</p>
  <p><code>ST_DWithin(geometry g1, geometry g2, double precision distance_of_srid);</code></p>
  <p>is <code>distance_of_srid</code> a unit of EPSG? Is there any way I can convert meters (e.g. 1km) to <code>distance_of_srid</code> with EPSG 5186?</p>
  </li>
  <li><p>I understand that geography calculation measures the distances between points as true paths over a sphere while geometry calculation measures the distances between points as true paths over a Cartesian plane. Then if I give exactly the same distance to the following queries, they are supposed to yield different results or same results? because my understanding is that geometry with SRID 5186 is already projected with distortion of earth, then they should yield the same results?</p>
  <pre><code>select *
  from geography_stores
  where st_dwithin(location, st_setsrid(st_point(126.970769, 37.555479), 4326), same_distance_meter)
  </code></pre>
  <pre><code>select *
  from geometry_stores
  where st_dwithin(location, st_setsrid(st_point(126.970769, 37.555479), 5186), same_distance_degree)
  </code></pre>
  </li>
  <li><p>When I calculate distance on the geometry table with the following query, it gives me a degree, not meters. Is there any way I can convert this degree to meters with consideration of distortion of the earth?</p>
  <pre><code>select st_distance(location, st_setsrid(st_point(126.970769, 37.555479), 5186))
  from geometry_stores
  where id = 1;
  </code></pre>
  <p>I have tried with this query but got some error of  <code>Only lon/lat coordinate systems are supported in  geography. Where: SQL function &quot;st_distancesphere&quot; during inlining</code></p>
  <pre><code>select st_distancesphere(location, st_setsrid(st_point(126.970769, 37.555479), 5186))
  from geometry_stores
  where id = 1;
  </code></pre>
  </li>
  </ol>
  <p>I have read documents at the PostGIS website and some questions in StackOverflow but still got those three questions. Thank you guys for your help.</p>
  <hr />
  <h2>------------------------------- UPDATED -----------------------------------------</h2>
  <ol>
  <li>The column for my spatial data is geometry(POINT, 5186) so the table definition would be as following. Note that it is not geometry(POINT, 4326) not to convert it to geometry(POINT, 5186) on calculation. Should I store my data in geometry(POINT, 4326) and convert it on calculation?</li>
  </ol>
  <pre><code>create table geometry_stores
  (
      id       serial primary key,
      location geometry(POINT, 5186) not null
  );
  </code></pre>
  <ol start="2">
  <li>I executed following query and got results as follows:</li>
  </ol>
  <pre><code>select st_distance(st_setsrid(st_makepoint(126.808183, 37.463557), 4326)::geography,
                     st_setsrid(st_makepoint(126.970769, 37.555479), 4326)::geography);
  
  
  st_distance
  --------------
  17627.3138509
  </code></pre>
  <pre><code>select st_distance(st_setsrid(st_makepoint(126.808183, 37.463557), 5186)::geometry,
                     st_setsrid(st_makepoint(126.970769, 37.555479), 5186)::geometry)
  
  st_distance
  --------------
  0.186772218169622
  </code></pre>
  <p>It seems that the second's query gives me degree while the first one gives me meters. Am I doing something wrong in my query, please?</p>
  <ol start="3">
  <li>For st_within, I populated 3M data in geometry_stores table and the spatial data spread over at least 10km. I executed the following query.</li>
  </ol>
  <pre><code>select *
  from users
  where st_dwithin(location, st_setsrid(st_point(126.970769, 37.555479), 5186), 0.001)
  </code></pre>
  <p>This query gives me 158 rows and geometry viewer displays as per picture below.</p>
  <p><a href="https://i.stack.imgur.com/DUnRg.png" rel="nofollow noreferrer"><img src="https://i.stack.imgur.com/DUnRg.png" alt="enter image description here" /></a></p>
  <p>Let's execute the same query with distance 1 not 0.0001</p>
  <pre><code>select *
  from users
  where st_dwithin(location, st_setsrid(st_point(126.970769, 37.555479), 5186), 1)
  </code></pre>
  <p>This query gives me 32792923 rows which is all data in the table.</p>
  <p>Considering that the spatial data spread over at least 10km, it seems that the st_within query calculates the distance between two geometries with a unit (degeree) of EPSG5186 not meters. Then, I'd like to know if I can convert meters to the unit (degree) of EPSG5186 because I'd like to query with meters, not degree which I don't know how far a unit (degree) of EPSG5186 is.</p>