Saturday, 24 October 2015

sql - Link to another table in CartoDB infowindow



I have two layers in CartoDB, one with spatial data, school_count, and one without, heisman_winners. I want to visualize my spatial layer but when a user clicks on a feature I want it to display data from my non-spatial table. I have a common column between the two tables: 'school'. On my non-spatial tables I have multiple records that correspond to each 'school' (i.e. 'class', 'name', 'position', 'percentage', 'points', 'year'). Can I set a SQL query that runs when a user clicks a feature that will return each corresponding record from another table? So when they click it will look like this:


'name' , 'position', 'points', 'percentage', 'year', 'class'
'name' , 'position', 'points', 'percentage', 'year', 'class'
'name' , 'position', 'points', 'percentage', 'year', 'class'
etc...


I fond this block but I am not sure exactly how to tweak it correctly.




raster - How to sum up pixel values in QGIS?


I need to know the value of the sum of all pixels in a raster. However, in the Statistics section of the Metadata tab in Properties, under Sum of All Cells it always indicates 0.00. I have tried with different raster formats to no avail. What am I doing wrong? How can I get this value?




postgresql - How to add sfcgal to an already postgis enabled database


I have a postgres 9.1 database with postgis 2.1, and I recently needed to add sfcgal to it, so i rebuilt postgis 2.1, but I can't find out how to update the postgis extension in postgres without dropping all data. Is there a way to do that ?


ALTER EXTENSION postgis UPDATE;

returns a notice saying i'm already at version 2.1.0


Thanks for any tips



Answer



just run the sfcgal.sql file into your existing database. Unfortunately, cgal is not packaged as an extension (actually, for exactly the reason your case demonstrates: you can't have two extensions with the same version and different capabilities).



Changing the the range for MODIS NDVI



I need to get MODIS NDVI (MOD13A3), but output from EarthExplorer or Daac2Disk HDF format is a numerical range is -2000 to 9996.


But I need for futher work basic range -1 to 1.


Is there any conversion method?




Why some coordinate systems define x-axis as northings and some as easting?


The definition of north and east are pretty straight forward to grasp but only becomes difficult when used interchangably with x-y coordinates which have varying definitions for the direction of the axes. In Mathematics y was always vertical and x was always horizontal so logically I would assume that "up" == "northing" == "y" and "along" == "easting" == "x".


Why is this not the case in GIS?




arcpy - Pass a variable from Modelbuilder into a Python script


I'm new to modelbuilder and python. I have used modelbuilder to iterate through all rows in a shapefile (Points), process them, and produce a seperate output shapefile for each row. The filename is generated using an inline variable (I think that is the correct terminology). i.e. all output files have identical filenames except that the value of the PointID from the original input file is appended to the filename to enable them to be distinguished. This works fine.


I've also written a python script to take the above output files from the model one at a time and do additional processing. At present the input filename is hard coded into the script (i.e. I have to edit the script to change the input filename each time I run it). Again this works fine while just processing a single file. However, I want to integrate the model and the python script so that I can run the script for each row in the original feature class automatically.


My question is therefore, how do I replace the hard coded name of the input file with a variable in the python script and how do I pass the filename to the script.





Friday, 23 October 2015

python - Querying Thousands of Points with ST_Value()?


Currently I have an application using the psychopg2 Python library to query a database for elevation data. My current python implementation looks like the following:


   def GetElevation(lat, lon, cur):
point = "'SRID=4326;POINT({} {})'::geometry".format(lat,lon)
cur.execute("SELECT ST_Value(rast, {}) FROM dted0 WHERE ST_Intersects(rast, {});".format(point, point))
return cur.fetchone()[0]


This works, but I am was curious if I could pass in an array of latitutdes and an array of longitudes. I tried building a "point string" with several thousand queries, but I get an error saying that I can only pass 100 arguments to the function.


Is there a way to pass all the points I need in a single query?


The reason I am doing this is because I assumed doing only one transaction with the database would be faster rather than looping for each query.


-------------------EDIT 1----------------------


In an attempt to implement the top rated post, I've done the following:


import json
json_arr = []
for lat, lon in zip(lats, lons):
json_arr.append({'lat:': lat, 'lon':lon})

json_str = json.dumps(json_arr)

Next I defined a function per his advice:


def GetElevationsBatch(json_elevations, cur):
cur.execute(
"""SELECT ST_Value(rast,z.point)
FROM dted0
JOIN (
SELECT ST_SetSRID(ST_MakePoint(lat, lon), 4326) as point
FROM json_to_recordset(%s) AS z(lon double precision, lat double precision)

) AS z
ON ST_Intersects(rast,z.point)""", (json_elevations,))
return cur.fetchall()

However, when I call the function I get I don't get good things:


a = GetElevationsBatch(json_str, cur)
print("a = ", a)
# Result: a = None

-----------------------EDIT 2--------------------------



I've left the python plugin out for now until I can find the best query, so here is my latest attempt:


SELECT (ST_Dump(gv.geom)).geom, gv.val
FROM srtm , LATERAL ST_Intersection(rast, 'SRID=4326;MULTIPOINT(-111.305048568 38.0601633931,-111.822991286 38.6025320796,-111.136977796 38.3631992596,-111.206470006 38.971228396)'::geometry) AS gv
WHERE ST_Intersects(rast, 'SRID=4326;MULTIPOINT(-111.305048568 38.0601633931,-111.822991286 38.6025320796,-111.136977796 38.3631992596,-111.206470006 38.971228396)'::geometry);

In the above query, I am attempting to use the notion of MULTIPOINT geometries. However, I found that these calls "work", but they are much slower than my original method of simply querying for every point. I understand that I am not running on any amazing hardware, but should calls really take on the order of seconds for a simple elevation query? Seems to me there is something awry here. It is taking nearly 3 seconds to retrieve 4 points with the above code. To compare to my original method, it only takes about 100ms to retrieve 4 points.


Shouldn't a solution in which I only query the database once be quick than one where I have to query several times?



Answer



Okay I thought of another reason why my original answer might be slow. If the bounding box of your point covers enough area, it would produce a lot of rasters that require checking the Slow way.


So here is another answer, still using multi-point but doing your original single check approach:



SELECT dp.geom, ST_Value(dted0.rast, geom) AS val
FROM ST_Dump(your_multi_point_here) AS dp
JOIN dted0 ON ST_Intersects(dted0.rast,dp.geom) ;

arcpy - Changing output name when exporting data driven pages to JPG?

Is there a way to save the output JPG, changing the output file name to the page name, instead of page number? I mean changing the script fo...