POSTGIS unwanted results with St_INTERSECTS and ST_CROSSES - postgis

I got a table l_t_troncon, with a geom column LINESTRING. It represents a Path.
Now, I want to detect if a path intersects others.
Sometimes, when I use ST_INTERSECTS, it says False.
Whereas, in same query, ST_DISTANCE says 0 and ST_INTERSECTION says there is intersection(s).
On a map, there is an intersection.
It's a join on table itself, SRID are same.
and geom column is a geometry (not geography).
I don't know why this happens.
I use postgresql 9.4 and postgis 2.1.8 from official postgresql APT repo.
Any explanation ?
my query :
SELECT lt2.id,
ST_DISTANCE(lt.geom, lt2.geom) as DIS,
ST_SRID(lt.geom) as SRID,
ST_ASTEXT(lt.geom) as GEOM1,
ST_ASTEXT(lt2.geom) as GEOM2,
ST_CROSSES(lt.geom, lt2.geom) as CROSSES,
ST_INTERSECTS(lt.geom, lt2.geom) as INTERSECTS,
ST_DWITHIN(lt.geom, lt2.geom, 0) as DWITHIN,
ST_EQUALS(ST_ClosestPoint(lt.geom, lt2.geom), ST_ClosestPoint(lt2.geom, lt.geom)) as POINTS_EQUALS,
ST_ASTEXT(ST_INTERSECTION(lt.geom, lt2.geom)) as INTERSECTION
FROM l_t_troncon lt
JOIN l_t_troncon lt2 ON lt.id != lt2.id
WHERE lt.id = 3253 ORDER BY DIS ASC;
results:
ID DIS SRID GEOM1 GEOM2 CROSSES INTERSECTS DWITHIN POINTS_EQUALS INTERSECTION
3251 0 2154 LINESTRING(1090785.76652617 6536888.19328297,1090833.26031043 6536868.26841502) LINESTRING(1090552.31321667 6538203.74023832,1090799.01544745 6536813.53348014) f f t t POINT(1090785.76652617 6536888.19328297)
3252 52.7247742247967 2154 LINESTRING(1090785.76652617 6536888.19328297,1090833.26031043 6536868.26841502) LINESTRING(1090799.01544745 6536813.53348014,1097373.76929105 6537183.45894039) f f f f GEOMETRYCOLLECTION EMPTY
3248 63.7211646996764 2154 LINESTRING(1090785.76652617 6536888.19328297,1090833.26031043 6536868.26841502) LINESTRING(1083213.56795189 6536386.7418127,1090799.01544745 6536813.53348014) f f f f GEOMETRYCOLLECTION EMPTY
If I try just on LINESTRINGS:
SELECT ST_DISTANCE(ST_GEOMFROMTEXT('LINESTRING(1090785.76652617 6536888.19328297,1090833.26031043 6536868.26841502)', 2154),
ST_GEOMFROMTEXT('LINESTRING(1090552.31321667 6538203.74023832,1090799.01544745 6536813.53348014)', 2154))
result is 4.06621238136966e-09
SELECT ST_ASTEXT(ST_INTERSECTION(ST_GEOMFROMTEXT('LINESTRING(1090785.76652617 6536888.19328297,1090833.26031043 6536868.26841502)', 2154),
ST_GEOMFROMTEXT('LINESTRING(1090552.31321667 6538203.74023832,1090799.01544745 6536813.53348014)', 2154)))
result is GEOMETRYCOLLECTION EMPTY
Issue seems to be only on the first result (ID 3251). Good results on both others

Related

JSON_EACH Columns

I have the following code
SELECT
p1.json_data -> 'factSheet'->'fact_sheet' ->'cash_flow' AS Date
FROM loan_applications AS l
LEFT JOIN risk_scores AS p1
ON l.id = p1.loan_application_id
WHERE l.human_readable_id = 'XXX'
And get this ouput:
{"21-01-11": 772.28, "21-01-12": 1210.28,...}
For my understanding, I should use json_each
json_each(p1.json_data -> 'factSheet'->'fact_sheet' ->'cash_flow') AS Date
but that just gives me the following output:
(21-01-11,772.28)
(21-01-12,1210.28)
...
...
I basically want the Date in one column and the revenue in the other.
Thanks

MDX Sum values from current month

