{"action":"created","comment":{"url":"https://api.github.com/repos/geotools/geotools/pulls/comments/173624646","pull_request_review_id":102852446,"id":173624646,"diff_hunk":"@@ -0,0 +1,129 @@\n+/*\n+ *    GeoTools - The Open Source Java GIS Toolkit\n+ *    http://geotools.org\n+ *\n+ *    (C) 2018, Open Source Geospatial Foundation (OSGeo)\n+ *\n+ *    This library is free software; you can redistribute it and/or\n+ *    modify it under the terms of the GNU Lesser General Public\n+ *    License as published by the Free Software Foundation;\n+ *    version 2.1 of the License.\n+ *\n+ *    This library is distributed in the hope that it will be useful,\n+ *    but WITHOUT ANY WARRANTY; without even the implied warranty of\n+ *    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU\n+ *    Lesser General Public License for more details.\n+ */\n+package org.geotools.gml.producer;\n+\n+import java.text.FieldPosition;\n+import java.text.NumberFormat;\n+import java.util.Locale;\n+\n+/**\n+ * Formats coordinates with a given number of decimals, using code more efficient than NumberFormat when possible\n+ */\n+public class CoordinateFormatter {\n+\n+    /**\n+     * The min value at which the decimal notation is used \n+     * (below it, the computerized scientific one is used instead)\n+     */\n+    private static final double DECIMAL_MIN = Math.pow(10, -3);\n+\n+    /**\n+     * The max value at which the decimal notation is used \n+     * (above it, the computerized scientific one is used instead)\n+     */\n+    private static final double DECIMAL_MAX = Math.pow(10, 7);\n+\n+    /** To be used for formatting numbers, uses US locale. */\n+    private final NumberFormat coordFormatter = NumberFormat.getInstance(Locale.US);\n+\n+    private final FieldPosition ZERO = new FieldPosition(0);\n+\n+    /**\n+     * The power of ten used for fast rounding\n+     */\n+    private final double scale;\n+\n+    /**\n+     * Whether we have to format in plain decimal numbers, or we can use scientific notation\n+     */\n+    private boolean forcedDecimal;\n+\n+\n+    public CoordinateFormatter(int numDecimals) {\n+        coordFormatter.setMaximumFractionDigits(numDecimals);\n+        coordFormatter.setGroupingUsed(false);\n+        scale = Math.pow(10, numDecimals);\n+    }\n+\n+    /**\n+     * Formats a number with the configured number of decimals. For better performance best use \n+     * {@link #format(double, StringBuffer)} against a re-used StringBuffer\n+     * @param x\n+     * @param sb\n+     */\n+    public String format(double x) {\n+        StringBuffer sb = new StringBuffer();\n+        format(x, sb);\n+        return sb.toString();\n+    }\n+\n+    /**\n+     * Formats a number with the configured number of decimals\n+     * @param x\n+     * @param sb\n+     */\n+    public StringBuffer format(double x, StringBuffer sb) {\n+        if ((Math.abs(x) >= DECIMAL_MIN && x < DECIMAL_MAX) || x == 0) {\n+            x = truncate(x);\n+            long lx = (long) x;\n+            if (lx == x)\n+                sb.append(lx);\n+            else\n+                sb.append(x);\n+        } else {\n+            if (forcedDecimal) {\n+                coordFormatter.format(x, sb, ZERO);\n+            } else {\n+                sb.append(truncate(x));\n+            }\n+        }\n+        \n+        return sb;\n+    }\n+\n+    final double truncate(double x) {\n+        return Math.floor(x * scale + 0.5) / scale;","path":"modules/library/main/src/main/java/org/geotools/gml/producer/CoordinateFormatter.java","position":99,"original_position":99,"commit_id":"14c805a8fde9772651fd003828539ce006c33c74","original_commit_id":"14c805a8fde9772651fd003828539ce006c33c74","user":{"login":"nmco","id":1590542,"avatar_url":"https://avatars1.githubusercontent.com/u/1590542?v=4","gravatar_id":"","url":"https://api.github.com/users/nmco","html_url":"https://github.com/nmco","followers_url":"https://api.github.com/users/nmco/followers","following_url":"https://api.github.com/users/nmco/following{/other_user}","gists_url":"https://api.github.com/users/nmco/gists{/gist_id}","starred_url":"https://api.github.com/users/nmco/starred{/owner}{/repo}","subscriptions_url":"https://api.github.com/users/nmco/subscriptions","organizations_url":"https://api.github.com/users/nmco/orgs","repos_url":"https://api.github.com/users/nmco/repos","events_url":"https://api.github.com/users/nmco/events{/privacy}","received_events_url":"https://api.github.com/users/nmco/received_events","type":"User","site_admin":false},"body":"It took me a few minutes to understand where the ``+0.5`` was coming from. Basically it just to round the decimal number right ? So if we want ``8`` decimals and want to truncate number ``3.123456786``:\r\n```\r\n3.123456786 * 10E8 + 05 = 312345678.6 + 0.5 = 312345679.1\r\n```\r\n\r\nThen we use ``Math.floor()`` to get the largest integer that is less than or equal to the argument.","created_at":"2018-03-10T15:03:51Z","updated_at":"2018-03-10T15:07:31Z","html_url":"https://github.com/geotools/geotools/pull/1824#discussion_r173624646","pull_request_url":"https://api.github.com/repos/geotools/geotools/pulls/1824","author_association":"CONTRIBUTOR","_links":{"self":{"href":"https://api.github.com/repos/geotools/geotools/pulls/comments/173624646"},"html":{"href":"https://github.com/geotools/geotools/pull/1824#discussion_r173624646"},"pull_request":{"href":"https://api.github.com/repos/geotools/geotools/pulls/1824"}}},"pull_request":{"url":"https://api.github.com/repos/geotools/geotools/pulls/1824","id":174005682,"html_url":"https://github.com/geotools/geotools/pull/1824","diff_url":"https://github.com/geotools/geotools/pull/1824.diff","patch_url":"https://github.com/geotools/geotools/pull/1824.patch","issue_url":"https://api.github.com/repos/geotools/geotools/issues/1824","number":1824,"state":"open","locked":false,"title":"[GEOT-5960] Allow setting number of decimals for encoding in GML 3.2 (fast path) and GML 3+ (complex features)","user":{"login":"aaime","id":325433,"avatar_url":"https://avatars3.githubusercontent.com/u/325433?v=4","gravatar_id":"","url":"https://api.github.com/users/aaime","html_url":"https://github.com/aaime","followers_url":"https://api.github.com/users/aaime/followers","following_url":"https://api.github.com/users/aaime/following{/other_user}","gists_url":"https://api.github.com/users/aaime/gists{/gist_id}","starred_url":"https://api.github.com/users/aaime/starred{/owner}{/repo}","subscriptions_url":"https://api.github.com/users/aaime/subscriptions","organizations_url":"https://api.github.com/users/aaime/orgs","repos_url":"https://api.github.com/users/aaime/repos","events_url":"https://api.github.com/users/aaime/events{/privacy}","received_events_url":"https://api.github.com/users/aaime/received_events","type":"User","site_admin":false},"body":"https://osgeo-org.atlassian.net/browse/GEOT-5960\r\n\r\nThis pull request is up for review but not for merge yet, a followup GeoServer pull request will land shortly.\r\n@bencaradocdavies you open the original issue on the GeoServer side, do you want to have a look? \r\nIn the meantime I've assigned review to our good @nmco ","created_at":"2018-03-09T13:31:51Z","updated_at":"2018-03-10T15:07:32Z","closed_at":null,"merged_at":null,"merge_commit_sha":"3aeb0929f419fff46f015b1f5a88c837bc281e93","assignee":null,"assignees":[],"requested_reviewers":[],"requested_teams":[],"labels":[],"milestone":null,"commits_url":"https://api.github.com/repos/geotools/geotools/pulls/1824/commits","review_comments_url":"https://api.github.com/repos/geotools/geotools/pulls/1824/comments","review_comment_url":"https://api.github.com/repos/geotools/geotools/pulls/comments{/number}","comments_url":"https://api.github.com/repos/geotools/geotools/issues/1824/comments","statuses_url":"https://api.github.com/repos/geotools/geotools/statuses/14c805a8fde9772651fd003828539ce006c33c74","head":{"label":"aaime:gml_decimals","ref":"gml_decimals","sha":"14c805a8fde9772651fd003828539ce006c33c74","user":{"login":"aaime","id":325433,"avatar_url":"https://avatars3.githubusercontent.com/u/325433?v=4","gravatar_id":"","url":"https://api.github.com/users/aaime","html_url":"https://github.com/aaime","followers_url":"https://api.github.com/users/aaime/followers","following_url":"https://api.github.com/users/aaime/following{/other_user}","gists_url":"https://api.github.com/users/aaime/gists{/gist_id}","starred_url":"https://api.github.com/users/aaime/starred{/owner}{/repo}","subscriptions_url":"https://api.github.com/users/aaime/subscriptions","organizations_url":"https://api.github.com/users/aaime/orgs","repos_url":"https://api.github.com/users/aaime/repos","events_url":"https://api.github.com/users/aaime/events{/privacy}","received_events_url":"https://api.github.com/users/aaime/received_events","type":"User","site_admin":false},"repo":{"id":4835074,"name":"geotools","full_name":"aaime/geotools","owner":{"login":"aaime","id":325433,"avatar_url":"https://avatars3.githubusercontent.com/u/325433?v=4","gravatar_id":"","url":"https://api.github.com/users/aaime","html_url":"https://github.com/aaime","followers_url":"https://api.github.com/users/aaime/followers","following_url":"https://api.github.com/users/aaime/following{/other_user}","gists_url":"https://api.github.com/users/aaime/gists{/gist_id}","starred_url":"https://api.github.com/users/aaime/starred{/owner}{/repo}","subscriptions_url":"https://api.github.com/users/aaime/subscriptions","organizations_url":"https://api.github.com/users/aaime/orgs","repos_url":"https://api.github.com/users/aaime/repos","events_url":"https://api.github.com/users/aaime/events{/privacy}","received_events_url":"https://api.github.com/users/aaime/received_events","type":"User","site_admin":false},"private":false,"html_url":"https://github.com/aaime/geotools","description":"Mirror of central GeoTools svn repository","fork":true,"url":"https://api.github.com/repos/aaime/geotools","forks_url":"https://api.github.com/repos/aaime/geotools/forks","keys_url":"https://api.github.com/repos/aaime/geotools/keys{/key_id}","collaborators_url":"https://api.github.com/repos/aaime/geotools/collaborators{/collaborator}","teams_url":"https://api.github.com/repos/aaime/geotools/teams","hooks_url":"https://api.github.com/repos/aaime/geotools/hooks","issue_events_url":"https://api.github.com/repos/aaime/geotools/issues/events{/number}","events_url":"https://api.github.com/repos/aaime/geotools/events","assignees_url":"https://api.github.com/repos/aaime/geotools/assignees{/user}","branches_url":"https://api.github.com/repos/aaime/geotools/branches{/branch}","tags_url":"https://api.github.com/repos/aaime/geotools/tags","blobs_url":"https://api.github.com/repos/aaime/geotools/git/blobs{/sha}","git_tags_url":"https://api.github.com/repos/aaime/geotools/git/tags{/sha}","git_refs_url":"https://api.github.com/repos/aaime/geotools/git/refs{/sha}","trees_url":"https://api.github.com/repos/aaime/geotools/git/trees{/sha}","statuses_url":"https://api.github.com/repos/aaime/geotools/statuses/{sha}","languages_url":"https://api.github.com/repos/aaime/geotools/languages","stargazers_url":"https://api.github.com/repos/aaime/geotools/stargazers","contributors_url":"https://api.github.com/repos/aaime/geotools/contributors","subscribers_url":"https://api.github.com/repos/aaime/geotools/subscribers","subscription_url":"https://api.github.com/repos/aaime/geotools/subscription","commits_url":"https://api.github.com/repos/aaime/geotools/commits{/sha}","git_commits_url":"https://api.github.com/repos/aaime/geotools/git/commits{/sha}","comments_url":"https://api.github.com/repos/aaime/geotools/comments{/number}","issue_comment_url":"https://api.github.com/repos/aaime/geotools/issues/comments{/number}","contents_url":"https://api.github.com/repos/aaime/geotools/contents/{+path}","compare_url":"https://api.github.com/repos/aaime/geotools/compare/{base}...{head}","merges_url":"https://api.github.com/repos/aaime/geotools/merges","archive_url":"https://api.github.com/repos/aaime/geotools/{archive_format}{/ref}","downloads_url":"https://api.github.com/repos/aaime/geotools/downloads","issues_url":"https://api.github.com/repos/aaime/geotools/issues{/number}","pulls_url":"https://api.github.com/repos/aaime/geotools/pulls{/number}","milestones_url":"https://api.github.com/repos/aaime/geotools/milestones{/number}","notifications_url":"https://api.github.com/repos/aaime/geotools/notifications{?since,all,participating}","labels_url":"https://api.github.com/repos/aaime/geotools/labels{/name}","releases_url":"https://api.github.com/repos/aaime/geotools/releases{/id}","deployments_url":"https://api.github.com/repos/aaime/geotools/deployments","created_at":"2012-06-29T17:32:49Z","updated_at":"2014-10-06T10:37:52Z","pushed_at":"2018-03-09T13:30:38Z","git_url":"git://github.com/aaime/geotools.git","ssh_url":"git@github.com:aaime/geotools.git","clone_url":"https://github.com/aaime/geotools.git","svn_url":"https://github.com/aaime/geotools","homepage":"http://geotools.org","size":172689,"stargazers_count":1,"watchers_count":1,"language":"Java","has_issues":false,"has_projects":true,"has_downloads":true,"has_wiki":true,"has_pages":false,"forks_count":1,"mirror_url":null,"archived":false,"open_issues_count":0,"license":null,"forks":1,"open_issues":0,"watchers":1,"default_branch":"master"}},"base":{"label":"geotools:master","ref":"master","sha":"db5f42b3a0dc95ae3017ce3f14adb23ddc6d5d10","user":{"login":"geotools","id":1187031,"avatar_url":"https://avatars2.githubusercontent.com/u/1187031?v=4","gravatar_id":"","url":"https://api.github.com/users/geotools","html_url":"https://github.com/geotools","followers_url":"https://api.github.com/users/geotools/followers","following_url":"https://api.github.com/users/geotools/following{/other_user}","gists_url":"https://api.github.com/users/geotools/gists{/gist_id}","starred_url":"https://api.github.com/users/geotools/starred{/owner}{/repo}","subscriptions_url":"https://api.github.com/users/geotools/subscriptions","organizations_url":"https://api.github.com/users/geotools/orgs","repos_url":"https://api.github.com/users/geotools/repos","events_url":"https://api.github.com/users/geotools/events{/privacy}","received_events_url":"https://api.github.com/users/geotools/received_events","type":"Organization","site_admin":false},"repo":{"id":2752522,"name":"geotools","full_name":"geotools/geotools","owner":{"login":"geotools","id":1187031,"avatar_url":"https://avatars2.githubusercontent.com/u/1187031?v=4","gravatar_id":"","url":"https://api.github.com/users/geotools","html_url":"https://github.com/geotools","followers_url":"https://api.github.com/users/geotools/followers","following_url":"https://api.github.com/users/geotools/following{/other_user}","gists_url":"https://api.github.com/users/geotools/gists{/gist_id}","starred_url":"https://api.github.com/users/geotools/starred{/owner}{/repo}","subscriptions_url":"https://api.github.com/users/geotools/subscriptions","organizations_url":"https://api.github.com/users/geotools/orgs","repos_url":"https://api.github.com/users/geotools/repos","events_url":"https://api.github.com/users/geotools/events{/privacy}","received_events_url":"https://api.github.com/users/geotools/received_events","type":"Organization","site_admin":false},"private":false,"html_url":"https://github.com/geotools/geotools","description":"Official GeoTools repository","fork":false,"url":"https://api.github.com/repos/geotools/geotools","forks_url":"https://api.github.com/repos/geotools/geotools/forks","keys_url":"https://api.github.com/repos/geotools/geotools/keys{/key_id}","collaborators_url":"https://api.github.com/repos/geotools/geotools/collaborators{/collaborator}","teams_url":"https://api.github.com/repos/geotools/geotools/teams","hooks_url":"https://api.github.com/repos/geotools/geotools/hooks","issue_events_url":"https://api.github.com/repos/geotools/geotools/issues/events{/number}","events_url":"https://api.github.com/repos/geotools/geotools/events","assignees_url":"https://api.github.com/repos/geotools/geotools/assignees{/user}","branches_url":"https://api.github.com/repos/geotools/geotools/branches{/branch}","tags_url":"https://api.github.com/repos/geotools/geotools/tags","blobs_url":"https://api.github.com/repos/geotools/geotools/git/blobs{/sha}","git_tags_url":"https://api.github.com/repos/geotools/geotools/git/tags{/sha}","git_refs_url":"https://api.github.com/repos/geotools/geotools/git/refs{/sha}","trees_url":"https://api.github.com/repos/geotools/geotools/git/trees{/sha}","statuses_url":"https://api.github.com/repos/geotools/geotools/statuses/{sha}","languages_url":"https://api.github.com/repos/geotools/geotools/languages","stargazers_url":"https://api.github.com/repos/geotools/geotools/stargazers","contributors_url":"https://api.github.com/repos/geotools/geotools/contributors","subscribers_url":"https://api.github.com/repos/geotools/geotools/subscribers","subscription_url":"https://api.github.com/repos/geotools/geotools/subscription","commits_url":"https://api.github.com/repos/geotools/geotools/commits{/sha}","git_commits_url":"https://api.github.com/repos/geotools/geotools/git/commits{/sha}","comments_url":"https://api.github.com/repos/geotools/geotools/comments{/number}","issue_comment_url":"https://api.github.com/repos/geotools/geotools/issues/comments{/number}","contents_url":"https://api.github.com/repos/geotools/geotools/contents/{+path}","compare_url":"https://api.github.com/repos/geotools/geotools/compare/{base}...{head}","merges_url":"https://api.github.com/repos/geotools/geotools/merges","archive_url":"https://api.github.com/repos/geotools/geotools/{archive_format}{/ref}","downloads_url":"https://api.github.com/repos/geotools/geotools/downloads","issues_url":"https://api.github.com/repos/geotools/geotools/issues{/number}","pulls_url":"https://api.github.com/repos/geotools/geotools/pulls{/number}","milestones_url":"https://api.github.com/repos/geotools/geotools/milestones{/number}","notifications_url":"https://api.github.com/repos/geotools/geotools/notifications{?since,all,participating}","labels_url":"https://api.github.com/repos/geotools/geotools/labels{/name}","releases_url":"https://api.github.com/repos/geotools/geotools/releases{/id}","deployments_url":"https://api.github.com/repos/geotools/geotools/deployments","created_at":"2011-11-10T23:22:12Z","updated_at":"2018-03-08T05:22:10Z","pushed_at":"2018-03-09T13:31:52Z","git_url":"git://github.com/geotools/geotools.git","ssh_url":"git@github.com:geotools/geotools.git","clone_url":"https://github.com/geotools/geotools.git","svn_url":"https://github.com/geotools/geotools","homepage":"http://geotools.org","size":173053,"stargazers_count":525,"watchers_count":525,"language":"Java","has_issues":false,"has_projects":true,"has_downloads":true,"has_wiki":true,"has_pages":false,"forks_count":637,"mirror_url":null,"archived":false,"open_issues_count":15,"license":null,"forks":637,"open_issues":15,"watchers":525,"default_branch":"master"}},"_links":{"self":{"href":"https://api.github.com/repos/geotools/geotools/pulls/1824"},"html":{"href":"https://github.com/geotools/geotools/pull/1824"},"issue":{"href":"https://api.github.com/repos/geotools/geotools/issues/1824"},"comments":{"href":"https://api.github.com/repos/geotools/geotools/issues/1824/comments"},"review_comments":{"href":"https://api.github.com/repos/geotools/geotools/pulls/1824/comments"},"review_comment":{"href":"https://api.github.com/repos/geotools/geotools/pulls/comments{/number}"},"commits":{"href":"https://api.github.com/repos/geotools/geotools/pulls/1824/commits"},"statuses":{"href":"https://api.github.com/repos/geotools/geotools/statuses/14c805a8fde9772651fd003828539ce006c33c74"}},"author_association":"OWNER"}}
{"actor":{"display_login":"nmco"}}