Trouble getting count of points inside polygon with PostGIS - gis

I am trying to find points from one table that fall within polygons from another table. Both tables have a geom column, both are using SRID 4326 which I confirmed using SELECT ST_SRID(geom) FROM poursafe.ca_licenses LIMIT 1;
I didn't create the polygon table with an SRID which should have been 2229. So, I ran this to convert it to 4326 which may be part of the problem?
ALTER TABLE public.ca_la_la_areas_neighborhoods ALTER COLUMN geom TYPE geometry(MultiPolygon,4326)
USING ST_Transform(ST_SetSRID(geom,2229),4326);
Both tables have spatial indexes (but, I don't know enough about them to know if I'm supposed to be running the query using them as my target tables?)
The query I am using to get the count is this:
SELECT count(*)
FROM public.ca_la_la_areas_neighborhoods pol
JOIN poursafe.ca_licenses poi ON (ST_Within(poi.geom, pol.geom));
I've tried this and many other attempts and get nothing but zeros as the count or select results.
CREATE ca_la_la_areas_neighborhoods (
gid INTEGER,
comty_name VARCHAR (40),
cert VARCHAR (3),
shape_star VARCHAR,
shape_stle VARCHAR,
geom GEOM
);
CREATE ca_licenses (
id BIGINT,
license VARCHAR,
type VARCHAR,
master VARCHAR,
lat DOUBLE PRECISION,
lon DOUBLE PRECISION,
geom GEOM
);

Related

Create Table query is giving syntax error due to decimal format

I am using Access 2007. There is following query to create the table.
I am trying to set max length as 12 and 2 for decimal precision for quantity column
create table tbl_sales
(
sales_id autoincrement primary key,
item_id number,
quantity_sold double(12, 2)
)
but due to some reasons it gives syntax error message at double. Am I missing anything?
Double has no parameters for precision or scale. It's a fixed 8-byte floating point format.
I still like this overview of data types best:
http://allenbrowne.com/ser-49.html
For DECIMAL (precision, scale) it states:
Not available in the Access query interface or DAO. Use ADO to Execute the DDL query statement.
So you will have to do exactly this if you want to run a CREATE TABLE statement with Decimal type.
Alternatively, use DAO with the Database.CreateTableDef method.
If you want to use Double, it's just that.
This works for me in Access 2010, executed from query design:
create table tbl_sales
(
sales_id autoincrement primary key,
item_id long,
quantity_sold double
)
Note that number also creates a Double column, so if you want Long Int for an ID column, use Long.
There are two major problems in the quer.
First The auto increment is not correct.
Second there should be decimal in place of double.
here is the query .
create table tbl_sales
(
sales_id INT AUTO_INCREMENT primary key,
item_id number,
quantity_sold Decimal(12, 2)
)

Mysql: can a query be served by Btree and geospatial index?

We use mysql 5.7 to store geospatial data. A table contains 5 columns store_id (String), tenant_id (String), type (enum), status(enum), boundaries (Multipolygon). The boundaries column has only one polygon but the type was set as MultiPolygon.
Our query has
SELECT DISTINCT store_id
FROM ${boundariesTable}
WHERE tenant_id = '${tenantId}'
AND status = '${status}'
AND type <> 'Z'
AND ST_Contains(boundaries, GeomFromText(
'Point(${coords.lng} ${coords.lat})'))
This DB call is very slow when boundary data has circles with several geolocation points. Hence, we want to use a geospatial index for the boundaries key. Would we need to modify the above query to use a geospatial index for the boundaries column? If yes then how should we structure the query? Without other parameters like type and tenantId, the number of rows increases multifold. So I am apprehensive to remove all other constraints and retain only the ST_Contains part of the query.
Thank

MySQL 5.7 Geospatial Index, will points concentrate in one point cause performance matter

