Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Can I use brick/geo for a wood cutting project ? #31

Open
fabrice-dresscodes opened this issue Jul 27, 2021 · 6 comments
Open

Can I use brick/geo for a wood cutting project ? #31

fabrice-dresscodes opened this issue Jul 27, 2021 · 6 comments

Comments

@fabrice-dresscodes
Copy link

fabrice-dresscodes commented Jul 27, 2021

example
Hello,

This is my first project in Geometry. I'm used to develop and search for PHP libraries but WKT, WKB, Geometry Calculation is quiet new for me. I'm not sure yet how to construct a simple closed circle so be tolerant :)

Context

I need to construct an HTML/JS/PHP for e-commerce website. This application should construct a final shape for woods panels (for kitchen, desktops, etc...).

I start with a simple shape (rectangle or ellipse). Then I add many cuts : interns and externals.

Then I will transform all my shapes in jpeg layers to make a preview.

Technical Context

Nginx Server/ PHP 7.1+ / MySql / Geos extension installed

Technical Expectations

I need to store each shapes to be sure there is no intersection between them, calculate a minimal distance between shapes, get the area (bonus to calculate the final weight).

I find easily how to store polygons (for my rectangle) but I understand that 'circle' (or curve) are not standard shapes so many functions are not available (distance, intersection).

Issue :

  • Should I use the brick/geo library for my project ? Alone ? With an other library ?
  • If not, do you have any advice or person to recommend ?

Thanks,

@BenMorel
Copy link
Member

BenMorel commented Jul 29, 2021

Hi,

You can very well use brick/geo for this purpose, you'll only be limited by the capabilities of the geometry engine you use.

Line-based geometries are supported by all engines, including MySQL and GEOS, so no problem for area/distance calculations with polygons such as rectangles. Polygons can have « holes » in them, which perfectly fits your use case.

For ellipses however, while the library itself has support for those (CircularString, CurvePolygon; see here for an example of a CircularString), not many engines support them; from a quick test, only PostGIS and SQL Server recognize them, and are able to perform accurate calculations such as area on them.

Given that SQL Server uses a non-standard syntax, it is not currently supported by brick/geo. So you're left with PostgreSQL with PostGIS as your only possible engine if you're looking for real support for ellipses. Note that you don't need to switch your database to PostgreSQL: you just need to have an extra PostgreSQL instance that brick/geo can connect to (docker provides some ready to use), just to perform the calculations, then you could save the results to MySQL.

Possible alternative

Another solution, compatible with MySQL & GEOS, would be to perform calculations using an approximation of every ellipse, as a Polygon with say, 100 points or more. This is the poor man's solution to the problem, but given a sufficient number of points, the precision can fall within your acceptable range. There is a built-in function, buffer(), that can return an approximation of a circle given a center Point, although the result is dependent on the engine, so it may not be exactly what you expect. You'll be probably better off calculating your own Polygon coordinates with trigonometric functions, for example:

$numPointsInPolygon = 100;

$centerX = 0.0;
$centerY = 0.0;

$radius = 1.0;

$polygonPoints = [];

for ($i = 0; $i < $numPointsInPolygon; ++$i) {
    $angle = 2.0 * pi() * $i / $numPointsInPolygon;

    $x = $centerX + $radius * cos($angle);
    $y = $centerY + $radius * sin($angle);

    $polygonPoints[] = Point::xy($x, $y);
}

// close the ring
$polygonPoints[] = $polygonPoints[0];

$circleApprox = Polygon::of(LineString::of(...$polygonPoints));

Wrapping up

Given that PostGIS supports the whole set of geometries that can solve the problem at hand, I would probably design my app around this engine, which again, does not mean that you cannot use MySQL for storage. But you could possibly achieve the same result (within a given precision) using MySQL or GEOS directly.

@BenMorel BenMorel changed the title Shloud I use the brick/geo library for my project ? Should I use the brick/geo library for my project ? Jul 29, 2021
@BenMorel BenMorel changed the title Should I use the brick/geo library for my project ? Can I use brick/geo for a wood cutting project ? Jul 29, 2021
@fabrice-dresscodes
Copy link
Author

Thanks for the complete answer !

The circular string example is perfect for me to understand.

I let you know after my test with PostGIS.

@fabrice-dresscodes
Copy link
Author

fabrice-dresscodes commented Feb 22, 2022

Hello,

I'm trying to represent an ellipse withPOSTGIS

ellipse

When I read

I make :

where a, b, c, d are the "peaks" points anticlockwise

$geometry = $reader->read("CURVEPOLYGON(COMPOUNDCURVE(CIRCULARSTRING(
$xa $ya,
$xb $yb,
$xc $yc),CIRCULARSTRING(
$xc $yc,
$xd $yd,
$xa $ya
)))"
);

but the calculated area is wrong. It give 2.47 m2. It shoud be 2.82m2

What do I miss ?

@BenMorel
Copy link
Member

Hi, sorry for the (very) late answer. Did you find the solution to your problem?

  • If yes, could you please share it?
  • If no, could you please give the exact coordinates contained in $xa, $ya etc.? And PostgreSQL / PostGIS versions please.

@fabrice-dresscodes
Copy link
Author

Hello,

No I've finally re-calculate manually the area for ellipse.

It did not works as expected. There were a small difference in results.

I'm checking the version.

@fabrice-dresscodes
Copy link
Author

There are the versions :
PG13 Postgis 3.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants