I am creating a database to track the (normalized) coordinates of events within a coordinate system. Think: a basketball shot chart, where coordinates of shot attempts are stored relative to where they were taken on the basketball court, in both positive and negative directions from center court.

I'm not exactly sure the best way to store this information in a database in order to give myself the most flexibility in utilizing the data. My options are:

  1. Store a JSON object in a TEXT/CHAR column with X and Y properties
  2. Store each X and Y coordinate in two DECIMAL columns
  3. Use MySQL's spatial POINT object to store the coordinate

My goal is to store a normalized vector2 (as a percentage of the bounding box), so I can map the positions back out onto a rectangle of any size.

It would be nice to be able to do calculations, like distance from another point, but my understanding of spatial objects is that it is more for geographical coordinates than a normalized vector. The other options, however, make calculations a bit more difficult though, currently for my project, they aren't a definitive requirement.

Is it possible to use spatial POINT for this and would calculations be similar to that of measuring geographical points?

有帮助吗?

解决方案

It is possible to use POINT, but it may be more of a hassle retrieving or modifying the values as it is stored in binary form. You won't be able to view or modify the field directly; you would use an SQL statement to get the components or create a new POINT to replace the old one.

They are stored as numbers and you can do normal mathematical operations on them. Geospatial-type calculations on distance would use other geospatial data types such as LINESTRING.

To insert a point you would have to create a point from two numbers (I think for your case, there would be no issues with the size of the numbers) :

INSERT INTO coordinatetable(testpoint) VALUES (GeomFromText('POINT(-100473882.33 2133151132.13)'));

INSERT INTO coordinatetable(testpoint) VALUES (GeomFromText('POINT(0.3 -0.213318973)'));

To retrieve it you would have to select the X and Y value separately

SELECT X(testpoint), Y(testpoint) from coordinatetable;

For your case, I would go with storing X and Y coordinate in two DECIMAL columns. It's easier to retrieve, modify and having X and Y coordinates separate would allow you direct access to to the coordinates rather than extract the values you want from data stored in a single field. For larger data sets, it may speed up your queries.

For example:

  • Whether the player is past half court only requires Y-coordinate
  • How much help the player could possibly get from the backboard would rely more on the X-coordinate than the Y-coordinate (X closer to zero => Straighter shot)
  • Whether the player usually scores from locations close to the long edges of the court would rely more on the X-coordinate than the Y-coordinate (X approaches 1 or -1)
许可以下: CC-BY-SA归因
不隶属于 StackOverflow
scroll top