<div dir="ltr">You can cast to geography only within your ST_Distance call to get meters:<br><br><font face="courier new, monospace" color="#351c75">ST_Distance(t1.geo_position::geography,t2.geo_position::geography)</font><br><div class="gmail_extra"><br></div><div class="gmail_extra">That way the rest of your query will still use the index.<br></div><div class="gmail_extra"><br><div class="gmail_quote">On Mon, Apr 11, 2016 at 11:21 AM, Michael Moore <span dir="ltr"><<a href="mailto:michaeljmoore@gmail.com" target="_blank">michaeljmoore@gmail.com</a>></span> wrote:<br><blockquote class="gmail_quote" style="margin:0px 0px 0px 0.8ex;border-left:1px solid rgb(204,204,204);padding-left:1ex"><div dir="ltr">I am trying to find all zip codes withing a given range of current zip code. For example, User is at zip code 95076 and want to know all zip codes within a 30 mile range. This will always be a short distance, nothing over 50 miles. The source zip code is any place in the USA.  My input table has a latitude field, a longitude field and a POINT field for each zip code. <div>I want to supply in input in meters, not degrees. I do NOT want to cast to <i>geography</i> because this prevents the index from being used. </div><div>Here is a <b><i>working query</i></b> that uses the index, but ST_DWithin  is not understanding ".05" as meters:</div><div><br></div><div><br></div><div><div><font face="courier new, monospace" color="#351c75">lcd1_dev=>   select t2.city, t2.postalcode, ST_Distance(t1.geo_position,t2.geo_position)</font></div><div><font face="courier new, monospace" color="#351c75">lcd1_dev->   from      tpostalcoordinate t1</font></div><div><font face="courier new, monospace" color="#351c75">lcd1_dev->   left join tpostalcoordinate t2 on ST_DWithin(t1.<b>geo_position</b>,t2.<b>geo_position</b> , .05)</font></div><div><font face="courier new, monospace" color="#351c75">lcd1_dev->   where t1.postalcode = '94404'and t1.countrycode2tcountry = 'US' and t2.countrycode2tcountry= 'US';</font></div><div><font face="courier new, monospace">     city     | postalcode |     st_distance</font></div><div><font face="courier new, monospace">--------------+------------+---------------------</font></div><div><font face="courier new, monospace"> Redwood City | 94065      |  0.0273766323714193</font></div><div><font face="courier new, monospace"> San Mateo    | 94408      | 0.00504738546179631</font></div><div><font face="courier new, monospace"> Belmont      | 94002      |  0.0440065904155286</font></div><div><font face="courier new, monospace"> San Mateo    | 94404      |                   0</font></div><div><font face="courier new, monospace"> San Mateo    | 94403      |  0.0370314731005901</font></div><div><font face="courier new, monospace"> San Mateo    | 94407      |  0.0416118372581607</font></div><div><span style="font-family:"courier new",monospace"><br></span></div><div><span style="font-family:"courier new",monospace"><b>This shows that I am using SRID 4896</b></span></div></div><div><font face="courier new, monospace">lcd1_dev=> select * from geometry_columns where f_table_name = 'tpostalcoordinate';</font></div><div><font face="courier new, monospace"> f_table_catalog | f_table_schema |   f_table_name    | f_geometry_column | coord_dimension | srid | type</font></div><div><font face="courier new, monospace">-----------------+----------------+-------------------+-------------------+-----------------+------+-------</font></div><div><font face="courier new, monospace"> lcd1_dev        | qsn_app        | tpostalcoordinate | geo_position      |               2 | 4896 | POINT </font></div><div><font face="courier new, monospace"><br></font></div><div><font face="courier new, monospace"><b>4896 is UNIT "metre" as show here:</b></font></div><div><div><span style="line-height:22.8571px"><font style="color:rgb(255,255,255);font-size:16px" face="Ubuntu Mono, Menlo, Monaco, Consolas, Courier New, monospace"> </font><font face="courier new, monospace" color="#000000" size="1">4896 | EPSG      |      4896 | GEOCCS["ITRF2005",DATUM["International_Terrestrial_Reference_Frame_2005",SPHEROID["GRS 1980",6378137,298.257222101,AUTHORIT</font></span></div><div><font face="courier new, monospace" size="1"><span style="line-height:22.8571px"><font color="#000000">Y["EPSG","7019"]],AUTHORITY["EPSG","6896"]],PRIMEM["Greenwich",0,AUTHORITY["EPSG","8901"]],</font><font style="background-color:rgb(255,255,0)" color="#cc0000">UNIT["metre"</font><font color="#000000">,1,AUTHORITY["EPSG","9001"]],AXIS["Geocentric X",OTH</font></span></font></div><div><span style="line-height:22.8571px"><font face="courier new, monospace" color="#000000" size="1">ER],AXIS["Geocentric Y",OTHER],AXIS["Geocentric Z",NORTH],AUTHORITY["EPSG","4896"]] | +proj=geocent +ellps=GRS80 +units=m +no_de</font></span></div><div><span style="line-height:22.8571px"><font face="courier new, monospace" color="#000000" size="1"><br></font></span></div><div><span style="line-height:22.8571px"><font face="courier new, monospace" color="#000000" size="2"><b>The following (from EXPLAIN) proves that the index is being used in the working query shown above:<span style="background-color:rgb(255,255,255)"><br></span></b></font></span><span style="color:rgb(11,83,148);font-family:"courier new",monospace;font-size:x-small;line-height:22.8571px;background-color:rgb(255,217,102)">   ->  Index Scan using tpostalcoordinate_pk on tpostalcoordinate t1  (cost=0.42..8.45 rows=1 width=32)</span></div><div><font face="courier new, monospace" size="1"><span style="line-height:22.8571px"><div style="color:rgb(0,0,0)"><br></div></span></font></div><div><span style="line-height:22.8571px"><font style="font-size:16px" face="Ubuntu Mono, Menlo, Monaco, Consolas, Courier New, monospace" color="#000000">What do I need to do to get this in meters without losing my index access?</font></span></div></div><div><span style="line-height:22.8571px"><font style="font-size:16px" face="Ubuntu Mono, Menlo, Monaco, Consolas, Courier New, monospace" color="#000000"><br></font></span></div><div><span style="line-height:22.8571px"><font style="font-size:16px" face="Ubuntu Mono, Menlo, Monaco, Consolas, Courier New, monospace" color="#000000">TIA,</font></span></div><div><span style="line-height:22.8571px"><font style="font-size:16px" face="Ubuntu Mono, Menlo, Monaco, Consolas, Courier New, monospace" color="#000000">Mike</font></span></div></div><br>_______________________________________________<br>
postgis-users mailing list<br>
<a href="mailto:postgis-users@lists.osgeo.org">postgis-users@lists.osgeo.org</a><br>
<a href="http://lists.osgeo.org/mailman/listinfo/postgis-users" rel="noreferrer" target="_blank">http://lists.osgeo.org/mailman/listinfo/postgis-users</a><br></blockquote></div><br></div></div>