Thanks in advance for any insight. I am new to using the spatial functions in SQL Server. I would appreciate it if someone could point me to the basic correct approach to solving a problem I have.
I am working with a client that monitors gas wells that are accessible from waterways. In a given day, a boat travels from point A to point B along the river. I have that "route" as a series of Lat/Long values that can be plotted on a map. I'm thinking that this would be used as a LineString in SQL Server.
I also have a list of known wells, along with their Lat/Long positions. I would like to store these records in a database, and index the Lat/Long with a spatial index of some sort. I'm not 100% sure how to index the table correctly, but it would contain the columns Lat/Long/WellName/Owner, etc.
The feature needs to do the following: Given the Lat/Long of the route the boat takes in a given day (the LineString and all of it's points), find all of the wells that are within 2000 feet of that line. The workers will get off of the boat and go to the well to perform maintenance on them. Each day they will work a different section of the river.
Again, I'm looking for directional advice on what the Wells table (tblWells) needs to be, and also create a stored procedure that accepts the Lat/Long of the line points as a nvarchar(MAX) input parameter. For now, it only needs to return all of the wells that are within 2000 feet of any point on the line.
Sincere thanks to anyone who takes the time to help.
Kindest regards...
First, you'd need a table of wells:
create table dbo.Wells (
WellId int identity not null,
Name varchar(50) not null,
Owner varchar(50) not null,
Location geography not null
)
And then a procedure to search for wells within a certain distance of a given path:
create procedure dbo.FindWells
(
#Path geography,
#Distance float
) as
begin
declare #buffer geography = #Path.STBuffer(#Distance);
select WellId
from dbo.Wells
where #buffer.STContains(Location);
)
If you really want to pass your path as a varchar, go for it; just make transforming it into a geography instance with geography::STLineFromText or something like that.
As for the indexing on the table, create a spatial index on the Location column. Play around with the resolution, but I'm guessing that given your use case, the first three resolutions will be low with the last being high. Play around with it, though.
Related
So I am working on a game server and I have a player inventory that stores as a json field. I am trying to create a query that specifically pulls two of the data sets in the field but those data sets repeat. For example two of the data sets are going to be name: and amount: but the same field will use these data sets as many times as there are items in the inventory table.
So example here I call on the two data sets on a specific vehicle trunk identified by its plate in the database. What I get back are Null Null.
SELECT json_extract(items, '$."amount"') AS amount, json_extract(items, '$."name"') AS name FROM trunkitems WHERE plate='6DV689SW';
What I need it to do its return an expanding table for just those two data points.
The proper solution is to use JSON_TABLE(), but this function is not implemented in MariaDB until version 10.6. You said you are currently using MariaDB 10.3, so you'll have to upgrade to use this solution.
SELECT j.amount, j.name
FROM trunkitems
CROSS JOIN JSON_TABLE(items, '$[*]' COLUMNS(
amount INT PATH '$.amount',
name VARCHAR(20) PATH '$.name'
)) AS j
WHERE j.name IS NOT NULL;
Dbfiddle
If you can't upgrade, then I recommend you store data in normal rows and columns, and not use JSON.
In fact, I usually recommend to avoid using JSON in MySQL or MariaDB in all cases. It just makes queries harder to write and maintain and optimize, and makes data take more storage space.
I need some help, I have a stored procedure(SP) that contains NO parameters in SSMS. The SP generates spatial data by joining 2 tables one table containing two fields needed for labelling purposes and the other from a table that containsthe GEO field.
This SP populates spatial data in SSMS which I can view and interact with. All seems well so far...
I then use SSRS to create a data source and a dataset of the said stored procedure. Following this I create a new .rdl and insert a map choosing SQL Server spatial query as the source then selecting the stored procedure dataset. Following this SSRS attempts to join the data and I then receive the error from the Microsoft SQL Server Report Designer:
Unable to determine the spatial data type in the specified dataset field: GEO
I have searched the the internet for answers already and none suit predominately because they all refer to parameters causing the issue however my query does not contain nor pass any. I have tried the geo field as both a geography and geometry datatype no avail.
Any help or advice would be great.
I have just been playing with a Heat Map in SSRS using SQL Jason's example (http://www.sqljason.com/2012/03/heat-maps-for-ssrs-using-map-control.html).
It looks like your data doesn't have enough points. According to Teo Lachev (http://prologika.com/CS/blogs/blog/archive/2009/08/30/heat-maps-as-reports.aspx):
SQL Server describes polygons in Well-Known Text (WKT) standard
sponsored by Open Geospatial Consortium. For instance, a polygon is
described as five (surprise) points on the coordinate system, as
shown below. The fifth point is the same as the first point.
The example format you gave only had four points. The first and last are the same so there seems to be a missing point or you are not graphing rectangles. It doesn't look like the Spatial Data supports any other shapes. The Map wizard only shows rectangles in the Wizard when creating the map.
I'm looking around for any answer which I could for my purpose but I'm not able to find it, so I've decided to submit a question.
I have several tables with geography type fields inside and i need to define a where clause for finding all the records within an ellipse.
My ellipse properties are: minoraxis, majoraxis, point and rotation. I already have the where clause which look for a geography inside a circle shape. It's something like this:
select *
from locations
where shape.STIntersects(geography::STPointFromText('POINT(32.113, -81.3225)', 4326).STBuffer(10)) = 1
How could I do it?
Thanks in advance.
I have a table with more than 50 columns and it is already normalized. Most number of columns have data type as nvarchar.
Now, I need to write a stored procedure that inserts a record in the same table.
I have not sure whether
1) I should write a SP with 50 parameters or
2) I should write SP that takes Xml as a parameter and extract record to be inserted into table as mentioned here.
How to insert xml data into table in sql server 2005
Please advise in terms of performance. Thanks.
According to this link : http://msdn.microsoft.com/en-us/library/ms143432.aspx , you can have up to 2100 parameters. So this won't be a problem.
In term of performance, it'll take extra processing to extract each node from the XML, but it certainly gives more flexibility (and readability) if you'd like to produce dynamics inserts (even if it could be done with standard parameters, with a bit more work I guess).
I have a Microsoft SQL Server database with a table of Locations. Each location has its address and latitude and longitude coordinates.
In my application, the user can input a zipcode and we return a list of close by locations.
This is my approach.
a) Using a zipcode DB I search the lat,lon for the zipcode (this is the center point).
b) I run a search like this
SELECT Position_ID, distance(pos_lon,pos_lat,zip_lon,zip_lat) dist
FROM Positions
ORDER BY dist
"distance" is a function that calculates the distance between two points.
The problem is that as my location DB increases the time to run these searches is starting to grow.
Is there a better approach?
If you're using SQL Server 2008, you probably want to look into the geography column type, the STDistance function and spatial indexes.
Spatial Indexing Overview
Geography Methods Supported by Spatial Indexes
How to: Create a Spatial Index
I would do a calculation of the box surrounding your zip code at the specified distance to get the lat/lon values for the four corners. Then do a simple comparison of your position values to select those which fall inside the square. This way, you don't have to calculate the distance from your zip code to every point in your db for every search.
Which version of SQL server? If 2008 have a look at the spatial type.
http://www.microsoft.com/sqlserver/2008/en/us/spatial-data.aspx
EDIT: Also limiting that query with a where would probably help as you likely don't want to go to far in any particular direction.