I have many images's information store in MySQL, most of image have geo location info but others don't.
Since MySQL geospatial index doesn't allow null geometry point, I have to insert some nonsense point to the table like POINT(1000 1000).
If all images that don't have geo location info are set to POINT(1000 1000) will cause any problem?
Or is there any better way to satisfy this need?
Geospatial index is not needed in such case. Faster and simpler way to do this is to use KEY (lon, lat) and SELECT * FROM the_table WHERE lon > a and lon < b and lat > c and lat < d;
POINT or GEOMETRY can be null in MySQL (using either InnoDB or MyISAM storage engine) However, when you are creating the table the columns must be declared as not null.
Simply do this:
CREATE TABLE images(filename TEXT, location POINT NOT NULL);
INSERT INTO images(filename, location) VALUES ('hello.png', NULL);
SELECT count(*) FROM images WHERE location IS NULL;

Whats the metric used in MYSQL Spatial?

I am trying to determine the metric system for MySQL Spatial 5.6.12.
For example, I have the following table that created to store point geometry for multiple records.
CREATE TABLE POINTS_DATA(
RECORD_ID INT(15),
STREET_ADDRESS VARCHAR(50),
CITY VARCHAR(50),
STATE VARCHAR(25),
ZIPCODE VARCHAR(11),
LOCATION_GEO_CODES_SDO POINT NOT NULL,
SPATIAL INDEX(LOCATION_GEO_CODES_SDO),
PRIMARY KEY(RECORD_ID)
) ENGINE=MyISAM;
After the table was created, I inserted some records into the table successfully.
Now I have constructed the following query to fetch all the records that are within one mile from a specified LAT/LONG. Here is the query I run for that:
SELECT RECORD_ID, STREET_ADDRESS, CITY, STATE, ZIPCODE
, GLength(LineStringFromWKB(LineString(LOCATION_GEO_CODES_SDO
, POINT(-85.123,39.113))) AS DISTANCE
FROM POINTS_DATA
HAVING DISTANCE < 1 ORDER BY DISTANCE;
After running this query, I do get some records, but the distance does not seem to be in Miles or meters. It’s some fractional value like 0.0123, 0.0145, etc…
I could not find any documentation on this anywhere in MySQL? Does anyone know what metric system is in use in MySQL? And if there is, how can I convert it into miles?
That means, if I need to run the query above to fetch all records within one mile, how do I reconstruct it?
MySQL Spatial uses the spatial reference system of the geometry for GLength. You haven't defined what (if any) SRID you are using for your data, but guessing from the values you provide, the SRID is the default one. As a result, the values you are getting are just the Cartesian distance between the two points, using degrees as units.
Since you're only calculating distance of a line, you could drop GLength and calculate the distance yourself using the Haversine formula. See MySQL Great Circle Distance (Haversine formula).

Find rows having min and max range values that covers the selected millions of values

I have a table with symbol names (e.g. functions) and their start memory address and end memory address placement. Now I want to look up many addresses that are between the start and end addresses and map to each symbol name (or simpler the start addr as below example).
I do a query like this:
SELECT r.caller_addr AS caller_addr,sm.addrstart AS caller FROM rets AS r
JOIN symbolmap AS sm ON r.caller_addr BETWEEN sm.addrstart AND sm.addrend;
rets is a table that contains approximately a million caller_addr. The symbolmap table is created as:
CREATE TABLE
symbolmap
(addrstart BIGINT NOT NULL,
addrend BIGINT NOT NULL,
name VARCHAR(45),
PRIMARY KEY (addrstart),
UNIQUE INDEX (addrend)) ENGINE = InnoDB;
All the addrstart to addrend rows are none overlapping, i.e. there can only be one row hit for any requested addr (r.caller_addr in the example). The symbolmap table contains 42000 rows. I have tried a few other index methods as well, but still the select takes very long time (many 10s of minutes) and has not managed to finish.
Any suggestions on better indexes or other select statements that have better performance? I'm running this on MySQL 5.1.41 and I don't need to worry about portability.
When I have searched for what others do I only find results with constant boundaries and not when finding the row having the right boundaries. But it seems to me like a quite general problem.
Try to combine the two columns in a single index:
CREATE TABLE
symbolmap
(addrstart BIGINT NOT NULL,
addrend BIGINT NOT NULL,
name VARCHAR(45),
PRIMARY KEY (addrstart, addrend)
) ENGINE = InnoDB;
Also make sure that caller_addr is also bigint