I have the problem with SUM query.
I want to sum the 'zaplata' from every month, but always I get the sum of all years...
MDX query:
with member [Zapl] as
'Sum(
[Data Zameldowania].[Sprzątanie w dany typ dnia],
[Measures].[Zaplata - Sprzatanie]
)'
select
non empty
{[Data Zameldowania].[Sprzątanie w dany typ dnia].[Miesiac Slownie]} on 0,
non empty
{[Measures].[Liczba gosci],[Zapl]} on 1
from [Hurtownia Danych]
where
[Data Zameldowania].[Data sprzątania].[Rok].&[2012];
This is what I get from my MDX Query:
This is what I have in Data Warehouse (SQL qyery from DW shows that in MDX query i get sum of all 'zaplata'. I want to sum 'zaplata' of each mont):
Do you actually need to create the new measures member [Zapl] ? Does this not work?
select
non empty
{[Data Zameldowania].[Sprzątanie w dany typ dnia].[Miesiac Slownie]} on 0,
non empty
{ [Measures].[Liczba gosci]
,[Measures].[Zaplata - Sprzatanie] } on 1
from [Hurtownia Danych]
where
[Data Zameldowania].[Data sprzątania].[Rok].&[2012];

How to combine columns in Pig FULL OUTER JOIN

I have two tables:
1,'hello'
2,'world'
4,'this'
and
1,'john'
3,'king'
and I want to produce a table
1,'hello','john'
2,'world',''
3,'' ,king
4,'this' ,''
I am currently using the Pig command:
JOIN A BY code FULL OUTER,
B BY code;
but this gives me the output:
1,'hello',1,'john'
2,'world',,''
,'' ,3,king
4,'this' ,,''
I need the code columns to combine, how can I do this? Thanks
Yes join will always produce output like this,its a expected behavior in pig. One option could be try group operator instead of join operator.
a.txt
1,'hello'
2,'world'
4,'this'
b.txt
1,'john'
3,'king'
PigScript:
A = LOAD 'a.txt' USING PigStorage(',') AS (code:int,name:chararray);
B = LOAD 'b.txt' USING PigStorage(',') AS (code:int,name:chararray);
C = GROUP A BY code,B BY code;
D = FOREACH C GENERATE group,(IsEmpty(A.name) ? TOTUPLE('') : BagToTuple(A.name)) AS aname,(IsEmpty(B.name) ? TOTUPLE('') : BagToTuple(B.name)) AS bname;
E = FOREACH D GENERATE group,FLATTEN(aname),FLATTEN(bname);
DUMP E;
Output:
(1,'hello','john')
(2,'world',)
(3,,'king')
(4,'this',)
BagToTuple() is not available in native pig, you have to download the pig-0.11.0.jar and set it in your classpath.
Download jar from this link:
http://www.java2s.com/Code/Jar/p/Downloadpig0110jar.htm
A = load 'a' using PigStorage(',') as (code:int,name:chararray);
B = load 'b' using PigStorage(',') as (code:int,name:chararray);
C = join A by code full outer ,B by code;
D = foreach C generate
(A::code IS NULL ? B::code : A::code) AS code,
A::name as aname, B::name as bname;
dump D;
the result is
(1,'hello','john')
(2,'world',)
(3,,'king')
(4,'this,)
You can use union and then do a groupBy
Union A,B will give you:
1,'hello'
2,'world'
4,'this'
1,'john'
3,'king'
Now do a groupBy based on id. This will give you:
1, {'hello', 'john'}
2, {'world'}
3, {'king'}
4, {'this'}
Now you just need a udf to parse the bag. In udf iterate over each key to generate output in your format.
I also ran into same issue. This is how I solved it.
You can use the ternary operator after your join to re-assign a new code, based on whether it was populated in the A or B relations. In this example, if A.code is null then B.code is used, else A.code is used.
C = JOIN A BY code FULL OUTER, B BY code;
D = FOREACH C GENERATE
(A.code IS NULL ? B.code : A.code) AS code,
A.field1,
A.field2,
B.field3,
B.field4;

SQL Server LEFT JOIN

This query has been keeping me busy for the last couple of days. I tried to rewrite it with different ideas but I keep having the same problem. To simplify the problem I put part of my query in a view, this view returns 23 records. Using a left join I would like to add fields coming from the table tblDatPositionsCalc to these 23 records. As you can see I have an additional condition on the tblDatPositionsCalc in order to only consider the most recent records. With this condition it would return 21 records. The join should be on two fields together colAccount and colId.
I simply want the query to return the 23 records from the view and where possible have the information from tblDatPositionsCalc. There is actually only 2 records in the view without corresponding id and account in tblDatPositionsCalc, that means out of the 23 records only 2 will have missing values in the fields coming from the table tblDatPositionsCalc.
The problem with my query is that it only returns the 21 records from tblDatPositionsCalc. I don't understand why. I tried to move the condition on date in just after the JOIN condition but that did not help.
SELECT TOP (100) PERCENT
dbo.vwCurrPos.Account,
dbo.vwCurrPos.Id,
dbo.vwCurrPos.TickerBB,
dbo.vwCurrPos.colEquityCode,
dbo.vwCurrPos.colType,
dbo.vwCurrPos.colCcy,
dbo.vwCurrPos.colRegion,
dbo.vwCurrPos.colExchange,
dbo.vwCurrPos.[Instr Type],
dbo.vwCurrPos.colMinLastDay,
dbo.vwCurrPos.colTimeShift,
dbo.vwCurrPos.Strike,
dbo.vwCurrPos.colMultiplier,
dbo.vwCurrPos.colBetaVol,
dbo.vwCurrPos.colBetaEq,
dbo.vwCurrPos.colBetaFloor,
dbo.vwCurrPos.colBetaCurv,
dbo.vwCurrPos.colUndlVol,
dbo.vwCurrPos.colUndlEq,
dbo.vwCurrPos.colUndlFut,
tblDatPositionsCalc_1.colLots,
dbo.vwCurrPos.[Open Positions],
dbo.vwCurrPos.colListMatShift,
dbo.vwCurrPos.colStartTime,
tblDatPositionsCalc_1.colPrice,
tblDatPositionsCalc_1.colMktPrice,
dbo.vwCurrPos.colProduct,
dbo.vwCurrPos.colCalendar,
CAST(dbo.vwCurrPos.colExpiry AS DATETIME) AS colExpiry,
dbo.vwCurrPos.colEndTime,
CAST(tblDatPositionsCalc_1.colDate AS datetime) AS colDate,
dbo.vwCurrPos.colFund,
dbo.vwCurrPos.colExchangeTT,
dbo.vwCurrPos.colUserTag
FROM dbo.vwCurrPos
LEFT OUTER JOIN dbo.tblDatPositionsCalc AS tblDatPositionsCalc_1
ON tblDatPositionsCalc_1.colId = dbo.vwCurrPos.Id
AND tblDatPositionsCalc_1.colAccount = dbo.vwCurrPos.Account
WHERE (tblDatPositionsCalc_1.colDate =
(SELECT MAX(colDate) AS Expr1 FROM dbo.tblDatPositionsCalc))
ORDER BY
dbo.vwCurrPos.Account,
dbo.vwCurrPos.Id,
dbo.vwCurrPos.colEquityCode,
dbo.vwCurrPos.colRegion
Any idea what might cause the problem?
(Option 1) DrCopyPaste is right so your from clause would look like:
...
FROM dbo.vwCurrPos
LEFT OUTER JOIN dbo.tblDatPositionsCalc AS tblDatPositionsCalc_1
ON tblDatPositionsCalc_1.colId = dbo.vwCurrPos.Id
AND tblDatPositionsCalc_1.colAccount = dbo.vwCurrPos.Account
and (tblDatPositionsCalc_1.colDate =
(SELECT MAX(colDate) AS Expr1 FROM dbo.tblDatPositionsCalc))
...
reason: the where clause restriction of left joined to column = some expression with fail to return for "null = something" so the row will be removed.
(Option 2) As oppose to pushing code in to additional views where it is harder to maintain you can nest sql select statements;
select
X.x1,X.x2,
Y.*
from X
left join
(select Z.z1 as y1, Z.z2 as y2, Z.z3 as y3
from Z
where Z.z1 = (select max(Z.z1) from Z)
) as Y
on x.x1 = Y.y1 and X.x2 = Y.y2
The advantage here is you check each nested sub query a move out quickly. Although if you still building up more logic check out common table expressions (CTE's) http://msdn.microsoft.com/en-us/library/ms175972.aspx

How to separate content from my query into 3 distinct columns

I have got the following query, which puts the data that I need (text in the 245, 260 and 300 tags of my table called "bib") into a single column (called "Title"). Instead I would like to have this data separated into three distinct columns, one for each tag. Any suggestions would be very welcome
SELECT DISTINCT isbnEX_inverted.isbn, ISNULL(bib.text, CONVERT(varchar(255), bib_longtext.longtext)) AS Title, top_circ_summary.ranking, bib.tag, bib.bib#
FROM bib INNER JOIN
item ON bib.bib# = item.bib# INNER JOIN
isbnEX_inverted ON bib.bib# = isbnEX_inverted.bib# INNER JOIN
top_circ_summary ON item.bib# = top_circ_summary.bib# LEFT OUTER JOIN
bib_longtext ON bib.bib# = bib_longtext.bib# AND bib.tag = bib_longtext.tag
WHERE (isbnEX_inverted.isbn LIKE '%978__________%') AND (top_circ_summary.collection_group = 'jfic') AND (bib.tag in ('245', '520', '300'))
order by top_circ_summary.ranking
Here's a sample of the (tab separated) output
isbn Title ranking tag bib# 9780143307334 a217 p. :bill. ;c21
cm. 1 300 962366 9780143307334 aDiary of a wimpy kid :bthe third
wheel /cby Jeff Kinney. 1 245 962366 9780143307334 aTrying to find
a partner for the Valentine's Day dance, Greg finds solace in the fact
that his best friend Rowley also doesn't have a date, but an
unexpected twist might turn his night around. 1 520 962366

Resources