-
Notifications
You must be signed in to change notification settings - Fork 2
Fix ESRIGeoJSON: handle Empty reply from server and paginate past maxRecordCount #52
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
Merged
Merged
Changes from all commits
Commits
Show all changes
3 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -1,42 +1,93 @@ | ||
| require 'net/http' | ||
| require 'open-uri' | ||
| require 'openssl' | ||
|
|
||
| module SpatialFeatures | ||
| module Download | ||
| # file can be a url, path, or file, any of which can return be a zipped archive | ||
| def self.open(file) | ||
| file = URI.open(file) | ||
| file = normalize_file(file) if file.is_a?(StringIO) | ||
| return file | ||
| end | ||
| REMOTE_URL = %r{\Ahttps?://}i.freeze | ||
|
|
||
| # Seconds to wait for a remote source, applied both to establishing the connection and to | ||
| # each read. Without it a hung server holds an import worker open indefinitely. | ||
| mattr_accessor :timeout | ||
| self.timeout = 60 | ||
|
|
||
| # file can be a url, path, or file, any of which can return be a zipped archive | ||
| def self.open_each(path_or_url, unzip: nil, **unzip_options) | ||
| file = Download.open(path_or_url) | ||
| files = if unzip && Unzip.is_zip?(file) | ||
| find_in_zip(file, find: unzip, **unzip_options) | ||
| else | ||
| [file] | ||
| # Errors meaning a remote source could not be read at all, as opposed to being read and | ||
| # found unusable. `OpenURI::HTTPError` covers the 4xx and 5xx replies. | ||
| UNREACHABLE_ERRORS = [::OpenURI::HTTPError, ::SocketError, ::SystemCallError, | ||
| ::Net::OpenTimeout, ::Net::ReadTimeout, ::OpenSSL::SSL::SSLError].freeze | ||
|
|
||
| class << self | ||
| # Returns an open File for `file`, which may be a URL, a path, or a File. The content may | ||
| # be a zipped archive; `::open_each` unwraps one. | ||
| # | ||
| # @raise [SpatialFeatures::ImportError] when a remote source cannot be reached. | ||
| def open(file) | ||
| file = fetch(file) | ||
| file = normalize_file(file) if file.is_a?(StringIO) | ||
| return file | ||
| end | ||
|
|
||
| return files.map { |f| File.open(f) } | ||
| end | ||
| # Returns the body of `path_or_url` as a String, without writing it to disk. | ||
| # | ||
| # @raise [SpatialFeatures::ImportError] when a remote source cannot be reached. | ||
| def read(path_or_url) | ||
| fetch(path_or_url).read | ||
| end | ||
|
|
||
| # Returns an open File for each source in `path_or_url`, unwrapping an archive when | ||
| # `unzip` is given a pattern its entries can match. | ||
| def open_each(path_or_url, unzip: nil, **unzip_options) | ||
| file = Download.open(path_or_url) | ||
| files = if unzip && Unzip.is_zip?(file) | ||
| find_in_zip(file, find: unzip, **unzip_options) | ||
| else | ||
| [file] | ||
| end | ||
|
|
||
| def self.normalize_file(file) | ||
| Tempfile.new.tap do |temp| | ||
| temp.binmode | ||
| temp.write(file.read) | ||
| temp.rewind | ||
| return files.map { |f| File.open(f) } | ||
| end | ||
| end | ||
|
|
||
| def self.entries(file) | ||
| file = Kernel.open(file) | ||
| file = normalize_file(file) if file.is_a?(StringIO) | ||
| Unzip.entries(file) | ||
| end | ||
| def normalize_file(file) | ||
| Tempfile.new.tap do |temp| | ||
| temp.binmode | ||
| temp.write(file.read) | ||
| temp.rewind | ||
| end | ||
| end | ||
|
|
||
| # Returns the entries of the archive at `file` without extracting them. | ||
| def entries(file) | ||
| file = fetch(file) | ||
| file = normalize_file(file) if file.is_a?(StringIO) | ||
| Unzip.entries(file) | ||
| end | ||
|
|
||
| def find_in_zip(file, find:, **unzip_options) | ||
| Unzip.paths(file, find: find, **unzip_options) | ||
| end | ||
|
|
||
| private | ||
|
|
||
| def self.find_in_zip(file, find:, **unzip_options) | ||
| Unzip.paths(file, find: find, **unzip_options) | ||
| # Returns an IO for `file`: fetched over the network when it is a remote URL, opened from | ||
| # disk when it is any other String, and left to open itself otherwise. | ||
| # | ||
| # @note A local path goes to `File.open`, never `URI.open`. `URI.open` hands anything | ||
| # that is not a URL to `Kernel#open`, which runs the name as a command when it begins | ||
| # with a pipe. | ||
| # @note Timeouts and the unreachable rescue apply only to a remote URL. `URI.open` | ||
| # rejects the timeouts when handed an already open file, and `Errno::ENOENT` for a | ||
| # local path is a `SystemCallError` that callers turn into a message for the person who | ||
| # uploaded the file, without naming the path the server looked in. | ||
| def fetch(file) | ||
| return URI.open(file) unless file.is_a?(String) | ||
| return File.open(file) unless file.match?(REMOTE_URL) | ||
|
|
||
| begin | ||
| URI.open(file, :open_timeout => timeout, :read_timeout => timeout) | ||
| rescue *UNREACHABLE_ERRORS => e | ||
| raise SpatialFeatures::ImportError, "This source could not be reached. #{e.message}" | ||
| end | ||
| end | ||
| end | ||
| end | ||
| end |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,42 @@ | ||
| require 'open3' | ||
|
|
||
| module SpatialFeatures | ||
| # Runs the GDAL command line tools. | ||
| # | ||
| # Every argument reaches the tool as a single argv entry, so a path or a projection taken | ||
| # from an uploaded archive arrives as one string rather than as shell syntax. | ||
| module GDAL | ||
| class << self | ||
| # Returns what `tool` wrote to standard output. | ||
| # | ||
| # @param tool [String] the executable name, such as `ogr2ogr`. | ||
| # @param args [Array<String>] one argv entry each. | ||
| # @return [String] the output, empty when the tool wrote nothing. | ||
| # @raise [Errno::ENOENT] when the tool is not installed. | ||
| def capture(tool, *args) | ||
| Open3.capture2(*argv(tool, args)).first | ||
| end | ||
|
|
||
| # Runs `tool` for its exit status rather than its output. | ||
| # | ||
| # @param tool [String] the executable name, such as `ogr2ogr`. | ||
| # @param args [Array<String>] one argv entry each. | ||
| # @return [Boolean] true when the tool exited successfully. | ||
| # @raise [Errno::ENOENT] when the tool is not installed. | ||
| def run(tool, *args) | ||
| system(*argv(tool, args)) | ||
| end | ||
|
|
||
| private | ||
|
|
||
| # Returns the argv to spawn `tool` with. | ||
| # | ||
| # @note The command is a two element array so that Ruby spawns the executable directly. | ||
| # Both `system` and `Open3` fall back to a shell when handed a lone string, which | ||
| # would put every argument here back in reach of shell parsing. | ||
| def argv(tool, args) | ||
| [[tool.to_s, tool.to_s], *args.map(&:to_s)] | ||
| end | ||
| end | ||
| end | ||
| end |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.