I am trying to run the following PostGIS query:
select ST_distance_spheroid(
ST_GeomFromText('POINT(
(select AsText(location) from test where name="EGMC")
)', 4326),
ST_GeomFromText('POINT(
(select AsText(location) from test where name="EGDY")
)', 4326),
'SPHEROID["WGS_1984",6378137,298.257223563]'
);
but keep getting an error:
ERROR: parse error - invalid geometry
HINT: "POINT(
(s" <-- parse error at position 9 within geometry
I'm happy that I know what the error means, I just don't know how to go about achieving what I want to do. I don't want to manually specify the location, it's stored in the database! I know the name of the place, so I want to get it's location by looking it up. How should I be doing this? Also, it seems a bit unnecessary to convert to a string to convert back, what else can I do?
If I can do this without having to specify variables that would be great.
Thanks.
You are mixing SQL and WKT, which are not the same. Furthermore, you don't need to recreate geometries that already exist. Query the existing geometries instead:
select ST_distance_spheroid(g1.location, g2.location, 'SPHEROID["WGS_1984",6378137,298.257223563]')
from test g1, test g2
where g1.name = "EGMC" and g2.name = "EGDY";
Related
What I have so far:
select Metro.Object_ID
from Geocoding_tab.dbo.Part1_Part2_Combined_Final as paypal
,Object_id.dbo.All_Combined_9_Metros as Metro
where paypal.geom.STIntersect(Metro.GEOM) = 1;
You can amend the query to use JOIN syntax
SELECT Metro.Object_ID
FROM
Geocoding_tab.dbo.Part1_Part2_Combined_Final as paypal
INNER JOIN
Object_id.dbo.All_Combined_9_Metros as Metro ON paypal.geom.STIntersects(Metro.GEOM) = 1;
EDIT - following error shown in comments below
You can use the STIsValid Method to detect if you have shape that's invalid. The method will return 0 if invalid.
The MakeValid Method will fix invalid shape data
for example:
UPDATE table SET geom = geom.MakeValid() where geom.STIsValid() = 0
Note
As the error suggests, the MakeValid could alter your shape in a way that you might consider incorrect, depending on the original problem with the shape. So you should confirm that you're happy with the "corrected" shape before proceeding.
while executing this query i am getting error
UPDATE treedetailsentered
set geom_line=st_geomfromtext('LineString(('||longitude||' '||latitude||','||lon2||' '||lat2||'))', 4326);
error
ERROR: parse error - invalid geometry
HINT: "LineString((7" <-- parse error at position 13 within geometry
********** Error **********
this is how i created column in table for the linestring
ALTER TABLE public.treedetailsentered
ADD COLUMN geom_line geometry(LineString, 4326)
It's hard to tell what the exact problem is without knowing what type longitude, latitude have, but my guess is that you're not forming a proper string.
My advice is to debug this by first creating a proper text via a query that's simpler:
select 'LineString(('||longitude||' '||latitude||','||lon2||' '||lat2||'))'
Depending on what types you have, you might have to do some conversions, like this:
select 'LineString(('||longitude::text||' '||latitude::text||','||lon2::text||' '||lat2::text||'))'
If you already have texts, make sure that the numbers are properly formatted. Floats are expected to have a dot decimal separator for example.
I have added an extra bracket i removed that it worked for me.
UPDATE treedetailsentered set geom_line=ST_GeomFromText('LINESTRING(' || longitude || ' '||latitude||','||lon2||' '||lat2||')', 4326);
The following is being attempted in the console for a postgis enabled rails4.2 application.
#target = Target.last
#meter_radius = 1000
#valid_points = Target.where("ST_DWithin(#{#target.lat}, #{#target.lon}, #{#meter_radius}))
lat and lon are defined as decimal values. This translates into the following query
SELECT "targets".* FROM "targets" WHERE (ST_DWithin(38.656679, 15.984094, 1000))
with the error:
PG::UndefinedFunction: ERROR: function st_dwithin(numeric, numeric, integer) does not exist
I believe I need to declare the data type (geometric or geographic) for these values but am not sure how. I am also wondering whether the ST_DWithin function can work off of the 3857 data type, even though the documentation does not state so.
note the #target object also has a lonlat attributes defined as a spatial value in postgresql with :srid=>3857, :type=>"point" defined.
Update
#valid_points = Target.where("ST_DWithin(lonlat, ST_PointFromText('#{#target.lonlat}', #{#meter_radius}))
returns a result and thus appears syntactically valid.
SELECT "targets".* FROM "targets" WHERE (ST_DWithin(lonlat, ST_PointFromText('POINT (15.984094 38.656679)', 3857), 1000))
However the result is incorrect. It essentially finds all the points of the table. Being of SRID type, this needed to be expressed in degrees, not meters.
One answer, leveraging the lonlat spatial point stored:
#target = Target.last
#degree_radius = 0.2249
#valid_points = Target.where("ST_DWithin(lonlat, ST_PointFromText('#{#target.lonlat}', #{#degree_radius}))
which returns a result and thus appears syntactically valid.
SELECT "targets".* FROM "targets" WHERE (ST_DWithin(lonlat, ST_PointFromText('POINT (15.984094 38.656679)', 3857), 0.2249))
Is there a (supposedly) simple way to get an exact match when using the replace function in SQL Server 2012? (I'm open to other searching possibilities as well, of course)
For example, I'm using the following code to grab all the objects in a DB containing 'texter' in it at some point:
select OBJECT_NAME(object_id) name,
OBJECT_DEFINITION(object_id) code,
type
into #tester
from sys.objects
where OBJECT_DEFINITION(object_id) LIKE '%texter%'
This doesn't seem to differentiate between .texter, #texter or stupidtexter.
and so if I use:
update #tester
set code = REPLACE(code, 'texter', 'bluenose')
where code LIKE '%texter%'
It's going to replace any variant of 'texter' with 'bluenose'
Let's assume I only want to replace the ' texter' and '.texter' versions of this and nothing else (noting that within each object it is possible that #texter or stupidtexter may also be present in the object code).
Is there a way I can differentiate between the variants of 'texter', #texter and stupidtexter?
Thanks
The solution was:
replace(REPLACE(code, '.texter', 'bluenose'), ' texter', 'bluenose')
Thanks to Sean Lange for the answer!
I am new to DB2 (using version 10.1) and I'm trying to execute a simple ltrim function in a test query.
select ltrim(',1,2,3,4', ',') from sysibm.sysdummy1;
This results in the following error:
DB2 SQL Error: SQLCODE=-440, SQLSTATE=42884, SQLERRMC=LTRIM;FUNCTION, DRIVER=3.65.77
Based on the documentation here, it seems my example should work. Note, I can use the alternative trim function or the 1-arg version of ltrim:
select trim(l ',' from ',1,2,3,4') from sysibm.sysdummy1;
select ltrim(' test') from sysibm.sysdummy1;
And those works fine!
Are there some fundamental differences between the 2-arg form of ltrim and the other examples I provided?
This function was added in DB2 10.1 fix pack 2 -- you may want to upgrade.
Making some guesses here, but are you sure you're on 10.1? That version of that function you're trying to use appears to have been added in 10, so if you're on 9.x, it won't work.
If you are, you might check and see if your schema path includes SYSIBM:
SELECT CURRENT_PATH FROM SYSIBM.SYSDUMMY1
The "old" one-argument version of the function is in SYSFUN, which (if your path doesn't include SYSIBM) might be why you're still able to use that version.
If SYSIBM isn't in your path, you can try changing it using:
SET PATH = SYSIBM, CURRENT_PATH
Why not try STRIP() instead:
VALUES STRIP(',1,2,3,4', L, ',');
(side note: It is simpler to use the VALUES statement, rather than SELECT FROM SYSIBM.SYSDUMMY1)