What is the best way to store shapes in a database

I am using an application that deals with Java Shapes. each user login and retrieval of inventory from the MySql database. forms have a different constructor and behavior.

So what is the best way to save your shapes? Here are some of my ideas:

  • Serialize the shape and save it as a Blob. but I would have a version problem if I need to change the Shape class taking into account the large database size and query performance
  • Make a Shape table with common fields such as width and height, and a table for each shape that references the Shape table. This can lead to many tables ...
  • Create a Shape table with all possible field and just place zero for a form that does not need this field ...

What do you think?

+4
source share
5 answers

I chose the second solution because it is easy to manage and expand. The serialization parameter is not practical, because if the structure of the serialized model changes (for example: adding an attribute), I have to control the versions of the model. The third option is not the right approach to creating databases.

0
source

Technically not the answer, but maybe the problem here is in SQL? I think a document storage system like CouchDB could be a much more efficient solution for this scenario.

I think something like this:

{ "_id": "whatever", "_rev": "whatever", "boundingBox": [ [0 0], [2 2] ], "size": [2 2], "circle": { "center": [1 1], "radius": 1 } 

}

The string "circle" will change the name and details depending on the shape. Rectangles would have angles (similar to "boundingBox" ), ellipsoids would ... Whatever the ellipsoids define: p

+2
source

Some simple ideas:

  • You can save a list of vertices defining a polygon.
  • You can save a list of vectors that define the edges of the polygon.

How they can be stored in the database:

  • Primary key form master table with identifier called "shapeID"
  • Vector table with primary key identifying vectorID, foreign key to corresponding ShapeID, origin (x, y, z), direction (x, y, z) and length.

Additional metadata can be assigned to the form with other tables, which also refer to the shape identifier by foreign key.

+1
source

There is really no β€œbetter” way to store the inheritance hierarchy in a database. Most database courses have three models: a single table with zeros, a table for a subclass, and a table for a specific class. You can find a good explanation in this blog post . You can choose any of them that you prefer if you just want to match the attributes of your shapes in the database.

If you need something more advanced, MySql provides a Geometry Type specifically designed to hold geometric shapes.

+1
source

I would vote:

  • Create a Shape table with all possible fields:

    • both width and height
    • base and height
    • radius ...
    • shape type (e.g. 0 = circle, 1 = square, 3 = triangle, ...)
  • One table, all possible forms, one instance / line of the form

IMHO ...

Q: How many different shapes do you think you can have? How many columns do you consider as each figure?

PS: A "Polygon" (consisting of an arbitrary number of points) can occupy two separate tables. But for most figures (circle, ellipse, square, rectangle, etc.) There should not be more than 4 or 5 columns / instances and should easily fit into one table.

+1
source

All Articles